[Core] switch_mprintf: Increase the size of loop variables in the printf() implementation. Add unit-tests disabled by default.

This commit is contained in:
Andrey Volk 2022-12-09 17:42:30 +03:00
parent ec32dc300f
commit f71a56022a
2 changed files with 46 additions and 2 deletions

View File

@ -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)

View File

@ -36,6 +36,8 @@
#include <openssl/ssl.h>
#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;