From: Fabian Maurer Subject: [PATCH v2] msvcrt: Don't crash if _vsnwprintf gets NULL as format string and add test Message-Id: <20171118190414.11520-1-dark.shadow4@web.de> Date: Sat, 18 Nov 2017 20:04:14 +0100 v2: Fix crash on older test machines Signed-off-by: Fabian Maurer --- 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