From 8c63b6d4680abba91e2ee84c0aee15b407ed789d Mon Sep 17 00:00:00 2001 From: Jonathan Rose Date: Fri, 24 Jan 2014 20:57:50 +0000 Subject: [PATCH] res_config_pgsql: Fix a memory leak and use RAII_VAR for cleanup when practical Review: https://reviewboard.asterisk.org/r/3141/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@406360 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- res/res_config_pgsql.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 5a5534414e..98745ac6a9 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -132,7 +132,7 @@ static struct tables *find_table(const char *database, const char *orig_tablenam struct tables *table; struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330); char *pgerror; - PGresult *result; + RAII_VAR(PGresult *, result, NULL, PQclear); char *fname, *ftype, *flen, *fnotnull, *fdef; int i, rows; @@ -222,7 +222,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam if (PQresultStatus(result) != PGRES_TUPLES_OK) { pgerror = PQresultErrorMessage(result); ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror); - PQclear(result); AST_LIST_UNLOCK(&psql_tables); ast_mutex_unlock(&pgsql_lock); return NULL; @@ -230,7 +229,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam 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); ast_mutex_unlock(&pgsql_lock); return NULL; @@ -250,7 +248,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))) { 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); ast_mutex_unlock(&pgsql_lock); @@ -281,7 +278,6 @@ static struct tables *find_table(const char *database, const char *orig_tablenam } AST_LIST_INSERT_TAIL(&table->columns, column, list); } - PQclear(result); AST_LIST_INSERT_TAIL(&psql_tables, table, list); ast_rwlock_rdlock(&table->lock); @@ -307,7 +303,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) { - 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); @@ -380,7 +376,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab "PostgreSQL RealTime: Failed to query '%s@%s'. Check debug for more info.\n", tablename, database); ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql)); ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn)); - PQclear(result); ast_mutex_unlock(&pgsql_lock); return NULL; } else { @@ -409,7 +404,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab 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; } @@ -438,7 +432,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); } - PQclear(result); ast_mutex_unlock(&pgsql_lock); return var; @@ -446,7 +439,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) { - 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); @@ -556,7 +549,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 Failed because: %s (%s)\n", PQresultErrorMessage(result), PQresStatus(result_status)); - PQclear(result); ast_mutex_unlock(&pgsql_lock); ast_config_destroy(cfg); return NULL; @@ -574,7 +566,6 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char 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; @@ -606,7 +597,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); } - PQclear(result); ast_mutex_unlock(&pgsql_lock); return cfg; @@ -615,7 +605,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, const char *lookup, va_list ap) { - PGresult *result = NULL; + RAII_VAR(PGresult *, result, NULL, PQclear); int numrows = 0, pgresult; const char *newparam, *newval; struct ast_str *sql = ast_str_thread_get(&sql_buf, 100); @@ -730,7 +720,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 Failed because: %s (%s)\n", PQresultErrorMessage(result), PQresStatus(result_status)); - PQclear(result); ast_mutex_unlock(&pgsql_lock); return -1; } @@ -755,7 +744,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) { - 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 char *newparam, *newval; @@ -893,7 +882,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) { - 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); @@ -972,7 +961,6 @@ static int store_pgsql(const char *database, const char *table, va_list ap) } 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); @@ -991,7 +979,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) { - 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); @@ -1090,7 +1078,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table, 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; @@ -1158,7 +1146,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table, 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; } @@ -1181,7 +1168,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table, "PostgreSQL RealTime: Could not find config '%s' in database.\n", file); } - PQclear(result); ast_mutex_unlock(&pgsql_lock); return cfg; -- 2.47.3