Avoid dropping null byte in format string (#4732)

* Avoid dropping null byte in format string

* Add a test for null byte in format string
This commit is contained in:
user202729 2026-04-24 00:34:27 +08:00 committed by GitHub
parent 4b50ad7944
commit eeff8680ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -2690,16 +2690,16 @@ template <typename... T> struct fstring {
static_assert(count<(is_view<remove_cvref_t<T>>::value &&
std::is_reference<T>::value)...>() == 0,
"passing views as lvalues is disallowed");
if (FMT_USE_CONSTEVAL) parse_format_string<char>(s, checker(s, arg_pack()));
if (FMT_USE_CONSTEVAL)
parse_format_string<char>(str, checker(str, arg_pack()));
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>();
(void)unused;
}
template <typename S,
FMT_ENABLE_IF(std::is_convertible<const S&, string_view>::value)>
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const S& s) : str(s) {
auto sv = string_view(str);
if (FMT_USE_CONSTEVAL)
detail::parse_format_string<char>(sv, checker(sv, arg_pack()));
detail::parse_format_string<char>(str, checker(str, arg_pack()));
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>();
(void)unused;
}

View File

@ -239,6 +239,16 @@ if (CMAKE_CXX_STANDARD GREATER_EQUAL 20)
#endif
"
ERROR)
expect_compile(
format-string-embedded-nul-error
"
#if FMT_USE_CONSTEVAL
fmt::format(\"a\\0{}\");
#else
#error
#endif
"
ERROR)
# Compile-time argument name check
expect_compile(