From f71a56022a579a91c8f563cc80c41c5129068bbd Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Fri, 9 Dec 2022 17:42:30 +0300 Subject: [PATCH] [Core] switch_mprintf: Increase the size of loop variables in the printf() implementation. Add unit-tests disabled by default. --- src/switch_mprintf.c | 4 ++-- tests/unit/switch_core.c | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/switch_mprintf.c b/src/switch_mprintf.c index 5f576a0bfd..d840b4f8f1 100644 --- a/src/switch_mprintf.c +++ b/src/switch_mprintf.c @@ -680,8 +680,8 @@ static int vxprintf(void (*func) (void *, const char *, int), /* Consumer of tex case etSQLESCAPE2: case etSQLESCAPE4: case etSQLESCAPE3:{ - int i, j, n, ch, isnull; - int needQuote; + size_t i, j, n, ch; + int needQuote, isnull; char *escarg = va_arg(ap, char *); isnull = escarg == 0; if (isnull) diff --git a/tests/unit/switch_core.c b/tests/unit/switch_core.c index 5689513cbc..4c302ea7aa 100644 --- a/tests/unit/switch_core.c +++ b/tests/unit/switch_core.c @@ -36,6 +36,8 @@ #include #endif +#define ENABLE_SNPRINTFV_TESTS 0 /* Do not turn on for CI as this requires a lot of RAM */ + FST_CORE_BEGIN("./conf") { FST_SUITE_BEGIN(switch_core) @@ -51,6 +53,48 @@ FST_CORE_BEGIN("./conf") } FST_TEARDOWN_END() +#if ENABLE_SNPRINTFV_TESTS + FST_TEST_BEGIN(test_snprintfv_1) + { + size_t src_buf_size = 0x100000001; + char* src = calloc(1, src_buf_size); + + if (!src) { + printf("bad allocation\n"); + + return -1; + } + + src[0] = '\xc0'; + memset(src + 1, '\x80', 0xffffffff); + + char dst[256]; + switch_snprintfv(dst, 256, "'%!q'", src); + free(src); + } + FST_TEST_END() + + FST_TEST_BEGIN(test_snprintfv_2) + { +#define STR_LEN ((0x100000001 - 3) / 2) + + char* src = calloc(1, STR_LEN + 1); /* Account for NULL byte. */ + + if (!src) { return -1; } + + memset(src, 'a', STR_LEN); + + char* dst = calloc(1, STR_LEN + 3); /* Account for extra quotes and NULL byte */ + if (!dst) { return -1; } + + switch_snprintfv(dst, 2 * STR_LEN + 3, "'%q'", src); + + free(src); + free(dst); + } + FST_TEST_END() +#endif + FST_TEST_BEGIN(test_switch_event_add_header_leak) { switch_event_t* event;