From f09ed875b95823e7a52c155e2152d9becb83f27f Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Fri, 24 Jul 2020 22:42:49 +0400 Subject: [PATCH] [Core] Fix buffer race in switch_ivr_eavesdrop_session() --- src/switch_ivr_async.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index cc5ec0581a..621836de00 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -2576,12 +2576,11 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session } if (ep->buffer) { + switch_buffer_lock(ep->buffer); while (switch_buffer_inuse(ep->buffer) >= len) { int tchanged = 0, changed = 0; - switch_buffer_lock(ep->buffer); write_frame.datalen = (uint32_t) switch_buffer_read(ep->buffer, buf, len); - switch_buffer_unlock(ep->buffer); write_frame.samples = write_frame.datalen / 2 / channels; write_frame.channels = channels; @@ -2635,6 +2634,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot init codec\n"); + switch_buffer_unlock(ep->buffer); goto end; } } @@ -2655,7 +2655,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session if ((status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0)) != SWITCH_STATUS_SUCCESS) { break; } + + switch_buffer_unlock(ep->buffer); + switch_buffer_lock(ep->buffer); } + + switch_buffer_unlock(ep->buffer); } }