]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] switch_mprintf: Increase the size of loop variables in the printf() implementa...
authorAndrey Volk <andywolk@gmail.com>
Fri, 9 Dec 2022 14:42:30 +0000 (17:42 +0300)
committerAndrey Volk <andywolk@gmail.com>
Fri, 9 Dec 2022 20:11:25 +0000 (23:11 +0300)
src/switch_mprintf.c
tests/unit/switch_core.c

index 5f576a0bfd0fc8490d4d6dd93861650774b66309..d840b4f8f144fdc0764016c107d43e6a94118e61 100644 (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)
index 5689513cbc85120c70afb218158c75ba1bab59a1..4c302ea7aa370daccda1ee46277f2fc6bd3e4a72 100644 (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;