소스 검색

db_text : fix memory allocation

Luis Azedo 8 년 전
부모
커밋
856518ab2d
1개의 변경된 파일28개의 추가작업 그리고 16개의 파일을 삭제
  1. 28 16
      src/modules/db_text/dbt_raw_query.c

+ 28 - 16
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 @@ error:
 	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);