Various: bugfixes found via chaos
authorScott Griepentrog <sgriepentrog@digium.com>
Tue, 17 Mar 2015 22:15:42 +0000 (22:15 +0000)
committerScott Griepentrog <sgriepentrog@digium.com>
Tue, 17 Mar 2015 22:15:42 +0000 (22:15 +0000)
Using DEBUG_CHAOS several instances of a null
pointer crash, and one uninitialized variable
were uncovered and fixed.  Also added details
on why Asterisk failed to initialize.

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

Merged revisions 433064 from http://svn.asterisk.org/svn/asterisk/branches/13

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

include/asterisk/config.h
main/asterisk.c
main/codec_builtin.c
main/config.c
main/endpoints.c
main/manager.c
main/utils.c
main/xmldoc.c

index 23c91b0..6489344 100644 (file)
@@ -844,8 +844,11 @@ void ast_category_append(struct ast_config *config, struct ast_category *cat);
  *
  * \details
  * This function is used to apply a base (template) to an existing category
+ *
+ * \retval 0 if succeeded
+ * \retval -1 if the memory allocation failed
  */
-void ast_category_inherit(struct ast_category *existing, const struct ast_category *base);
+int ast_category_inherit(struct ast_category *existing, const struct ast_category *base);
 
 /*!
  * \brief Removes and destroys all variables in a category
index a466120..21883c4 100644 (file)
@@ -4377,34 +4377,34 @@ int main(int argc, char *argv[])
        ast_builtins_init();
 
        if (ast_utils_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_utils_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_tps_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_tps_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_fd_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_fd_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_pbx_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_pbx_init\n%s", term_quit());
                exit(1);
        }
 
 #ifdef TEST_FRAMEWORK
        if (ast_test_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_test_init\n%s", term_quit());
                exit(1);
        }
 #endif
 
        if (ast_translate_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_translate_init\n%s", term_quit());
                exit(1);
        }
 
@@ -4412,27 +4412,27 @@ int main(int argc, char *argv[])
        ast_uuid_init();
 
        if (ast_sorcery_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_sorcery_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_codec_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_codec_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_format_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_format_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_format_cache_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_format_cache_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_codec_builtin_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_codec_builtin_init\n%s", term_quit());
                exit(1);
        }
 
@@ -4444,7 +4444,7 @@ int main(int argc, char *argv[])
        aco_init();
 
        if (ast_bucket_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_bucket_init\n%s", term_quit());
                exit(1);
        }
 
@@ -4486,26 +4486,26 @@ int main(int argc, char *argv[])
        initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool));
 
        if (init_logger()) {            /* Start logging subsystem */
-               printf("%s", term_quit());
+               printf("Failed: init_logger\n%s", term_quit());
                exit(1);
        }
 
        threadstorage_init();
 
        if (ast_rtp_engine_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_rtp_engine_init\n%s", term_quit());
                exit(1);
        }
 
        ast_autoservice_init();
 
        if (ast_timing_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_timing_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_ssl_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_ssl_init\n%s", term_quit());
                exit(1);
        }
 
