Fix ODBC to clear title each time (bug #3379)
authorMark Spencer <markster@digium.com>
Thu, 20 Jan 2005 04:53:17 +0000 (04:53 +0000)
committerMark Spencer <markster@digium.com>
Thu, 20 Jan 2005 04:53:17 +0000 (04:53 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4851 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/utils.h
res/res_config_odbc.c

index dd11419..0669c08 100755 (executable)
@@ -134,6 +134,24 @@ extern int test_for_thread_safety(void);
 extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
 extern int ast_utils_init(void);
 
+/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
+
+struct ast_realloca {
+       char *ptr;
+       int alloclen;
+};
+
+#define ast_restrdupa(ra, s) \
+       ({ \
+               if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \
+                       strcpy((ra)->ptr, s); \
+               } else { \
+                       (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \
+                       if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \
+               } \
+               (ra)->ptr; \
+       })
+
 #ifdef inet_ntoa
 #undef inet_ntoa
 #endif
index 2a27c06..f8813de 100755 (executable)
@@ -186,19 +186,21 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
        struct ast_variable *var=NULL, *prev=NULL;
        struct ast_config *cfg=NULL;
        struct ast_category *cat=NULL;
+       struct ast_realloca ra;
        SQLLEN rowcount=0;
        SQLULEN colsize;
        SQLSMALLINT colcount=0;
        SQLSMALLINT datatype;
        SQLSMALLINT decimaldigits;
        SQLSMALLINT nullable;
+
        va_list aq;
-       
        va_copy(aq, ap);
        
        
        if (!table)
                return NULL;
+       memset(&ra, 0, sizeof(ra));
 
        obj = fetch_odbc_obj(database);
        if (!obj)
@@ -269,6 +271,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
        while (rowcount--) {
                var = NULL;
                prev = NULL;
+               title = NULL;
                res = SQLFetch(stmt);
                if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
                        ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
@@ -297,7 +300,7 @@ static struct ast_config *realtime_multi_odbc(const char *database, const char *
                                chunk = strsep(&stringp, ";");
                                if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
                                        if (initfield && !strcmp(initfield, coltitle) && !title)
-                                               title = ast_strdupa(chunk);
+                                               title = ast_restrdupa(&ra, chunk);
                                        if (prev) {
                                                prev->next = ast_new_variable(coltitle, chunk);
                                                if (prev->next)