[sr-dev] git:master:856518ab: db_text : fix memory allocation

Luis Azedo luis at 2600hz.com
Thu Mar 9 12:40:31 CET 2017


Module: kamailio
Branch: master
Commit: 856518ab2d31659be606f8ef607ea061eec079f4
URL: https://github.com/kamailio/kamailio/commit/856518ab2d31659be606f8ef607ea061eec079f4

Author: Luis Azedo <luis at 2600hz.com>
Committer: Luis Azedo <luis at 2600hz.com>
Date: 2017-03-03T17:34:38Z

db_text : fix memory allocation

---

Modified: src/modules/db_text/dbt_raw_query.c

---

Diff:  https://github.com/kamailio/kamailio/commit/856518ab2d31659be606f8ef607ea061eec079f4.diff
Patch: https://github.com/kamailio/kamailio/commit/856518ab2d31659be606f8ef607ea061eec079f4.patch

---

diff --git a/src/modules/db_text/dbt_raw_query.c b/src/modules/db_text/dbt_raw_query.c
index 82078b8..cda4a38 100644
--- a/src/modules/db_text/dbt_raw_query.c
+++ b/src/modules/db_text/dbt_raw_query.c
@@ -89,8 +89,8 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
 	dbt_trim(table_ptr);
 
 	table.s = table_ptr;
-	table.len = len;
-	LM_DBG("using table '%.*s'\n", table.len, table.s);
+    table.len = strlen(table_ptr);
+    LM_DBG("using table '%.*s'\n", table.len, table.s);
 
 	if(dbt_use_table(_h, &table) != 0) {
 		LM_ERR("use table is invalid %.*s\n", table.len, table.s);
@@ -117,15 +117,22 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
 		result_cols = pkg_malloc(sizeof(db_key_t) * cols);
 		memset(result_cols, 0, sizeof(db_key_t) * cols);
 		for(n=0; n < cols; n++) {
-			result_cols[n] = &_tbc->colv[n]->name;
+			result_cols[n] = pkg_malloc(sizeof(str));
+			result_cols[n]->len = _tbc->colv[n]->name.len;
+			result_cols[n]->s = pkg_malloc((_tbc->colv[n]->name.len + 1) * sizeof(char));
+			strncpy(result_cols[n]->s, _tbc->colv[n]->name.s, _tbc->colv[n]->name.len);
+			result_cols[n]->s[_tbc->colv[n]->name.len] = '\0';
 		}
 	} else {
 		cols = ncols;
 		result_cols = pkg_malloc(sizeof(db_key_t) * cols);
 		memset(result_cols, 0, sizeof(db_key_t) * cols);
 		for(n=0; *(tokens + n); n++) {
-			result_cols[n]->s = *(tokens + n);
+			result_cols[n] = pkg_malloc(sizeof(str));
 			result_cols[n]->len = strlen(*(tokens + n));
+			result_cols[n]->s = pkg_malloc((strlen(*(tokens + n)) + 1) * sizeof(char));
+			strncpy(result_cols[n]->s, *(tokens + n), strlen(*(tokens + n)));
+			result_cols[n]->s[strlen(*(tokens + n))] = '\0';
 		}
 	}
 
@@ -159,6 +166,10 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
 	dbt_clean_where(nc, _k, _op, _v);
 
 	if(result_cols) {
+		for(n=0; n < cols; n++) {
+			pkg_free(result_cols[n]->s);
+			pkg_free(result_cols[n]);
+		}
 		pkg_free(result_cols);
 	}
 
@@ -200,9 +211,14 @@ int dbt_raw_query_update(db1_con_t* _h, str* _s, db1_res_t** _r)
 	table_ptr[len] = '\0';
 	dbt_trim(table_ptr);
 	table.s = table_ptr;
-	table.len = len;
+	table.len = strlen(table_ptr);
 
 	where_ptr = strcasestr(_s->s, " where ");
+	if(where_ptr == NULL) {
+		LM_ERR("specify where clause to determine keys\n");
+		goto error;
+	}
+	
 	fields_end_ptr = where_ptr;
 	len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
 	fields_ptr = pkg_malloc(len);
@@ -216,11 +232,6 @@ int dbt_raw_query_update(db1_con_t* _h, str* _s, db1_res_t** _r)
 		goto error;
 	}
 
-
-	if(where_ptr == NULL) {
-		LM_ERR("specify where clause to determine keys\n");
-		goto error;
-	}
 	nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
 	if(nkeys < 1) {
 		LM_ERR("needsa at least one key\n");
@@ -383,9 +394,14 @@ int dbt_raw_query_replace(db1_con_t* _h, str* _s, db1_res_t** _r)
 	table_ptr[len] = '\0';
 	dbt_trim(table_ptr);
 	table.s = table_ptr;
-	table.len = len;
+	table.len = strlen(table_ptr);
 
 	where_ptr = strcasestr(_s->s, " where ");
+	if(where_ptr == NULL) {
+		LM_ERR("specify where clause to determine keys\n");
+		goto error;
+	}
+	
 	fields_end_ptr = where_ptr;
 	len = fields_end_ptr - ( fields_start_ptr + 4) + 1;
 	fields_ptr = pkg_malloc(len);
@@ -399,11 +415,6 @@ int dbt_raw_query_replace(db1_con_t* _h, str* _s, db1_res_t** _r)
 		goto error;
 	}
 
-
-	if(where_ptr == NULL) {
-		LM_ERR("specify where clause to determine keys\n");
-		goto error;
-	}
 	nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv);
 	if(nkeys < 1) {
 		LM_ERR("needsa at least one key\n");
@@ -492,6 +503,7 @@ int dbt_raw_query(db1_con_t* _h, str* _s, db1_res_t** _r)
 		return res;
 	}
 
+	((dbt_con_p)_h->tail)->affected = 0;
 	dbt_trim(_s->s);
 	_s->len = strlen(_s->s);
 




More information about the sr-dev mailing list