diff -urN asterisk_clean-app_sql_postgres/apps/app_sql_postgres.c asterisk_wrappers-app_sql_postgres/apps/app_sql_postgres.c --- asterisk_clean-app_sql_postgres/apps/app_sql_postgres.c 2006-01-17 13:42:48.000000000 -0500 +++ asterisk_wrappers-app_sql_postgres/apps/app_sql_postgres.c 2006-01-17 13:36:39.000000000 -0500 @@ -44,6 +44,7 @@ #include "asterisk/linkedlists.h" #include "asterisk/chanvars.h" #include "asterisk/lock.h" +#include "asterisk/utils.h" #include "libpq-fe.h" @@ -122,13 +123,15 @@ LOCAL_USER_DECL; -#define AST_PGSQL_ID_DUMMY 0 -#define AST_PGSQL_ID_CONNID 1 -#define AST_PGSQL_ID_RESID 2 -#define AST_PGSQL_ID_FETCHID 3 +enum id_type { + AST_PGSQL_ID_DUMMY = 0, + AST_PGSQL_ID_CONNID = 1, + AST_PGSQL_ID_RESID = 2, + AST_PGSQL_ID_FETCHID = 3 +}; struct ast_PGSQL_id { - int identifier_type; /* 0 = dummy, 1 = connid, 2 = resultid */ + enum id_type identifier_type; /* 0 = dummy, 1 = connid, 2 = resultid, 3 = fetchid */ int identifier; void *data; AST_LIST_ENTRY(ast_PGSQL_id) entries; @@ -136,7 +139,7 @@ AST_LIST_HEAD(PGSQLidshead, ast_PGSQL_id) PGSQLidshead; -static void *find_identifier(int identifier, int identifier_type) +static void *find_identifier(const int identifier, const enum id_type identifier_type) { struct PGSQLidshead *headp; struct ast_PGSQL_id *i; @@ -164,7 +167,7 @@ return res; } -static int add_identifier(int identifier_type, void *data) +static int add_identifier(const enum id_type identifier_type, void *data) { struct ast_PGSQL_id *i, *j; struct PGSQLidshead *headp; @@ -178,7 +181,9 @@ ast_log(LOG_WARNING, "Unable to lock identifiers list\n"); return -1; } else { - i = malloc(sizeof(struct ast_PGSQL_id)); + if (!(i = ast_malloc(sizeof(*i)))) { + /* TODO: Handle memory allocation failure */ + } AST_LIST_TRAVERSE(headp, j, entries) { if (j->identifier > maxidentifier) { maxidentifier = j->identifier; @@ -195,7 +200,7 @@ return i->identifier; } -static int del_identifier(int identifier, int identifier_type) +static int del_identifier(const int identifier, const enum id_type identifier_type) { struct ast_PGSQL_id *i; struct PGSQLidshead *headp; @@ -239,7 +244,9 @@ res = 0; l = strlen(data) + 2; - s1 = malloc(l); + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ @@ -258,12 +265,13 @@ } free(s1); + return res; } static int aPGSQL_query(struct ast_channel *chan, void *data) { - char *s1, *s2, *s3, *s4; + char *s1, *s2, *s3; char s[100] = ""; char *querystring; char *var; @@ -276,16 +284,17 @@ res = 0; l = strlen(data) + 2; - s1 = malloc(l); - s2 = malloc(l); + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ - s3 = strsep(&stringp, " "); + s2 = strsep(&stringp, " "); while (1) { /* ugly trick to make branches with break; */ - var = s3; - s4 = strsep(&stringp, " "); - id = atoi(s4); + var = s2; + s3 = strsep(&stringp, " "); + id = atoi(s3); querystring = strsep(&stringp, "\n"); if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aPGSQL_query\n", id); @@ -312,14 +321,14 @@ } free(s1); - free(s2); return res; } static int aPGSQL_fetch(struct ast_channel *chan, void *data) { - char *s1, *s2, *fetchid_var, *s4, *s5, *s6, *s7; + char *s1, *s2, *s3, *s4, *fetchid_var; + const char *s5; char s[100]; char *var; int l; @@ -336,9 +345,10 @@ res = 0; l = strlen(data) + 2; - s7 = NULL; - s1 = malloc(l); - s2 = malloc(l); + s5 = NULL; + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ @@ -349,25 +359,25 @@ AST_LIST_TRAVERSE(headp, variables, entries) { if (!(strncasecmp(ast_var_name(variables), fetchid_var, strlen(fetchid_var)))) { - s7 = ast_var_value(variables); + s5 = ast_var_value(variables); fnd = 1; break; } } if (!fnd) { - s7 = "0"; - pbx_builtin_setvar_helper(chan, fetchid_var, s7); + s5 = "0"; + pbx_builtin_setvar_helper(chan, fetchid_var, s5); } - s4 = strsep(&stringp, " "); - id = atoi(s4); /* resultid */ + s2 = strsep(&stringp, " "); + id = atoi(s2); /* resultid */ if (!(PGSQLres = find_identifier(id, AST_PGSQL_ID_RESID))) { ast_log(LOG_WARNING, "Invalid result identifier %d passed in aPGSQL_fetch\n", id); res = -1; break; } - id = atoi(s7); /*fetchid */ + id = atoi(s5); /*fetchid */ if (!(identp = find_identifier(id, AST_PGSQL_ID_FETCHID))) { i = 0; /* fetching the very first row */ } else { @@ -380,18 +390,20 @@ nres = PQnfields(PGSQLres); ast_log(LOG_WARNING, "ast_PGSQL_fetch : nres = %d i = %d ;\n", nres, i); for (j = 0; j < nres; j++) { - if (!(s5 = strsep(&stringp, " "))) { + if (!(s3 = strsep(&stringp, " "))) { ast_log(LOG_WARNING, "ast_PGSQL_fetch : More tuples (%d) than variables (%d)\n", nres, j); break; } - if (!(s6 = PQgetvalue(PGSQLres, i, j))) { + if (!(s4 = PQgetvalue(PGSQLres, i, j))) { ast_log(LOG_WARNING, "PQgetvalue(res, %d, %d) returned NULL in ast_PGSQL_fetch\n", i, j); break; } - ast_log(LOG_WARNING, "===setting variable '%s' to '%s'\n", s5, s6); - pbx_builtin_setvar_helper(chan, s5, s6); + ast_log(LOG_WARNING, "===setting variable '%s' to '%s'\n", s3, s4); + pbx_builtin_setvar_helper(chan, s3, s4); + } + if (!(identp = ast_malloc(sizeof(int)))) { + /* TODO: Handle memory allocation failure */ } - identp = malloc(sizeof(int)); *identp = ++i; /* advance to the next row */ id1 = add_identifier(AST_PGSQL_ID_FETCHID, identp); } else { @@ -405,26 +417,27 @@ } free(s1); - free(s2); return res; } static int aPGSQL_reset(struct ast_channel *chan, void *data) { - char *s1, *s3; + char *s1, *s2; int l; PGconn *PGSQLconn; int id; char *stringp = NULL; l = strlen(data) + 2; - s1 = malloc(l); + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ - s3 = strsep(&stringp, " "); - id = atoi(s3); + s2 = strsep(&stringp, " "); + id = atoi(s2); if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aPGSQL_reset\n", id); } else { @@ -438,19 +451,21 @@ static int aPGSQL_clear(struct ast_channel *chan, void *data) { - char *s1, *s3; + char *s1, *s2; int l; PGresult *PGSQLres; int id; char *stringp = NULL; l = strlen(data) + 2; - s1 = malloc(l); + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ - s3 = strsep(&stringp, " "); - id = atoi(s3); + s2 = strsep(&stringp, " "); + id = atoi(s2); if (!(PGSQLres = find_identifier(id, AST_PGSQL_ID_RESID))) { ast_log(LOG_WARNING, "Invalid result identifier %d passed in aPGSQL_clear\n", id); } else { @@ -465,19 +480,21 @@ static int aPGSQL_disconnect(struct ast_channel *chan, void *data) { - char *s1, *s3; + char *s1, *s2; int l; PGconn *PGSQLconn; int id; char *stringp = NULL; l = strlen(data) + 2; - s1 = malloc(l); + if (!(s1 = ast_malloc(l))) { + /* TODO: Handle memory allocation failure */ + } strncpy(s1, data, l - 1); stringp = s1; strsep(&stringp, " "); /* eat the first token, we already know it :P */ - s3 = strsep(&stringp, " "); - id = atoi(s3); + s2 = strsep(&stringp, " "); + id = atoi(s2); if (!(PGSQLconn = find_identifier(id, AST_PGSQL_ID_CONNID))) { ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aPGSQL_disconnect\n", id); } else {