From 55f2dc475932fd825bab1f2de1fe6565852f2e03 Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Fri, 21 Jul 2023 11:49:40 +0100 Subject: [PATCH] [core] Coverity fixes * [core] Coverity CID 1024233 (Dereference before null check) * [core] Coverity CID 1024239 (Dereference before null check) * [core] Coverity CID 1024242 (Dereference before null check) * [core] Coverity CID 1024243 (Dereference before null check) * [core] Coverity CID 1024453 (Dereference before null check) * [core] Coverity CID 1024554 (Logically dead code) * [core] Coverity CID 1024868 (unchecked return value from library) * [core] Coverity CID 1024869 (unchecked return value from library) * [core] Coverity CID 1468281 (Dereference before null check) * [core] Coverity CID 1024238 (Dereference before null check) * [core] Coverity CID 1468621 (Copy into fixed size buffer) * [core] Coverity CID 1024871 (Unchecked return value) --- src/switch_core_codec.c | 10 ++++++++-- src/switch_event.c | 20 +++++++++++++++----- src/switch_ivr_originate.c | 15 +++++++++------ src/switch_rtp.c | 18 ------------------ src/switch_xml.c | 36 ++++++++++++++++++++++++++---------- 5 files changed, 58 insertions(+), 41 deletions(-) diff --git a/src/switch_core_codec.c b/src/switch_core_codec.c index 69883f8122..e5c22cd610 100644 --- a/src/switch_core_codec.c +++ b/src/switch_core_codec.c @@ -120,8 +120,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c } } else { /* replace real_read_codec */ switch_codec_t *cur_codec; + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Original read codec replaced with %s:%d\n", - switch_channel_get_name(session->channel), codec->implementation->iananame, codec->implementation->ianacode); + switch_channel_get_name(session->channel), codec->implementation ? codec->implementation->iananame : "undefined", codec->implementation ? codec->implementation->ianacode : -1); /* Set real_read_codec to front of the list of read_codecs */ cur_codec = session->read_codec; while (cur_codec != NULL) { @@ -129,8 +130,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c cur_codec->next = codec; break; } + cur_codec = cur_codec->next; } + session->real_read_codec = codec; session->real_read_impl = *codec->implementation; @@ -154,6 +157,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c session->bug_codec.implementation->iananame, session->bug_codec.implementation->ianacode); switch_core_codec_destroy(&session->bug_codec); } + switch_thread_rwlock_unlock(session->bug_rwlock); } else { status = SWITCH_STATUS_FALSE; @@ -169,6 +173,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c if (session->read_impl.actual_samples_per_second != session->read_impl.samples_per_second) { switch_event_add_header(event, SWITCH_STACK_BOTTOM, "channel-reported-read-codec-rate", "%d", session->read_impl.samples_per_second); } + switch_event_fire(&event); } @@ -191,6 +196,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c } switch_mutex_unlock(session->codec_read_mutex); + return status; } @@ -221,7 +227,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_s goto end; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Push codec %s:%d\n", - switch_channel_get_name(session->channel), codec->implementation->iananame, codec->implementation->ianacode); + switch_channel_get_name(session->channel), codec->implementation ? codec->implementation->iananame : "undefined", codec->implementation ? codec->implementation->ianacode : -1); codec->next = session->read_codec; session->read_codec = codec; if (codec->implementation) { diff --git a/src/switch_event.c b/src/switch_event.c index 272255d31c..02a6f81505 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -2071,15 +2071,18 @@ SWITCH_DECLARE(switch_status_t) switch_event_bind_removable(const char *id, swit switch_mutex_lock(CUSTOM_HASH_MUTEX); if (!(subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name))) { - switch_event_reserve_subclass_detailed(id, subclass_name); - subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name); - subclass->bind = 1; + if (switch_event_reserve_subclass_detailed(id, subclass_name) == SWITCH_STATUS_SUCCESS) { + if ((subclass = switch_core_hash_find(CUSTOM_HASH, subclass_name))) { + subclass->bind = 1; + } + } } switch_mutex_unlock(CUSTOM_HASH_MUTEX); if (!subclass) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not reserve subclass. '%s'\n", subclass_name); + return SWITCH_STATUS_FALSE; } } @@ -2094,6 +2097,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_bind_removable(const char *id, swit if (subclass_name) { event_node->subclass_name = DUP(subclass_name); } + event_node->callback = callback; event_node->user_data = user_data; @@ -2952,14 +2956,17 @@ static void ecd_deliver(event_channel_data_t **ecdP) int x_argc = switch_separate_string_string(key, (char*) sep, x_argv, SWITCH_CHANNEL_DISPATCH_MAX_KEY_PARTS); char buf[1024]; int i, r; + for(i=x_argc - 1; i > 0; i--) { int z; + memset(buf, 0, 1024); - sprintf(buf, "%s", x_argv[0]); + switch_snprintf(buf, sizeof(buf), "%s", x_argv[0]); for(z=1; z < i; z++) { strcat(buf, sep); - strcat(buf, x_argv[z]); + strncat(buf, x_argv[z], sizeof(buf) - strlen(buf) - 1); } + r = _switch_event_channel_broadcast(buf, ecd->event_channel, ecd->json, ecd->key, ecd->id); t += r; if (r && switch_core_test_flag(SCF_EVENT_CHANNEL_HIERARCHY_DELIVERY_ONCE)) { @@ -2968,11 +2975,13 @@ static void ecd_deliver(event_channel_data_t **ecdP) } } else { char *p = NULL; + if ((p = strchr(key, '.'))) { *p = '\0'; t += _switch_event_channel_broadcast(key, ecd->event_channel, ecd->json, ecd->key, ecd->id); } } + switch_safe_free(key); t += _switch_event_channel_broadcast(SWITCH_EVENT_CHANNEL_GLOBAL, ecd->event_channel, ecd->json, ecd->key, ecd->id); @@ -2980,6 +2989,7 @@ static void ecd_deliver(event_channel_data_t **ecdP) if(t == 0) { if (switch_core_test_flag(SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON)) { char *json = cJSON_Print(ecd->json); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no subscribers for %s , %s => %s\n", ecd->event_channel, ecd->key, json); switch_safe_free(json); } else { diff --git a/src/switch_ivr_originate.c b/src/switch_ivr_originate.c index f23549713b..a39553e2f9 100644 --- a/src/switch_ivr_originate.c +++ b/src/switch_ivr_originate.c @@ -186,15 +186,16 @@ struct key_collect { static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t *thread, void *obj) { struct key_collect *collect = (struct key_collect *) obj; - switch_channel_t *channel = switch_core_session_get_channel(collect->session); + switch_channel_t *channel = NULL; char buf[10] = SWITCH_BLANK_STRING; switch_application_interface_t *application_interface = NULL; - if (collect->session) { - if (switch_core_session_read_lock(collect->session) != SWITCH_STATUS_SUCCESS) { - return NULL; - } - } else { + if (!collect->session) { + return NULL; + } + + channel = switch_core_session_get_channel(collect->session); + if (switch_core_session_read_lock(collect->session) != SWITCH_STATUS_SUCCESS) { return NULL; } @@ -232,6 +233,7 @@ static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t *thread, void switch_channel_set_flag(channel, CF_WINNER); switch_channel_set_variable(channel, "group_dial_status", "winner"); } + goto wbreak; } @@ -271,6 +273,7 @@ static void *SWITCH_THREAD_FUNC collect_thread_run(switch_thread_t *thread, void switch_ivr_play_file(collect->session, NULL, collect->error_file, NULL); } } + wbreak: switch_core_session_rwunlock(collect->session); diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 4aa68061ab..db6f63e2dd 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -3934,12 +3934,6 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess case SWITCH_RTP_CRYPTO_RECV: switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key); break; - case SWITCH_RTP_CRYPTO_SEND_RTCP: - switch_channel_set_variable(channel, "srtcp_local_crypto_key", (const char *)b64_key); - break; - case SWITCH_RTP_CRYPTO_RECV_RTCP: - switch_channel_set_variable(channel, "srtcp_remote_crypto_key", (const char *)b64_key); - break; default: break; } @@ -3952,12 +3946,6 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess case SWITCH_RTP_CRYPTO_RECV: switch_channel_set_variable(channel, "srtp_remote_video_crypto_key", (const char *)b64_key); break; - case SWITCH_RTP_CRYPTO_SEND_RTCP: - switch_channel_set_variable(channel, "srtcp_local_video_crypto_key", (const char *)b64_key); - break; - case SWITCH_RTP_CRYPTO_RECV_RTCP: - switch_channel_set_variable(channel, "srtcp_remote_video_crypto_key", (const char *)b64_key); - break; default: break; } @@ -3970,12 +3958,6 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess case SWITCH_RTP_CRYPTO_RECV: switch_channel_set_variable(channel, "srtp_remote_audio_crypto_key", (const char *)b64_key); break; - case SWITCH_RTP_CRYPTO_SEND_RTCP: - switch_channel_set_variable(channel, "srtcp_local_audio_crypto_key", (const char *)b64_key); - break; - case SWITCH_RTP_CRYPTO_RECV_RTCP: - switch_channel_set_variable(channel, "srtcp_remote_audio_crypto_key", (const char *)b64_key); - break; default: break; } diff --git a/src/switch_xml.c b/src/switch_xml.c index 80b7553907..8ed7c27658 100644 --- a/src/switch_xml.c +++ b/src/switch_xml.c @@ -550,15 +550,22 @@ SWITCH_DECLARE(const char **) switch_xml_pi(switch_xml_t xml, const char *target switch_xml_root_t root = (switch_xml_root_t) xml; int i = 0; - if (!root) + if (!root) { return (const char **) SWITCH_XML_NIL; - while (root->xml.parent) + } + + while (root && root->xml.parent) { root = (switch_xml_root_t) root->xml.parent; /* root tag */ + } + if (!root || !root->pi) { return (const char **) SWITCH_XML_NIL; } - while (root->pi[i] && strcmp(target, root->pi[i][0])) + + while (root->pi[i] && strcmp(target, root->pi[i][0])) { i++; /* find target */ + } + return (const char **) ((root->pi[i]) ? root->pi[i] + 1 : SWITCH_XML_NIL); } @@ -1146,7 +1153,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_str(char *s, switch_size_t len) return switch_xml_err(root, d, "unclosed