From 0fdd5b67277706e8dccacb98ac7790c24cd22062 Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Wed, 3 Nov 2021 17:00:08 +0300 Subject: [PATCH] [Core] Fix switch_event_base_add_header leaking memory when headers are with indexes. Add a unit-test. * [Unit-tests] Add a test provoking switch_event_add_header to memory leak. * [Core] Fix switch_event_base_add_header leaking memory when headers are with indexes. --- libs/esl/src/esl_event.c | 3 +++ src/switch_event.c | 3 +++ tests/unit/switch_core.c | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/libs/esl/src/esl_event.c b/libs/esl/src/esl_event.c index 8a88395ec2..cca7899d80 100644 --- a/libs/esl/src/esl_event.c +++ b/libs/esl/src/esl_event.c @@ -484,11 +484,14 @@ static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t st exists = 1; } + FREE(data); goto redraw; } } else if (tmp_header) { free_header(&tmp_header); } + + FREE(data); goto end; } else { if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { diff --git a/src/switch_event.c b/src/switch_event.c index 3e5b05ae32..a24bee0126 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -1062,11 +1062,14 @@ static switch_status_t switch_event_base_add_header(switch_event_t *event, switc exists = 1; } + FREE(data); goto redraw; } } else if (tmp_header) { free_header(&tmp_header); } + + FREE(data); goto end; } else { if ((stack & SWITCH_STACK_PUSH) || (stack & SWITCH_STACK_UNSHIFT)) { diff --git a/tests/unit/switch_core.c b/tests/unit/switch_core.c index 126921997c..5689513cbc 100644 --- a/tests/unit/switch_core.c +++ b/tests/unit/switch_core.c @@ -51,6 +51,22 @@ FST_CORE_BEGIN("./conf") } FST_TEARDOWN_END() + FST_TEST_BEGIN(test_switch_event_add_header_leak) + { + switch_event_t* event; + + if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number[0]", "1"); + switch_event_fire(&event); + } + + if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number[5000]", "12"); + switch_event_fire(&event); + } + } + FST_TEST_END() + FST_TEST_BEGIN(test_xml_free_attr) { switch_xml_t parent_xml = switch_xml_new("xml");