@@ -4520,87 +4520,87 @@ int main(int argc, char *argv[])
        }
 
        if (astdb_init()) {
-               printf("%s", term_quit());
+               printf("Failed: astdb_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_msg_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_msg_init\n%s", term_quit());
                exit(1);
        }
 
        /* initialize the data retrieval API */
        if (ast_data_init()) {
-               printf ("%s", term_quit());
+               printf ("Failed: ast_data_init\n%s", term_quit());
                exit(1);
        }
 
        ast_channels_init();
 
        if (ast_endpoint_init()) {
-               printf ("%s", term_quit());
+               printf ("Failed: ast_endpoint_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_pickup_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_pickup_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_bridging_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_bridging_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_parking_stasis_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_parking_stasis_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_device_state_engine_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_device_state_engine_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_presence_state_engine_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_presence_state_engine_init\n%s", term_quit());
                exit(1);
        }
 
        if ((moduleresult = load_modules(1))) {         /* Load modules, pre-load only */
-               printf("%s", term_quit());
+               printf("Failed: load_modules\n%s", term_quit());
                exit(moduleresult == -2 ? 2 : 1);
        }
 
        if (ast_features_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_features_init\n%s", term_quit());
                exit(1);
        }
 
        if (dnsmgr_init()) {            /* Initialize the DNS manager */
-               printf("%s", term_quit());
+               printf("Failed: dnsmgr_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_security_stasis_init()) {               /* Initialize Security Stasis Topic and Events */
-               printf("%s", term_quit());
+               printf("Failed: ast_security_stasis_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_named_acl_init()) { /* Initialize the Named ACL system */
-               printf("%s", term_quit());
+               printf("Failed: ast_named_acl_init\n%s", term_quit());
                exit(1);
        }
 
        ast_http_init();                /* Start the HTTP server, if needed */
 
        if (ast_indications_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_indications_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_cdr_engine_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_cdr_engine_init\n%s", term_quit());
                exit(1);
        }
 
@@ -4608,47 +4608,47 @@ int main(int argc, char *argv[])
        ast_udptl_init();
 
        if (ast_image_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_image_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_file_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_file_init\n%s", term_quit());
                exit(1);
        }
 
        if (load_pbx()) {
-               printf("%s", term_quit());
+               printf("Failed: load_pbx\n%s", term_quit());
                exit(1);
        }
 
        if (ast_local_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_local_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_cel_engine_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_cel_engine_init\n%s", term_quit());
                exit(1);
        }
 
        if (init_manager()) {
-               printf("%s", term_quit());
+               printf("Failed: init_manager\n%s", term_quit());
                exit(1);
        }
 
        if (ast_enum_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_enum_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_cc_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_cc_init\n%s", term_quit());
                exit(1);
        }
 
        if (ast_sounds_index_init()) {
-               printf("%s", term_quit());
+               printf("Failed: ast_sounds_index_init\n%s", term_quit());
                exit(1);
        }
 
index 594074a..2e89e9a 100644 (file)
@@ -776,7 +776,7 @@ static struct ast_codec t140 = {
                struct ast_codec *__codec_ ## __LINE__; \
                res |= __ast_codec_register(&(codec), NULL); \
                __codec_ ## __LINE__ = ast_codec_get((codec).name, (codec).type, (codec).sample_rate); \
-               __fmt_ ## __LINE__ = ast_format_create(__codec_ ## __LINE__); \
+               __fmt_ ## __LINE__ = __codec_ ## __LINE__ ? ast_format_create(__codec_ ## __LINE__) : NULL; \
                res |= ast_format_cache_set(__fmt_ ## __LINE__); \
                ao2_ref(__fmt_ ## __LINE__, -1); \
                ao2_ref(__codec_ ## __LINE__, -1); \
index 7e51413..517b33b 100644 (file)
@@ -1238,23 +1238,27 @@ void ast_category_rename(struct ast_category *cat, const char *name)
        ast_copy_string(cat->name, name, sizeof(cat->name));
 }
 
-void ast_category_inherit(struct ast_category *new, const struct ast_category *base)
+int ast_category_inherit(struct ast_category *new, const struct ast_category *base)
 {
        struct ast_variable *var;
        struct ast_category_template_instance *x;
 
        x = ast_calloc(1, sizeof(*x));
        if (!x) {
-               return;
+               return -1;
        }
        strcpy(x->name, base->name);
        x->inst = base;
        AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
        for (var = base->root; var; var = var->next) {
                struct ast_variable *cloned = variable_clone(var);
+               if (!cloned) {
+                       return -1;
+               }
                cloned->inherited = 1;
                ast_variable_append(new, cloned);
        }
+       return 0;
 }
 
 struct ast_config *ast_config_new(void)
@@ -1691,7 +1695,10 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                                ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
                                                return -1;
                                        }
-                                       ast_category_inherit(*cat, base);
+                                       if (ast_category_inherit(*cat, base)) {
+                                               ast_log(LOG_ERROR, "Inheritence requested, but allocation failed\n");
+                                               return -1;
+                                       }
                                }
                        }
                }
index 3a6f494..66ad461 100644 (file)
@@ -472,7 +472,8 @@ struct ast_endpoint_snapshot *ast_endpoint_snapshot_create(
                endpoint_snapshot_dtor,
                AO2_ALLOC_OPT_LOCK_NOLOCK);
 
-       if (ast_string_field_init(snapshot, 80) != 0) {
+       if (!snapshot || ast_string_field_init(snapshot, 80) != 0) {
+               ao2_cleanup(snapshot);
                return NULL;
        }
 
index aa8a5c3..2bd9c00 100644 (file)
@@ -3628,7 +3628,10 @@ static enum error_type handle_updates(struct mansession *s, const struct message
                        if (inherit) {
                                while ((tmpl_name = ast_strsep(&inherit, ',', AST_STRSEP_STRIP))) {
                                        if ((template = ast_category_get(cfg, tmpl_name, "TEMPLATES=restrict"))) {
-                                               ast_category_inherit(category, template);
+                                               if (ast_category_inherit(category, template)) {
+                                                       result = FAILURE_ALLOCATION;
+                                                       break;
+                                               }
                                        } else {
                                                ast_category_destroy(category);
                                                category = NULL;
index 578a01e..d7bf4d7 100644 (file)
@@ -611,7 +611,7 @@ const char *ast_inet_ntoa(struct in_addr ia)
        return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
 }
 
-static int dev_urandom_fd;
+static int dev_urandom_fd = -1;
 
 #ifndef __linux__
 #undef pthread_create /* For ast_pthread_create function only */
index 2a17e52..395df8e 100644 (file)
@@ -2214,6 +2214,9 @@ static struct ast_str *xmldoc_get_formatted(struct ast_xml_node *node, int raw_o
                ast_xml_free_text(tmpstr);
        } else {
                ret = ast_str_create(128);
+               if (!ret) {
+                       return NULL;
+               }
                for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
                        /* if found, parse children elements. */
                        if (xmldoc_parse_common_elements(tmp, "", "\n", &ret)) {
@@ -2263,7 +2266,7 @@ static char *_xmldoc_build_field(struct ast_xml_node *node, const char *var, int
        }
 
        formatted = xmldoc_get_formatted(node, raw, raw);
-       if (ast_str_strlen(formatted) > 0) {
+       if (formatted && ast_str_strlen(formatted) > 0) {
                ret = ast_strdup(ast_str_buffer(formatted));
        }
        ast_free(formatted);