Skip to content

Commit

Permalink
[core] Coverity fixes
Browse files Browse the repository at this point in the history
* [core] Coverity 1518099, 1518097, 1518098 (Unchecked return value from library)

* [core] Coverity 1468551 Unchecked return value

* [core] Coverity 1468293 Unchecked return value

* [core] Coverity 1468274 Explicit null dereferenced

* [core] Coverity 1395588 Unchecked return value

* [core] Coverity 1395515 Logically dead code

* [core] Coverity 1364984 Result is not floating-point

* [core] Coverity 1395554, 1468440 Dereference before null check

* [core] Coverity 1024487 Dereference after null check

* [core] Coverity 1024872 Unchecked return value

* [core] Coverity 1025822 Unchecked return value

* [core] Coverity 1025823 Unchecked return value

* [core] Coverity 1087637, 1346467, 1087638 Unchecked return value

* [core] Coverity 1107607 Unchecked return value

* [core] Coverity 1210777 Unchecked return value

* [core] Coverity 1227670 Dereference before null check

* [core] Coverity 1024551 Logically dead code

* [core] Coverity 1024560 Logically dead code

* [core] Coverity 1024664 Operands don't affect result

* [core] Coverity 1364957 Dereference after null check

* [core] Coverity 1395572 Logically dead code

* [core] Coverity 1412459 Unchecked return value

* [core] Coverity 1412490 Unchecked return value

* [core] Coverity 1395515/2 Logically dead code

