Fix misuses of asprintf throughout the code.

This fixes three main issues

* Change asprintf() uses to ast_asprintf() so that it
pairs properly with ast_free() and no longer causes
MALLOC_DEBUG to freak out.

* When ast_asprintf() fails, set the pointer NULL if
it will be referenced later.

* Fix some memory leaks that were spotted while taking
care of the first two points.

(Closes issue ASTERISK-20135)
reported by Richard Mudgett

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

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

Merged revisions 371591 from http://svn.asterisk.org/svn/asterisk/branches/10
........

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


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@371593 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Michelson
2012-08-21 21:01:11 +00:00
parent 89a5ff859d
commit 6a539ace84
14 changed files with 61 additions and 55 deletions

View File

@@ -668,13 +668,13 @@ static struct sqlite_cache_tables *find_table(const char *tablename)
}
/* Table structure not cached; build the structure now */
if (asprintf(&sql, sql_table_structure, tablename) < 0) {
ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
if (ast_asprintf(&sql, sql_table_structure, tablename) < 0) {
sql = NULL;
}
if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
AST_RWLIST_UNLOCK(&sqlite_tables);
ast_log(LOG_ERROR, "Memory error. Cannot cache table '%s'\n", tablename);
ast_free(sql);
return NULL;
}
tblptr->name = (char *)tblptr + sizeof(*tblptr);
@@ -690,9 +690,11 @@ static struct sqlite_cache_tables *find_table(const char *tablename)
ast_free(errstr);
free_table(tblptr);
AST_RWLIST_UNLOCK(&sqlite_tables);
ast_free(sql);
return NULL;
}
ast_mutex_unlock(&mutex);
ast_free(sql);
if (AST_LIST_EMPTY(&(tblptr->columns))) {
free_table(tblptr);