+2023-04-01 Bruno Haible <bruno@clisp.org>
+
+ 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.
+
2023-04-01 Bruno Haible <bruno@clisp.org>
stddef: Fix __need_wint_t handling in case of two stddef.h overrides.
count = local_wcrtomb (cbuf, arg, &state);
if (count < 0)
- /* Inconsistency. */
- abort ();
+ /* Cannot convert. */
+ goto fail_with_EILSEQ;
characters = count;
}
}
# endif
count = local_wcrtomb (cbuf, arg, &state);
- if (count <= 0)
- /* Inconsistency. */
- abort ();
+ if (count < 0)
+ /* Cannot convert. */
+ goto fail_with_EILSEQ;
ENSURE_ALLOCATION (xsum (length, count));
memcpy (result + length, cbuf, count);
length += count;
errno = ENOMEM;
goto fail_with_errno;
-#if ENABLE_UNISTDIO || ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T)
+#if ENABLE_UNISTDIO || ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T) || ((NEED_PRINTF_DIRECTIVE_LC || ENABLE_WCHAR_FALLBACK) && HAVE_WINT_T && !WIDE_CHAR_VERSION)
fail_with_EILSEQ:
errno = EILSEQ;
goto fail_with_errno;
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
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
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
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
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