* [core] Coverity cleanup
jakubkarolczyk authored Sep 8, 2023
1 parent b6ccc27 commit bb9afcb
Showing 14 changed files with 142 additions and 80 deletions.
44 changes: 22 additions & 22 deletions src/cJSON.c
Original file line number Diff line number Diff line change
@@ -1104,34 +1104,32 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
buffer->length = default_buffer_size;
buffer->format = format;
buffer->hooks = *hooks;
if (buffer->buffer == NULL)
{

if (buffer->buffer == NULL) {
goto fail;
}

/* print the value */
if (!print_value(item, buffer))
{
if (!print_value(item, buffer)) {
goto fail;
}

update_offset(buffer);

/* check if reallocate is available */
if (hooks->reallocate != NULL)
{
if (hooks->reallocate != NULL) {
printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
if (printed == NULL) {
goto fail;
}

buffer->buffer = NULL;
}
else /* otherwise copy the JSON over to a new buffer */
{
} else { /* otherwise copy the JSON over to a new buffer */
printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
if (printed == NULL)
{
if (printed == NULL) {
goto fail;
}

memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
printed[buffer->offset] = '\0'; /* just to be sure */

@@ -1142,16 +1140,10 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
return printed;

fail:
if (buffer->buffer != NULL)
{
if (buffer->buffer != NULL) {
hooks->deallocate(buffer->buffer);
}

if (printed != NULL)
{
hooks->deallocate(printed);
}

return NULL;
}

@@ -1942,33 +1934,41 @@ static cJSON_bool add_item_to_object(cJSON * const object, const char * const st

CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, false);
cJSON_bool res = add_item_to_object(object, string, item, &global_hooks, false);
(void)res;
}

/* Add an item to an object with constant string as key */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{
add_item_to_object(object, string, item, &global_hooks, true);
cJSON_bool res = add_item_to_object(object, string, item, &global_hooks, true);
(void)res;
}

CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
{
cJSON_bool res;

if (array == NULL)
{
return;
}

add_item_to_array(array, create_reference(item, &global_hooks));
res = add_item_to_array(array, create_reference(item, &global_hooks));
(void)res;
}

CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
{
cJSON_bool res;

if ((object == NULL) || (string == NULL))
{
return;
}

add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
res = add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
(void)res;
}

CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
43 changes: 26 additions & 17 deletions src/switch_core_media.c
Original file line number Diff line number Diff line change
@@ -722,13 +722,10 @@ SWITCH_DECLARE(payload_map_t *) switch_core_media_add_payload_map(switch_core_se
exists = (type == pmap->type && !strcasecmp(name, pmap->iananame) && pmap->pt == pt && (!pmap->rate || rate == pmap->rate) && (!pmap->ptime || pmap->ptime == ptime));
break;
case SWITCH_MEDIA_TYPE_VIDEO:
if (sdp_type == SDP_TYPE_RESPONSE) {
exists = (pmap->sdp_type == SDP_TYPE_REQUEST && type == pmap->type && !strcasecmp(name, pmap->iananame));
} else {
exists = (type == pmap->type && !strcasecmp(name, pmap->iananame));
}
exists = (pmap->sdp_type == SDP_TYPE_REQUEST && type == pmap->type && !strcasecmp(name, pmap->iananame));

switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "CHECK PMAP %s:%s %d %s:%s %d ... %d\n",
name, sdp_type == SDP_TYPE_REQUEST ? "REQ" : "RES", pt,
name, "RES", pt,
pmap->iananame, pmap->sdp_type == SDP_TYPE_REQUEST ? "REQ" : "RES", pmap->pt, exists);


@@ -2524,7 +2521,7 @@ static void check_jb_sync(switch_core_session_t *session)
}

if (!jb_sync_msec && frames) {
jb_sync_msec = (double)(1000 / fps) * frames;
jb_sync_msec = ((double)1000 / fps) * frames;
}

switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session),
@@ -4624,9 +4621,10 @@ static void check_stream_changes(switch_core_session_t *session, const char *r_s
{
switch_core_session_t *other_session = NULL;
switch_core_session_message_t *msg;
switch_status_t status = SWITCH_STATUS_SUCCESS;

switch_core_session_get_partner(session, &other_session);

status = switch_core_session_get_partner(session, &other_session);
(void)status;

if (switch_channel_test_flag(session->channel, CF_STREAM_CHANGED)) {
switch_channel_clear_flag(session->channel, CF_STREAM_CHANGED);
@@ -4662,13 +4660,15 @@ static void check_stream_changes(switch_core_session_t *session, const char *r_s
if (switch_channel_test_flag(other_session->channel, CF_AWAITING_STREAM_CHANGE)) {
uint8_t proceed = 1;
const char *sdp_in, *other_ep;
uint8_t res = 0;

if ((other_ep = switch_channel_get_variable(session->channel, "ep_codec_string"))) {
switch_channel_set_variable(other_session->channel, "codec_string", other_ep);
}

sdp_in = switch_channel_get_variable(other_session->channel, SWITCH_R_SDP_VARIABLE);
switch_core_media_negotiate_sdp(other_session, sdp_in, &proceed, SDP_TYPE_REQUEST);
res = switch_core_media_negotiate_sdp(other_session, sdp_in, &proceed, SDP_TYPE_REQUEST);
(void)res;
switch_core_media_activate_rtp(other_session);
msg = switch_core_session_alloc(other_session, sizeof(*msg));
msg->message_id = SWITCH_MESSAGE_INDICATE_RESPOND;
@@ -13587,6 +13587,7 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
if (zstr(attr->a_name)) {
continue;
}

if (!strcasecmp(attr->a_name, "ptime")) {
dptime = atoi(attr->a_value);
break;
@@ -13599,22 +13600,27 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
if ((m->m_type == sdp_media_audio || m->m_type == sdp_media_video) && m->m_port) {
for (map = m->m_rtpmaps; map; map = map->rm_next) {
int found = 0;

for (attr = m->m_attributes; attr && found < 2; attr = attr->a_next) {
if (zstr(attr->a_name)) {
continue;
}

if (!strcasecmp(attr->a_name, "ptime") && attr->a_value) {
ptime = atoi(attr->a_value);
found++;
}

if (!strcasecmp(attr->a_name, "rtcp-mux")) {
if (switch_channel_var_true(channel, "rtcp_mux_auto_detect")) {
switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "setting rtcp-mux from sdp\n");
switch_channel_set_variable(channel, "rtcp_mux", "true");
}

found++;
}
}

switch_core_media_add_payload_map(session,
m->m_type == sdp_media_audio ? SWITCH_MEDIA_TYPE_AUDIO : SWITCH_MEDIA_TYPE_VIDEO,
map->rm_encoding,
@@ -13640,11 +13646,13 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
if (zstr(attr->a_name)) {
continue;
}

if (!strcasecmp(attr->a_name, "ptime") && attr->a_value) {
ptime = atoi(attr->a_value);
break;
}
}

connection = sdp->sdp_connection;
if (m->m_connections) {
connection = m->m_connections;
@@ -13658,7 +13666,7 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND || prefer_sdp) {
for (map = m->m_rtpmaps; map; map = map->rm_next) {

if (map->rm_pt > 127 || already_did[map->rm_pt]) {
if (already_did[map->rm_pt]) {
continue;
}

@@ -13679,19 +13687,20 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
if (match) {
add_audio_codec(map, imp, ptime, buf, sizeof(buf));
}

}
}

} else {
for (i = 0; i < num_codecs; i++) {
const switch_codec_implementation_t *imp = codecs[i];

if (imp->codec_type != SWITCH_CODEC_TYPE_AUDIO || imp->ianacode > 127 || already_did[imp->ianacode]) {
continue;
}

for (map = m->m_rtpmaps; map; map = map->rm_next) {

if (map->rm_pt > 127 || already_did[map->rm_pt]) {
if (already_did[map->rm_pt]) {
continue;
}

@@ -13724,11 +13733,10 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
break;
}


if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND || prefer_sdp) {
for (map = m->m_rtpmaps; map; map = map->rm_next) {

if (map->rm_pt > 127 || already_did[map->rm_pt]) {
if (already_did[map->rm_pt]) {
continue;
}

@@ -13752,11 +13760,11 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
} else {
switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ",%s.%s", imp->modname, imp->iananame);
}

already_did[imp->ianacode] = 1;
}
}
}

} else {
for (i = 0; i < num_codecs; i++) {
const switch_codec_implementation_t *imp = codecs[i];
@@ -13772,7 +13780,7 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess

for (map = m->m_rtpmaps; map; map = map->rm_next) {

if (map->rm_pt > 127 || already_did[map->rm_pt]) {
if (already_did[map->rm_pt]) {
continue;
}

@@ -13793,6 +13801,7 @@ static void switch_core_media_set_r_sdp_codec_string(switch_core_session_t *sess
} else {
switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ",%s.%s", imp->modname, imp->iananame);
}

already_did[imp->ianacode] = 1;
}
}
5 changes: 4 additions & 1 deletion src/switch_core_memory.c
Original file line number Diff line number Diff line change
@@ -501,9 +501,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_destroy_memory_pool(switch_m
#if APR_POOL_DEBUG
fspr_pool_destroy_debug(tmp_pool, func);
#else
fspr_pool_destroy(tmp_pool);
if (tmp_pool) {
fspr_pool_destroy(tmp_pool);
}
#endif
#ifdef USE_MEM_LOCK

switch_mutex_unlock(memory_manager.mem_lock);
#endif
}
8 changes: 5 additions & 3 deletions src/switch_core_sqldb.c
Original file line number Diff line number Diff line change
@@ -2065,6 +2065,7 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
switch_status_t status;
uint32_t ttl = 0;
uint32_t i;
switch_status_t res;

if (!zstr(qm->pre_trans_execute)) {
switch_cache_db_execute_sql_real(qm->event_db, qm->pre_trans_execute, &errmsg);
@@ -2126,7 +2127,8 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)

for (i = 0; (qm->max_trans == 0 || ttl <= qm->max_trans) && (i < qm->numq); i++) {
switch_mutex_lock(qm->mutex);
switch_queue_trypop(qm->sql_queue[i], &pop);
res = switch_queue_trypop(qm->sql_queue[i], &pop);
(void)res;
switch_mutex_unlock(qm->mutex);
if (pop) break;
}
@@ -2138,6 +2140,7 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
switch_mutex_unlock(qm->mutex);
ttl++;
}

switch_safe_free(pop);
if (status != SWITCH_STATUS_SUCCESS) break;
} else {
@@ -2153,7 +2156,6 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
}
}


end:

switch(qm->event_db->type) {
@@ -2190,11 +2192,11 @@ static uint32_t do_trans(switch_sql_queue_manager_t *qm)
}
}


switch_mutex_lock(qm->mutex);
for (i = 0; i < qm->numq; i++) {
qm->written[i] = qm->pre_written[i];
}

switch_mutex_unlock(qm->mutex);

return ttl;
13 changes: 7 additions & 6 deletions src/switch_cpp.cpp
Original file line number Diff line number Diff line change
@@ -98,21 +98,24 @@ SWITCH_DECLARE(Event *) EventConsumer::pop(int block, int timeout)
void *pop = NULL;
Event *ret = NULL;
switch_event_t *event;
switch_status_t res;

if (!ready) {
return NULL;
}

if (block) {
if (timeout > 0) {
switch_queue_pop_timeout(events, &pop, (switch_interval_time_t) timeout * 1000); // millisec rather than microsec
res = switch_queue_pop_timeout(events, &pop, (switch_interval_time_t) timeout * 1000); // millisec rather than microsec
} else {
switch_queue_pop(events, &pop);
res = switch_queue_pop(events, &pop);
}
} else {
switch_queue_trypop(events, &pop);
res = switch_queue_trypop(events, &pop);
}

(void)res;

if ((event = (switch_event_t *) pop)) {
ret = new Event(event, 1);
}
@@ -138,9 +141,7 @@ SWITCH_DECLARE(void) EventConsumer::cleanup()

node_index = 0;

if (events) {
switch_queue_interrupt_all(events);
}
switch_queue_interrupt_all(events);

while(switch_queue_trypop(events, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *event = (switch_event_t *) pop;
Loading

0 comments on commit bb9afcb

Please sign in to comment.