diff options
author | Bruno Haible <bruno@clisp.org> | 2023-04-01 11:45:23 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-04-01 11:45:23 +0200 |
commit | 154a389b7cc0654a4fbec0faf4f0ddc3e89a947a (patch) | |
tree | 1db12ccfbcc04e30c4643a41dfa6e3d4638e5969 /tests | |
parent | 5386b7c76fc0ecf8b05003a5b6daaa33799d506f (diff) | |
download | gnulib-154a389b7cc0654a4fbec0faf4f0ddc3e89a947a.tar.gz |
vasnprintf-posix: Fix crash in narrow %lc directive (regr. 2023-03-21).
* lib/vasnprintf.c (VASNPRINTF): Negative results of local_wcrtomb can
now occur; handle them.
* tests/test-vasnprintf-posix.c (test_function): Test %lc directive with
a wint_t argument that is not a valid wide character.
* tests/test-snprintf-posix.h (test_function): Likewise.
* tests/test-sprintf-posix.h (test_function): Likewise.
* tests/test-vasnwprintf-posix.c (test_function): Likewise.
* tests/test-vasprintf-posix.c (test_function): Likewise.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-snprintf-posix.h | 18 | ||||
-rw-r--r-- | tests/test-sprintf-posix.h | 16 | ||||
-rw-r--r-- | tests/test-vasnprintf-posix.c | 18 | ||||
-rw-r--r-- | tests/test-vasnwprintf-posix.c | 29 | ||||
-rw-r--r-- | tests/test-vasprintf-posix.c | 20 |
5 files changed, 101 insertions, 0 deletions
diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h index bfc8b9914e..90fcf61c5a 100644 --- a/tests/test-snprintf-posix.h +++ b/tests/test-snprintf-posix.h @@ -3089,6 +3089,24 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) ASSERT (memcmp (result, "az 33\0", 5 + 1) == 0); ASSERT (retval == 5); } + + static wint_t L_invalid = (wchar_t) 0x76543210; + + { /* Invalid wide character. + The conversion may succeed or may fail, but it should not abort. */ + int retval = + my_snprintf (result, sizeof (result), + "%lc %d", L_invalid, 33, 44, 55); + (void) retval; + } + + { /* Invalid wide character and width. + The conversion may succeed or may fail, but it should not abort. */ + int retval = + my_snprintf (result, sizeof (result), + "%10lc %d", L_invalid, 33, 44, 55); + (void) retval; + } #endif /* Test the support of the 'x' conversion specifier for hexadecimal output of diff --git a/tests/test-sprintf-posix.h b/tests/test-sprintf-posix.h index d2f0e579ea..6ea2052186 100644 --- a/tests/test-sprintf-posix.h +++ b/tests/test-sprintf-posix.h @@ -3067,6 +3067,22 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) ASSERT (memcmp (result, "az 33\0", 5 + 1) == 0); ASSERT (retval == 5); } + + static wint_t L_invalid = (wchar_t) 0x76543210; + + { /* Invalid wide character. + The conversion may succeed or may fail, but it should not abort. */ + int retval = + my_sprintf (result, "%lc %d", L_invalid, 33, 44, 55); + (void) retval; + } + + { /* Invalid wide character and width. + The conversion may succeed or may fail, but it should not abort. */ + int retval = + my_sprintf (result, "%10lc %d", L_invalid, 33, 44, 55); + (void) retval; + } #endif /* Test the support of the 'x' conversion specifier for hexadecimal output of diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index bc71e02156..9eb042c552 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -4015,6 +4015,24 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) ASSERT (length == 5); free (result); } + + static wint_t L_invalid = (wchar_t) 0x76543210; + + { /* Invalid wide character. + The conversion may succeed or may fail, but it should not abort. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%lc %d", L_invalid, 33, 44, 55); + free (result); + } + + { /* Invalid wide character and width. + The conversion may succeed or may fail, but it should not abort. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%10lc %d", L_invalid, 33, 44, 55); + free (result); + } #endif /* Test the support of the 'x' conversion specifier for hexadecimal output of diff --git a/tests/test-vasnwprintf-posix.c b/tests/test-vasnwprintf-posix.c index f0ecfb082a..74a26079d3 100644 --- a/tests/test-vasnwprintf-posix.c +++ b/tests/test-vasnwprintf-posix.c @@ -4020,6 +4020,35 @@ test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, ASSERT (length == 6); free (result); } + + static wint_t L_invalid = (wchar_t) 0x76543210; + + { /* Invalid wide character. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%lc %d", L_invalid, 33, 44, 55); + /* No failure is allowed: ISO C says "the wint_t argument is converted + to wchar_t and written." */ + ASSERT (result != NULL); + ASSERT (result[0] == (wchar_t) 0x76543210); + ASSERT (wmemcmp (result + 1, L" 33\0", 3 + 1) == 0); + ASSERT (length == 4); + free (result); + } + + { /* Invalid wide character and width. */ + size_t length; + wchar_t *result = + my_asnwprintf (NULL, &length, L"%10lc %d", L_invalid, 33, 44, 55); + /* No failure is allowed: ISO C says "the wint_t argument is converted + to wchar_t and written." */ + ASSERT (result != NULL); + ASSERT (wmemcmp (result, L" ", 9) == 0); + ASSERT (result[9] == (wchar_t) 0x76543210); + ASSERT (wmemcmp (result + 10, L" 33\0", 3 + 1) == 0); + ASSERT (length == 13); + free (result); + } #endif /* Test the support of the 'x' conversion specifier for hexadecimal output of diff --git a/tests/test-vasprintf-posix.c b/tests/test-vasprintf-posix.c index 6a18413abb..e4cc0fb983 100644 --- a/tests/test-vasprintf-posix.c +++ b/tests/test-vasprintf-posix.c @@ -3954,6 +3954,26 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) ASSERT (retval == 5); free (result); } + + static wint_t L_invalid = (wchar_t) 0x76543210; + + { /* Invalid wide character. + The conversion may succeed or may fail, but it should not abort. */ + char *result; + int retval = + my_asprintf (&result, "%lc %d", L_invalid, 33, 44, 55); + if (retval >= 0) + free (result); + } + + { /* Invalid wide character and width. + The conversion may succeed or may fail, but it should not abort. */ + char *result; + int retval = + my_asprintf (&result, "%10lc %d", L_invalid, 33, 44, 55); + if (retval >= 0) + free (result); + } #endif /* Test the support of the 'x' conversion specifier for hexadecimal output of |