Commit 48b8b0c4d7 for freeswitch.com

commit 48b8b0c4d70cbd533684fcb2e238663beeedb02a
Author: Dragos Oancea <dragos@signalwire.com>
Date:   Wed Mar 29 19:08:18 2023 +0300

    [Core] Coverity fixes

    * [core] coverity CID 1395504 (Logically dead code)

    * [core] coverity CID 1395581 (Dereference before null check)

    * [core] coverity CID 1346451 (Improper use of negative value)

    * [core] coverity CID 1395497 (Dereference before null check)

    * [core] coverity CID 1468458 (Dereference before null check)

diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 0754494ee3..d4954ee6b1 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -3057,7 +3057,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session
 							uint32_t codec_ms = (int) (engine->read_frame.timestamp -
 													   engine->last_ts) / (engine->read_impl.samples_per_second / 1000);
 							if (engine->last_seq && (int) (engine->read_frame.seq - engine->last_seq) > 1) {
-								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[%s]: Correcting calculated ptime value from [%d] to [%d] to compensate for [%d] lost packet(s). \n", is_vbr?"VBR":"CBR", codec_ms, codec_ms / (int) (engine->read_frame.seq - engine->last_seq), (int) (engine->read_frame.seq - engine->last_seq - 1));
+								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[CBR]: Correcting calculated ptime value from [%d] to [%d] to compensate for [%d] lost packet(s). \n",  codec_ms, codec_ms / (int) (engine->read_frame.seq - engine->last_seq), (int) (engine->read_frame.seq - engine->last_seq - 1));
 								codec_ms = codec_ms / (int) (engine->read_frame.seq - engine->last_seq);
 							}

@@ -3080,9 +3080,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session

 									if (codec_ms > 120) {	/* yeah right */
 										switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
-														  "[%s]: Your phone is trying to send timestamps that suggest an increment of %dms per packet\n"
+														  "[CBR]: Your phone is trying to send timestamps that suggest an increment of %dms per packet\n"
 														  "That seems hard to believe so I am going to go on ahead and um ignore that, mmkay?\n",
-														  is_vbr?"VBR":"CBR",
 														  (int) codec_ms);
 										engine->check_frames = MAX_CODEC_CHECK_FRAMES;
 										goto skip;
@@ -3092,8 +3091,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session

 									if (codec_ms != engine->cur_payload_map->codec_ms) {
 										switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
-														  "[%s]: Asynchronous PTIME not supported, changing our end from %d to %d\n",
-														  is_vbr?"VBR":"CBR",
+														  "[CBR]: Asynchronous PTIME not supported, changing our end from %d to %d\n",
 														  (int) engine->cur_payload_map->codec_ms,
 														  (int) codec_ms
 														  );
@@ -3123,7 +3121,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session
 								   engine->last_ts) / (engine->read_impl.samples_per_second / 1000);

 						if (engine->last_seq && (int) (engine->read_frame.seq - engine->last_seq) > 1) {
-								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[%s]: Correcting calculated ptime value from [%d] to [%d] to compensate for [%d] lost packet(s)\n", is_vbr?"VBR":"CBR", codec_ms, codec_ms / (int) (engine->read_frame.seq - engine->last_seq), (int) (engine->read_frame.seq - engine->last_seq - 1));
+								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[VBR]: Correcting calculated ptime value from [%d] to [%d] to compensate for [%d] lost packet(s)\n", codec_ms, codec_ms / (int) (engine->read_frame.seq - engine->last_seq), (int) (engine->read_frame.seq - engine->last_seq - 1));
 								codec_ms = codec_ms / (int) (engine->read_frame.seq - engine->last_seq);
 						}

@@ -3142,8 +3140,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session
 							if (codec_ms > 120) {
 								/*will show too many times with packet loss*/
 								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG3,
-												  "[%s]: Remote party is trying to send timestamps that suggest an increment of [%d] ms per packet, which is too high. Ignoring.\n",
-												   is_vbr?"VBR":"CBR",
+												  "[VBR]: Remote party is trying to send timestamps that suggest an increment of [%d] ms per packet, which is too high. Ignoring.\n",
 												  (int) codec_ms);
 								engine->last_ts = engine->read_frame.timestamp;
 								engine->last_seq = engine->read_frame.seq;
@@ -3152,8 +3149,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_read_frame(switch_core_session

 							if (codec_ms != engine->cur_payload_map->codec_ms) {
 								switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
-												  "[%s]: Packet size change detected. Remote PTIME changed from [%d] to [%d]\n",
-												  is_vbr?"VBR":"CBR",
+												  "[VBR]: Packet size change detected. Remote PTIME changed from [%d] to [%d]\n",
 												  (int) engine->cur_payload_map->codec_ms,
 												  (int) codec_ms
 												  );
diff --git a/src/switch_ivr_originate.c b/src/switch_ivr_originate.c
index e0b0b55246..f23549713b 100644
--- a/src/switch_ivr_originate.c
+++ b/src/switch_ivr_originate.c
@@ -909,6 +909,9 @@ static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map
 		return -1;
 	}
 	wrote = teletone_mux_tones(ts, map);
+	if (wrote <= 0) {
+		return -1;
+	}

 	if (tto->channels != 1) {
 		if (tto->mux_buflen < wrote * 2 * tto->channels) {
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 17f3e66f8c..8398717818 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -3529,9 +3529,13 @@ SWITCH_DECLARE(dtls_state_t) switch_rtp_dtls_state(switch_rtp_t *rtp_session, dt
 {
 	dtls_state_t s = DS_OFF;

+	if (!rtp_session) {
+		return s;
+	}
+
 	switch_mutex_lock(rtp_session->ice_mutex);

-	if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
+	if (!rtp_session->dtls && !rtp_session->rtcp_dtls) {
 		s = DS_OFF;
 		goto done;
 	}
@@ -3556,9 +3560,13 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_del_dtls(switch_rtp_t *rtp_session, d
 {
 	switch_status_t status = SWITCH_STATUS_SUCCESS;

+	if (!rtp_session) {
+		return SWITCH_STATUS_FALSE;
+	}
+
 	switch_mutex_lock(rtp_session->ice_mutex);

-	if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
+	if (!rtp_session->dtls && !rtp_session->rtcp_dtls) {
 		switch_goto_status(SWITCH_STATUS_FALSE, done);
 	}

@@ -6967,13 +6975,10 @@ static void check_timeout(switch_rtp_t *rtp_session)
 					  elapsed, rtp_session->media_timeout);

 	if (elapsed > rtp_session->media_timeout) {
-
-		if (rtp_session->session) {
 			switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);

 			switch_channel_execute_on(channel, "execute_on_media_timeout");
 			switch_channel_hangup(channel, SWITCH_CAUSE_MEDIA_TIMEOUT);
-		}
 	}
 }