Browse code

db_mysql: properly free db result structure if db_mysql_convert_result() fails

- reported by Torrey Searle, FS#420

Daniel-Constantin Mierla authored on 16/04/2014 17:43:45
Showing 2 changed files
... ...
@@ -251,12 +251,12 @@ static int db_mysql_store_result(const db1_con_t* _h, db1_res_t** _r)
251 251
 	if (db_mysql_convert_result(_h, *_r) < 0) {
252 252
 		LM_ERR("error while converting result\n");
253 253
 		LM_DBG("freeing result set at %p\n", _r);
254
-		pkg_free(*_r);
255
-		*_r = 0;
256 254
 		/* all mem on Kamailio API side is already freed by
257 255
 		 * db_mysql_convert_result in case of error, but we also need
258
-		 * to free the mem from the mysql lib side */
259
-		mysql_free_result(RES_RESULT(*_r));
256
+		 * to free the mem from the mysql lib side, internal pkg for it
257
+		 * and *_r */
258
+		db_mysql_free_result(_h, *_r);
259
+		*_r = 0;
260 260
 #if (MYSQL_VERSION_ID >= 40100)
261 261
 		while( mysql_more_results(CON_CONNECTION(_h)) && mysql_next_result(CON_CONNECTION(_h)) > 0 ) {
262 262
 			MYSQL_RES *res = mysql_store_result( CON_CONNECTION(_h) );
... ...
@@ -69,6 +69,7 @@ int db_mysql_get_columns(const db1_con_t* _h, db1_res_t* _r)
69 69
 	}
70 70
 	
71 71
 	if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
72
+		RES_COL_N(_r) = 0;
72 73
 		LM_ERR("could not allocate columns\n");
73 74
 		return -3;
74 75
 	}
... ...
@@ -173,6 +174,7 @@ static inline int db_mysql_convert_rows(const db1_con_t* _h, db1_res_t* _r)
173 174
 
174 175
 	if (db_allocate_rows(_r) < 0) {
175 176
 		LM_ERR("could not allocate rows");
177
+		RES_ROW_N(_r) = 0;
176 178
 		return -2;
177 179
 	}
178 180