From 2819fea3529062c614fd50533b3e41f6d92636f7 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Mon, 18 May 2020 15:15:34 +0300 Subject: [PATCH] ucrtbase: Add tests and fix more zero length __stdio_common_vs[w]printf cases. Signed-off-by: Martin Storsjo Signed-off-by: Piotr Caban Signed-off-by: Alexandre Julliard --- dlls/msvcrt/wcs.c | 8 ++++++-- dlls/ucrtbase/tests/printf.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/dlls/msvcrt/wcs.c b/dlls/msvcrt/wcs.c index 17d06eed3c2..f751d658efb 100644 --- a/dlls/msvcrt/wcs.c +++ b/dlls/msvcrt/wcs.c @@ -744,7 +744,9 @@ int CDECL __stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_s return ret>len ? -1 : ret; if(ret>=len) { if(len) str[len-1] = 0; - return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2; + if(options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) + return ret; + return len > 0 ? -2 : -1; } return ret; } @@ -1284,7 +1286,9 @@ int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options, return ret>len ? -1 : ret; if(ret>=len) { if(len) str[len-1] = 0; - return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2; + if(options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) + return ret; + return len > 0 ? -2 : -1; } return ret; } diff --git a/dlls/ucrtbase/tests/printf.c b/dlls/ucrtbase/tests/printf.c index 8cacbbb79ab..3b48e08b105 100644 --- a/dlls/ucrtbase/tests/printf.c +++ b/dlls/ucrtbase/tests/printf.c @@ -144,6 +144,18 @@ static void test_snprintf (void) "Failure to snprintf to NULL\n"); ok (vsprintf_wrapper (0, NULL, 0, "abcd") == 4, "Failure to snprintf to NULL\n"); + ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, 0, "abcd") == 4, + "Failure to snprintf to zero length buffer\n"); + ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, 0, "abcd") == -1, + "Failure to snprintf to zero length buffer\n"); + ok (vsprintf_wrapper (0, buffer, 0, "abcd") == -1, + "Failure to snprintf to zero length buffer\n"); + ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, 0, "") == 0, + "Failure to snprintf a zero length string to a zero length buffer\n"); + ok (vsprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, 0, "") == 0, + "Failure to snprintf a zero length string to a zero length buffer\n"); + ok (vsprintf_wrapper (0, buffer, 0, "") == -1, + "Failure to snprintf a zero length string to a zero length buffer\n"); } static int WINAPIV vswprintf_wrapper(unsigned __int64 options, wchar_t *str, @@ -163,6 +175,7 @@ static void test_swprintf (void) const wchar_t str_justfit[] = {'j','u','s','t','f','i','t',0}; const wchar_t str_justfits[] = {'j','u','s','t','f','i','t','s',0}; const wchar_t str_muchlonger[] = {'m','u','c','h','l','o','n','g','e','r',0}; + const wchar_t str_empty[] = {0}; const wchar_t *tests[] = {str_short, str_justfit, str_justfits, str_muchlonger}; wchar_t buffer[8]; @@ -225,6 +238,18 @@ static void test_swprintf (void) "Failure to swprintf to NULL\n"); ok (vswprintf_wrapper (0, NULL, 0, str_short) == 5, "Failure to swprintf to NULL\n"); + ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, 0, str_short) == 5, + "Failure to swprintf to a zero length buffer\n"); + ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, 0, str_short) == -1, + "Failure to swprintf to a zero length buffer\n"); + ok (vswprintf_wrapper (0, buffer, 0, str_short) == -1, + "Failure to swprintf to a zero length buffer\n"); + ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, buffer, 0, str_empty) == 0, + "Failure to swprintf a zero length string to a zero length buffer\n"); + ok (vswprintf_wrapper (_CRT_INTERNAL_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION, buffer, 0, str_empty) == 0, + "Failure to swprintf a zero length string to a zero length buffer\n"); + ok (vswprintf_wrapper (0, buffer, 0, str_empty) == -1, + "Failure to swprintf a zero length string to a zero length buffer\n"); } static int WINAPIV vfprintf_wrapper(FILE *file,