Skip to content
Snippets Groups Projects
Commit ab81490e authored by Jonathan Rose's avatar Jonathan Rose
Browse files

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


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@406361 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent 11d4a03d
Branches
Tags
No related merge requests found
...@@ -252,8 +252,8 @@ static struct tables *find_table(const char *database, const char *orig_tablenam ...@@ -252,8 +252,8 @@ static struct tables *find_table(const char *database, const char *orig_tablenam
struct columns *column; struct columns *column;
struct tables *table; struct tables *table;
struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330); struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330);
PGresult *result; RAII_VAR(PGresult *, result, NULL, PQclear);
int exec_result; int exec_result;
char *fname, *ftype, *flen, *fnotnull, *fdef; char *fname, *ftype, *flen, *fnotnull, *fdef;
int i, rows; int i, rows;
...@@ -339,14 +339,12 @@ static struct tables *find_table(const char *database, const char *orig_tablenam ...@@ -339,14 +339,12 @@ static struct tables *find_table(const char *database, const char *orig_tablenam
ast_debug(1, "Query of table structure complete. Now retrieving results.\n"); ast_debug(1, "Query of table structure complete. Now retrieving results.\n");
if (exec_result != 0) { if (exec_result != 0) {
ast_log(LOG_ERROR, "Failed to query database columns for table %s\n", orig_tablename); ast_log(LOG_ERROR, "Failed to query database columns for table %s\n", orig_tablename);
PQclear(result);
AST_LIST_UNLOCK(&psql_tables); AST_LIST_UNLOCK(&psql_tables);
return NULL; return NULL;
} }
if (!(table = ast_calloc(1, sizeof(*table) + strlen(orig_tablename) + 1))) { if (!(table = ast_calloc(1, sizeof(*table) + strlen(orig_tablename) + 1))) {
ast_log(LOG_ERROR, "Unable to allocate memory for new table structure\n"); ast_log(LOG_ERROR, "Unable to allocate memory for new table structure\n");
PQclear(result);
AST_LIST_UNLOCK(&psql_tables); AST_LIST_UNLOCK(&psql_tables);
return NULL; return NULL;
} }
...@@ -365,7 +363,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam ...@@ -365,7 +363,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam
if (!(column = ast_calloc(1, sizeof(*column) + strlen(fname) + strlen(ftype) + 2))) { 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); ast_log(LOG_ERROR, "Unable to allocate column element for %s, %s\n", orig_tablename, fname);
PQclear(result);
destroy_table(table); destroy_table(table);
AST_LIST_UNLOCK(&psql_tables); AST_LIST_UNLOCK(&psql_tables);
return NULL; return NULL;
...@@ -395,7 +392,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam ...@@ -395,7 +392,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam
} }
AST_LIST_INSERT_TAIL(&table->columns, column, list); AST_LIST_INSERT_TAIL(&table->columns, column, list);
} }
PQclear(result);
AST_LIST_INSERT_TAIL(&psql_tables, table, list); AST_LIST_INSERT_TAIL(&psql_tables, table, list);
ast_rwlock_rdlock(&table->lock); ast_rwlock_rdlock(&table->lock);
...@@ -420,7 +416,7 @@ static struct columns *find_column(struct tables *t, const char *colname) ...@@ -420,7 +416,7 @@ static struct columns *find_column(struct tables *t, const char *colname)
static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, va_list ap) static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
int num_rows = 0, pgresult; int num_rows = 0, pgresult;
struct ast_str *sql = ast_str_thread_get(&sql_buf, 100); struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100); struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
...@@ -485,7 +481,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab ...@@ -485,7 +481,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
ast_mutex_lock(&pgsql_lock); ast_mutex_lock(&pgsql_lock);
if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) { if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return NULL; return NULL;
} }
...@@ -501,7 +496,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab ...@@ -501,7 +496,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows); ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) { if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return NULL; return NULL;
} }
...@@ -530,7 +524,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab ...@@ -530,7 +524,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s@%s.\n", tablename, database); ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s@%s.\n", tablename, database);
} }
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return var; return var;
...@@ -538,7 +531,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab ...@@ -538,7 +531,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap) static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
int num_rows = 0, pgresult; int num_rows = 0, pgresult;
struct ast_str *sql = ast_str_thread_get(&sql_buf, 100); struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100); struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
...@@ -639,7 +632,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char ...@@ -639,7 +632,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql)); ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n", ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
PQresultErrorMessage(result), PQresStatus(result_status)); PQresultErrorMessage(result), PQresStatus(result_status));
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
ast_config_destroy(cfg); ast_config_destroy(cfg);
return NULL; return NULL;
...@@ -657,7 +649,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char ...@@ -657,7 +649,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows); ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) { if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
ast_config_destroy(cfg); ast_config_destroy(cfg);
return NULL; return NULL;
...@@ -689,7 +680,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char ...@@ -689,7 +680,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
ast_debug(1, "PostgreSQL RealTime: Could not find any rows in table %s.\n", table); ast_debug(1, "PostgreSQL RealTime: Could not find any rows in table %s.\n", table);
} }
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return cfg; return cfg;
...@@ -698,7 +688,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char ...@@ -698,7 +688,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
static int update_pgsql(const char *database, const char *tablename, const char *keyfield, static int update_pgsql(const char *database, const char *tablename, const char *keyfield,
const char *lookup, va_list ap) const char *lookup, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
int numrows = 0, pgresult; int numrows = 0, pgresult;
const char *newparam, *newval; const char *newparam, *newval;
struct ast_str *sql = ast_str_thread_get(&sql_buf, 100); struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
...@@ -805,7 +795,6 @@ static int update_pgsql(const char *database, const char *tablename, const char ...@@ -805,7 +795,6 @@ static int update_pgsql(const char *database, const char *tablename, const char
ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql)); ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n", ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
PQresultErrorMessage(result), PQresStatus(result_status)); PQresultErrorMessage(result), PQresStatus(result_status));
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return -1; return -1;
} }
...@@ -830,7 +819,7 @@ static int update_pgsql(const char *database, const char *tablename, const char ...@@ -830,7 +819,7 @@ static int update_pgsql(const char *database, const char *tablename, const char
static int update2_pgsql(const char *database, const char *tablename, va_list ap) static int update2_pgsql(const char *database, const char *tablename, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
int numrows = 0, pgresult, first = 1; int numrows = 0, pgresult, first = 1;
struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 16); struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 16);
const char *newparam, *newval; const char *newparam, *newval;
...@@ -945,7 +934,7 @@ static int update2_pgsql(const char *database, const char *tablename, va_list ap ...@@ -945,7 +934,7 @@ static int update2_pgsql(const char *database, const char *tablename, va_list ap
static int store_pgsql(const char *database, const char *table, va_list ap) static int store_pgsql(const char *database, const char *table, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
Oid insertid; Oid insertid;
struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256); struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256);
struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256); struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256);
...@@ -1007,7 +996,6 @@ static int store_pgsql(const char *database, const char *table, va_list ap) ...@@ -1007,7 +996,6 @@ static int store_pgsql(const char *database, const char *table, va_list ap)
} }
insertid = PQoidValue(result); insertid = PQoidValue(result);
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid); ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid);
...@@ -1026,7 +1014,7 @@ static int store_pgsql(const char *database, const char *table, va_list ap) ...@@ -1026,7 +1014,7 @@ static int store_pgsql(const char *database, const char *table, va_list ap)
static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap) static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
int numrows = 0; int numrows = 0;
int pgresult; int pgresult;
struct ast_str *sql = ast_str_thread_get(&sql_buf, 256); struct ast_str *sql = ast_str_thread_get(&sql_buf, 256);
...@@ -1108,7 +1096,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table, ...@@ -1108,7 +1096,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
const char *file, struct ast_config *cfg, const char *file, struct ast_config *cfg,
struct ast_flags flags, const char *suggested_incl, const char *who_asked) struct ast_flags flags, const char *suggested_incl, const char *who_asked)
{ {
PGresult *result = NULL; RAII_VAR(PGresult *, result, NULL, PQclear);
long num_rows; long num_rows;
struct ast_variable *new_v; struct ast_variable *new_v;
struct ast_category *cur_cat = NULL; struct ast_category *cur_cat = NULL;
...@@ -1155,7 +1143,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table, ...@@ -1155,7 +1143,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
char *field_cat_metric = PQgetvalue(result, rowIndex, 3); char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
if (!strcmp(field_var_name, "#include")) { if (!strcmp(field_var_name, "#include")) {
if (!ast_config_internal_load(field_var_val, cfg, flags, "", who_asked)) { if (!ast_config_internal_load(field_var_val, cfg, flags, "", who_asked)) {
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return NULL; return NULL;
} }
...@@ -1178,7 +1165,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table, ...@@ -1178,7 +1165,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
"PostgreSQL RealTime: Could not find config '%s' in database.\n", file); "PostgreSQL RealTime: Could not find config '%s' in database.\n", file);
} }
PQclear(result);
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return cfg; return cfg;
...@@ -1298,7 +1284,7 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap ...@@ -1298,7 +1284,7 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
ast_debug(1, "About to run ALTER query on table '%s' to add column '%s'\n", tablename, elm); 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) { if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
ast_mutex_unlock(&pgsql_lock); ast_mutex_unlock(&pgsql_lock);
return -1; return -1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment