[asterisk-commits] jrose: branch 12 r406389 - in /branches/12: ./ res/res_config_pgsql.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jan 24 15:25:49 CST 2014


Author: jrose
Date: Fri Jan 24 15:25:47 2014
New Revision: 406389

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=406389
Log:
res_config_pgsql: Fix a memory leak and use RAII_VAR for cleanup when practical

Review: https://reviewboard.asterisk.org/r/3141/
........

Merged revisions 406360 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 406361 from http://svn.asterisk.org/svn/asterisk/branches/11

Modified:
    branches/12/   (props changed)
    branches/12/res/res_config_pgsql.c

Propchange: branches/12/
------------------------------------------------------------------------------
Binary property 'branch-11-merged' - no diff available.

Modified: branches/12/res/res_config_pgsql.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_config_pgsql.c?view=diff&rev=406389&r1=406388&r2=406389
==============================================================================
--- branches/12/res/res_config_pgsql.c (original)
+++ branches/12/res/res_config_pgsql.c Fri Jan 24 15:25:47 2014
@@ -252,8 +252,8 @@
 	struct columns *column;
 	struct tables *table;
 	struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330);
-        PGresult *result;
-        int exec_result;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
+	int exec_result;
 	char *fname, *ftype, *flen, *fnotnull, *fdef;
 	int i, rows;
 
@@ -339,14 +339,12 @@
 	ast_debug(1, "Query of table structure complete.  Now retrieving results.\n");
 	if (exec_result != 0) {
 		ast_log(LOG_ERROR, "Failed to query database columns for table %s\n", orig_tablename);
-		PQclear(result);
 		AST_LIST_UNLOCK(&psql_tables);
 		return NULL;
 	}
 
 	if (!(table = ast_calloc(1, sizeof(*table) + strlen(orig_tablename) + 1))) {
 		ast_log(LOG_ERROR, "Unable to allocate memory for new table structure\n");
-		PQclear(result);
 		AST_LIST_UNLOCK(&psql_tables);
 		return NULL;
 	}
@@ -365,7 +363,6 @@
 
 		if (!(column = ast_calloc(1, sizeof(*column) + strlen(fname) + strlen(ftype) + 2))) {
 			ast_log(LOG_ERROR, "Unable to allocate column element for %s, %s\n", orig_tablename, fname);
-			PQclear(result);
 			destroy_table(table);
 			AST_LIST_UNLOCK(&psql_tables);
 			return NULL;
@@ -395,7 +392,6 @@
 		}
 		AST_LIST_INSERT_TAIL(&table->columns, column, list);
 	}
-	PQclear(result);
 
 	AST_LIST_INSERT_TAIL(&psql_tables, table, list);
 	ast_rwlock_rdlock(&table->lock);
@@ -420,7 +416,7 @@
 
 static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, const struct ast_variable *fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	int num_rows = 0, pgresult;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
 	struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
@@ -482,7 +478,6 @@
 	ast_mutex_lock(&pgsql_lock);
 
         if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
-		PQclear(result);
 		ast_mutex_unlock(&pgsql_lock);
 		return NULL;
         }
@@ -498,7 +493,6 @@
 		ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
 
 		if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
-			PQclear(result);
 			ast_mutex_unlock(&pgsql_lock);
 			return NULL;
 		}
@@ -527,7 +521,6 @@
 		ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s@%s.\n", tablename, database);
 	}
 
-	PQclear(result);
 	ast_mutex_unlock(&pgsql_lock);
 
 	return var;
@@ -535,7 +528,7 @@
 
 static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, const struct ast_variable *fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	int num_rows = 0, pgresult;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
 	struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
@@ -633,7 +626,6 @@
 			ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
 			ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
 						PQresultErrorMessage(result), PQresStatus(result_status));
-			PQclear(result);
 			ast_mutex_unlock(&pgsql_lock);
 			ast_config_destroy(cfg);
 			return NULL;
@@ -651,7 +643,6 @@
 		ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
 
 		if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
-			PQclear(result);
 			ast_mutex_unlock(&pgsql_lock);
 			ast_config_destroy(cfg);
 			return NULL;
@@ -683,7 +674,6 @@
 		ast_debug(1, "PostgreSQL RealTime: Could not find any rows in table %s.\n", table);
 	}
 
-	PQclear(result);
 	ast_mutex_unlock(&pgsql_lock);
 
 	return cfg;
@@ -692,7 +682,7 @@
 static int update_pgsql(const char *database, const char *tablename, const char *keyfield,
 						const char *lookup, const struct ast_variable *fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	int numrows = 0, pgresult;
 	const struct ast_variable *field = fields;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
@@ -795,7 +785,6 @@
 			ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
 			ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
 						PQresultErrorMessage(result), PQresStatus(result_status));
-			PQclear(result);
 			ast_mutex_unlock(&pgsql_lock);
 			return -1;
 		}
@@ -820,7 +809,7 @@
 
 static int update2_pgsql(const char *database, const char *tablename, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	int numrows = 0, pgresult, first = 1;
 	struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 16);
 	const struct ast_variable *field;
@@ -932,7 +921,7 @@
 
 static int store_pgsql(const char *database, const char *table, const struct ast_variable *fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	Oid insertid;
 	struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256);
 	struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256);
@@ -991,7 +980,6 @@
         }
 
 	insertid = PQoidValue(result);
-	PQclear(result);
 	ast_mutex_unlock(&pgsql_lock);
 
 	ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid);
@@ -1010,7 +998,7 @@
 
 static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, const struct ast_variable *fields)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	int numrows = 0;
 	int pgresult;
 	struct ast_str *sql = ast_str_thread_get(&sql_buf, 256);
@@ -1091,7 +1079,7 @@
 									   const char *file, struct ast_config *cfg,
 									   struct ast_flags flags, const char *suggested_incl, const char *who_asked)
 {
-	PGresult *result = NULL;
+	RAII_VAR(PGresult *, result, NULL, PQclear);
 	long num_rows;
 	struct ast_variable *new_v;
 	struct ast_category *cur_cat = NULL;
@@ -1138,7 +1126,6 @@
 			char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
 			if (!strcmp(field_var_name, "#include")) {
 				if (!ast_config_internal_load(field_var_val, cfg, flags, "", who_asked)) {
-					PQclear(result);
 					ast_mutex_unlock(&pgsql_lock);
 					return NULL;
 				}
@@ -1161,7 +1148,6 @@
 				"PostgreSQL RealTime: Could not find config '%s' in database.\n", file);
 	}
 
-	PQclear(result);
 	ast_mutex_unlock(&pgsql_lock);
 
 	return cfg;
@@ -1281,7 +1267,7 @@
 				ast_debug(1, "About to run ALTER query on table '%s' to add column '%s'\n", tablename, elm);
 
 			        if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
-					ast_mutex_unlock(&pgsql_lock);
+						ast_mutex_unlock(&pgsql_lock);
 				        return -1;
 			        }
 




More information about the asterisk-commits mailing list