]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_kazoo] Coverity fixes (#2043)
authorJakub Karolczyk <jakub.karolczyk@signalwire.com>
Mon, 17 Apr 2023 10:53:26 +0000 (11:53 +0100)
committerGitHub <noreply@github.com>
Mon, 17 Apr 2023 10:53:26 +0000 (13:53 +0300)
* [mod_kazoo] Coverity CID 1395503 (Resource leak)

* [mod_kazoo] Coverity CID 1468146 (Resource leak)

* [mod_kazoo] Coverity CID 1468483 (Resource leak)

src/mod/event_handlers/mod_kazoo/kazoo_ei_config.c
src/mod/event_handlers/mod_kazoo/kazoo_message.c
src/mod/event_handlers/mod_kazoo/kazoo_node.c

index f1726b677be5c209a683dc1a9a93e11e092daa09..3c626c15da0a22f200307ebb2ad1a66a7f45041d 100644 (file)
@@ -564,13 +564,16 @@ switch_status_t kazoo_config_fetch_handler(kazoo_config_ptr definitions, kazoo_c
        switch_memory_pool_t *pool = NULL;
 
        char *name = (char *) switch_xml_attr_soft(cfg, "name");
+
        if (zstr(name)) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "missing name in profile\n");
+
                return SWITCH_STATUS_GENERR;
        }
 
        if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error allocation pool for new profile : %s\n", name);
+
                return SWITCH_STATUS_GENERR;
        }
 
@@ -582,6 +585,7 @@ switch_status_t kazoo_config_fetch_handler(kazoo_config_ptr definitions, kazoo_c
        fetch_section = switch_xml_parse_section_string(name);
 
        if ((params = switch_xml_child(cfg, "params")) != NULL) {
+
                for (param = switch_xml_child(params, "param"); param; param = param->next) {
                        char *var = (char *) switch_xml_attr_soft(param, "name");
                        char *val = (char *) switch_xml_attr_soft(param, "value");
@@ -605,7 +609,9 @@ switch_status_t kazoo_config_fetch_handler(kazoo_config_ptr definitions, kazoo_c
        }
 
        if (fetch_section == SWITCH_XML_SECTION_RESULT) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Fetch Profile[%s] invalid fetch-section: %s\n", name, switch_xml_toxml(cfg, SWITCH_FALSE));
+               char *tmp = switch_xml_toxml(cfg, SWITCH_FALSE);
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Fetch Profile[%s] invalid fetch-section: %s\n", name, tmp);
+               free(tmp);
                goto err;
        }
 
@@ -622,17 +628,20 @@ switch_status_t kazoo_config_fetch_handler(kazoo_config_ptr definitions, kazoo_c
                }
        }
 
-       if(ptr)
+       if (ptr) {
                *ptr = profile;
+       }
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "fetch handler profile %s successfully configured\n", name);
+
        return SWITCH_STATUS_SUCCESS;
 
  err:
        /* Cleanup */
-    if(pool) {
-       switch_core_destroy_memory_pool(&pool);
-    }
+       if(pool) {
+               switch_core_destroy_memory_pool(&pool);
+       }
+
        return SWITCH_STATUS_GENERR;
 
 }
