[PATCH v2] msvcrt: Don't crash if _vsnwprintf gets NULL as format string and add test

Fabian Maurer dark.shadow4 at web.de
Sat Nov 18 13:04:14 CST 2017


v2: Fix crash on older test machines

Signed-off-by: Fabian Maurer <dark.shadow4 at web.de>
---
 dlls/msvcrt/printf.h       |  3 +++
 dlls/msvcrt/tests/printf.c | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/dlls/msvcrt/printf.h b/dlls/msvcrt/printf.h
index 4ba02bafdb..a88097232d 100644
--- a/dlls/msvcrt/printf.h
+++ b/dlls/msvcrt/printf.h
@@ -376,6 +376,9 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API
 
     TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt));
 
+    if(!fmt)
+        return -1;
+
     if(!locale)
         locinfo = get_locinfo();
     else
diff --git a/dlls/msvcrt/tests/printf.c b/dlls/msvcrt/tests/printf.c
index 64658ce25f..5cbc1c31a1 100644
--- a/dlls/msvcrt/tests/printf.c
+++ b/dlls/msvcrt/tests/printf.c
@@ -1204,6 +1204,14 @@ static int WINAPIV _vsnwprintf_wrapper(wchar_t *str, size_t len, const wchar_t *
     return ret;
 }
 
+static BOOL is_vista_or_newer(void)
+{
+    OSVERSIONINFOA version;
+    version.dwOSVersionInfoSize = sizeof(version);
+    GetVersionExA(&version);
+    return version.dwMajorVersion > 5;
+}
+
 static void test_vsnwprintf(void)
 {
     const wchar_t format[] = {'%','w','s','%','w','s','%','w','s',0};
@@ -1226,6 +1234,17 @@ static void test_vsnwprintf(void)
 
     ret = _vsnwprintf_wrapper( NULL, 0, format, one, two, three );
     ok( ret == 11 || broken(ret == -1 /* Win2k */), "got %d, expected 11\n", ret );
+
+    /* Test with format string set to NULL, we should not crash on vista or newer. */
+    if(is_vista_or_newer())
+    {
+        str[0] = 'x';
+        ret = _vsnwprintf_wrapper( str, 0, NULL );
+        ok( ret == -1, "got %d, expected -1\n", ret );
+        ok( str[0] == 'x', "Expected string to be unchanged.\n" );
+    }
+    else
+        win_skip("_vsnwprintf would crash with a NULL argument.\n");
 }
 
 static int WINAPIV vswprintf_wrapper(wchar_t *str, const wchar_t *format, ...)
-- 
2.15.0




More information about the wine-devel mailing list