Browse code

db_text : memory allocation / release in raw query

Luis Azedo authored on 11/06/2015 16:17:08
Showing 2 changed files
... ...
@@ -120,10 +120,15 @@ int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r)
120 120
 
121 121
 
122 122
 	dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
123
+    _tbc = NULL;
123 124
 
124 125
 	res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, NULL, _r);
125 126
 
126 127
 error:
128
+
129
+    if(_tbc)
130
+        dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
131
+    
127 132
 	if(tokens) {
128 133
 	    for (i = 0; *(tokens + i); i++) {
129 134
 	    	pkg_free(*(tokens + i));
... ...
@@ -141,6 +141,7 @@ void dbt_clean_where(int n, db_key_t* _k, db_op_t* _op, db_val_t* _v)
141 141
 	if(_k) {
142 142
 		for(i=0; i < n; i++) {
143 143
 			pkg_free(_k[i]->s);
144
+			pkg_free(_k[i]);
144 145
 		}
145 146
 		pkg_free(_k);
146 147
 	}
... ...
@@ -167,7 +168,7 @@ int dbt_build_where(char* where, db_key_t** _k, db_op_t** _o, db_val_t** _v)
167 167
 	char** _o1 = NULL;
168 168
 	db_val_t* _v1 = NULL;
169 169
 	regmatch_t* matches = NULL;
170
-	int l;
170
+	int n, l;
171 171
 	int len;
172 172
 	regex_t preg;
173 173
 	int offset = 0;
... ...
@@ -214,9 +215,9 @@ int dbt_build_where(char* where, db_key_t** _k, db_op_t** _o, db_val_t** _v)
214 214
 		//      needs changes in dbt_query / dbt_row_match
215 215
 
216 216
 		l = matches[2].rm_eo - matches[2].rm_so;
217
-		_k1[idx] = pkg_malloc(sizeof(str)+l+1);
217
+		_k1[idx] = pkg_malloc(sizeof(db_key_t));
218 218
 		_k1[idx]->len = l;
219
-		_k1[idx]->s = (char*) (_k1[idx]+sizeof(str));
219
+		_k1[idx]->s = pkg_malloc(sizeof(char) * (l+1));
220 220
 		strncpy(_k1[idx]->s, buffer+matches[2].rm_so, l);
221 221
 		_k1[idx]->s[l]='\0';
222 222
 
... ...
@@ -237,11 +238,11 @@ int dbt_build_where(char* where, db_key_t** _k, db_op_t** _o, db_val_t** _v)
237 237
 			_v1[idx].val.str_val.s = pkg_malloc(l+1);
238 238
 			strncpy(_v1[idx].val.str_val.s, buffer+matches[5].rm_so, l);
239 239
 		}
240
-/*		int n;
241
-		for(n=0; n < MAX_MATCH; n++) {
242
-			LM_ERR("MATCH RESULT %d - %d,%d\n", n, matches[n].rm_so, matches[n].rm_eo);
243
-		}
244
-*/
240
+
241
+//		for(n=0; n < MAX_MATCH; n++) {
242
+//			LM_ERR("MATCH RESULT %d - %d,%d\n", n, matches[n].rm_so, matches[n].rm_eo);
243
+//		}
244
+
245 245
 		if(matches[0].rm_eo != -1)
246 246
 			offset += matches[0].rm_eo;
247 247