index e43d656b4ceb0fd2e10eacaf5172c8c1b5540f9a..43716a8e027f7a4a73238c6184cd347b7ccbb137 100644 (file)
@@ -233,9 +233,11 @@ cJSON * kazoo_event_add_field_to_json(cJSON *dst, switch_event_t *src, kazoo_fie
                        } else if((header = switch_event_get_header_ptr(src, field->name)) != NULL) {
                                if (header->idx) {
                                        item = cJSON_CreateArray();
+
                                        for(i = 0; i < header->idx; i++) {
                                                cJSON_AddItemToArray(item, kazoo_event_json_value(field->out_type, header->array[i]));
                                        }
+
                                        kazoo_cJSON_AddItemToObject(dst, field->as ? field->as : field->name, item);
                                } else if (field->out_type_as_array) {
                                        item = cJSON_CreateArray();
@@ -251,13 +253,16 @@ cJSON * kazoo_event_add_field_to_json(cJSON *dst, switch_event_t *src, kazoo_fie
                        expanded = kz_event_expand_headers(src, field->value);
                        if(expanded != NULL && !zstr(expanded)) {
                                item = kazoo_event_add_json_value(dst, field, field->as ? field->as : field->name, expanded);
-                               if(expanded != field->value) {
-                                       free(expanded);
-                               }
                        }
+
+                       if (expanded != field->value) {
+                               switch_safe_free(expanded);
+                       }
+
                        break;
 
                case FIELD_FIRST_OF:
+
                        for(n = 0; n < field->list.size; n++) {
                                if(*field->list.value[n] == '#') {
                                        item = kazoo_event_add_json_value(dst, field, field->as ? field->as : field->name, ++field->list.value[n]);
@@ -267,33 +272,41 @@ cJSON * kazoo_event_add_field_to_json(cJSON *dst, switch_event_t *src, kazoo_fie
                                        if(header) {
                                                if (header->idx) {
                                                        item = cJSON_CreateArray();
+
                                                        for(i = 0; i < header->idx; i++) {
                                                                cJSON_AddItemToArray(item, kazoo_event_json_value(field->out_type, header->array[i]));
                                                        }
+
                                                        kazoo_cJSON_AddItemToObject(dst, field->as ? field->as : field->name, item);
                                                } else {
                                                        item = kazoo_event_add_json_value(dst, field, field->as ? field->as : field->name, header->value);
                                                }
+
                                                break;
                                        }
                                }
                        }
+
                        break;
 
                case FIELD_PREFIX:
+
                        for (header = src->headers; header; header = header->next) {
                                if(!strncmp(header->name, field->name, strlen(field->name))) {
                                        if (header->idx) {
                                                cJSON *array = cJSON_CreateArray();
+
                                                for(i = 0; i < header->idx; i++) {
                                                        cJSON_AddItemToArray(array, kazoo_event_json_value(field->out_type, header->array[i]));
                                                }
+
                                                kazoo_cJSON_AddItemToObject(dst, field->exclude_prefix ? header->name+strlen(field->name) : header->name, array);
                                        } else {
                                                kazoo_event_add_json_value(dst, field, field->exclude_prefix ? header->name+strlen(field->name) : header->name, header->value);
                                        }
                                }
                        }
+
                        break;
 
                case FIELD_STATIC:
@@ -308,7 +321,7 @@ cJSON * kazoo_event_add_field_to_json(cJSON *dst, switch_event_t *src, kazoo_fie
                        break;
        }
 
-    return item;
+       return item;
 }
 
 static switch_status_t kazoo_event_add_fields_to_json(kazoo_logging_ptr logging, cJSON *dst, switch_event_t *src, kazoo_field_ptr field) {
index feb8970a86411ebee9c9d1d193e0b5196c173d8b..f75c3270234b4e310819ca72e56adc47c2ff7059 100644 (file)
@@ -1097,23 +1097,27 @@ static switch_status_t handle_request_fetch_reply(ei_node_t *ei_node, erlang_pid
        if (ei_decode_atom_safe(buf->buff, &buf->index, section_str)
                || !(section = switch_xml_parse_section_string(section_str))) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Ignoring a fetch reply without a configuration section\n");
+
                return erlang_response_badarg(rbuf);
        }
 
        if (ei_decode_string_or_binary_limited(buf->buff, &buf->index, sizeof(uuid_str), uuid_str)
                || zstr_buf(uuid_str)) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Ignoring a fetch reply without request UUID\n");
+
                return erlang_response_badarg(rbuf);
        }
 
        if (ei_decode_string_or_binary(buf->buff, &buf->index, &xml_str)) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Ignoring a fetch reply without XML : %s \n", uuid_str);
+
                return erlang_response_badarg(rbuf);
        }
 
        if (zstr(xml_str)) {
                switch_safe_free(xml_str);
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Ignoring an empty fetch reply : %s\n", uuid_str);
+
                return erlang_response_badarg(rbuf);
        }
 
@@ -1138,13 +1142,19 @@ static switch_status_t handle_request_fetch_reply(ei_node_t *ei_node, erlang_pid
                break;
        default:
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received fetch reply %s for an unknown configuration section: %s : %s\n", uuid_str, section_str, xml_str);
+               switch_safe_free(xml_str);
+
                return erlang_response_badarg(rbuf);
        }
 
        if (result == SWITCH_STATUS_SUCCESS) {
+               switch_safe_free(xml_str);
+
                return erlang_response_ok(rbuf);
        } else {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received fetch reply %s is unknown or has expired : %s\n", uuid_str, xml_str);
+               switch_safe_free(xml_str);
+
                return erlang_response_baduuid(rbuf);
        }
 }