]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Consistent memory allocation by ast_bt_get_symbols.
authorWalter Doekes <walter+asterisk@wjd.nu>
Thu, 8 Aug 2013 20:21:52 +0000 (20:21 +0000)
committerWalter Doekes <walter+asterisk@wjd.nu>
Thu, 8 Aug 2013 20:21:52 +0000 (20:21 +0000)
Always use ast_alloc/ast_free. This is handled differently in trunk (r391012).

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

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

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@396441 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/logger.h
main/astobj2.c
main/logger.c
main/utils.c

index 1632291df48dce1c27aeaad62e1048ab69d24942..fc70a815ac3cef62fb8d51de7e07f7b4d3168292 100644 (file)
@@ -411,7 +411,7 @@ void *ast_bt_destroy(struct ast_bt *bt);
  * \param addresses A list of addresses, such as the ->addresses structure element of struct ast_bt.
  * \param num_frames Number of addresses in the addresses list
  * \retval NULL Unable to allocate memory
- * \return List of strings
+ * \return List of strings. Free the entire list with a single ast_free call.
  * \since 1.6.2.16
  */
 char **ast_bt_get_symbols(void **addresses, size_t num_frames);
index 745f1c941fad68dd5a8e2b49111a401a0b544469..1ab5d3e1c8f484af86cd5334fb9ad78b789b875f 100644 (file)
@@ -123,7 +123,7 @@ void ao2_bt(void)
        for(i = 0; i < c; i++) {
                ast_verbose("%d: %p %s\n", i, addresses[i], strings[i]);
        }
-       free(strings);
+       ast_free(strings);
 }
 #endif
 
index c06ebd0d503a1f5b56026878fe593360c18b76c5..2011d250c9cfa3bf3ddf67bf1e494b82d1f633e6 100644 (file)
@@ -1733,7 +1733,31 @@ char **ast_bt_get_symbols(void **addresses, size_t num_frames)
                }
        }
 #else /* !defined(BETTER_BACKTRACES) */
-       strings = backtrace_symbols(addresses, num_frames);
+       if ((strings = backtrace_symbols(addresses, num_frames))) {
+               /* Re-do value into ast_alloc'ed memory */
+               char **ast_strings;
+               char *p;
+               unsigned size = num_frames + sizeof(*strings);
+               int i;
+               for (i = 0; i < num_frames; ++i) {
+                       size += strlen(strings[i]) + 1;
+               }
+#undef free
+               if (!(ast_strings = ast_malloc(size))) {
+                       free(strings);
+                       ast_log(LOG_WARNING, "Unable to re-allocate space for backtrace structure\n");
+                       return NULL;
+               }
+               p = (char *) (ast_strings + num_frames);
+               for (i = 0; i < num_frames; ++i) {
+                       unsigned len = strlen(strings[i]) + 1;
+                       ast_strings[i] = p;
+                       memcpy(p, strings[i], len);
+                       p += len;
+               }
+               free(strings);
+               strings = ast_strings;
+       }
 #endif /* defined(BETTER_BACKTRACES) */
        return strings;
 }
@@ -1758,9 +1782,7 @@ void ast_backtrace(void)
                        ast_debug(1, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
                }
 
-               /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
-#undef free
-               free(strings);
+               ast_free(strings);
        } else {
                ast_debug(1, "Could not allocate memory for backtrace\n");
        }
index 2c3537a8fe5eaca3228ac874fd59b904afab4561..e77215b5a9c13eaef2bf7c9907a4d9826c07eaa6 100644 (file)
@@ -869,7 +869,7 @@ static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt
                        ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]);
                }
 
-               free(symbols);
+               ast_free(symbols);
        } else {
                ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n");
        }