Skip to content

Commit eeff868

Browse files
authored
Avoid dropping null byte in format string (#4732)
* Avoid dropping null byte in format string * Add a test for null byte in format string
1 parent 4b50ad7 commit eeff868

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

include/fmt/base.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,16 +2690,16 @@ template <typename... T> struct fstring {
26902690
static_assert(count<(is_view<remove_cvref_t<T>>::value &&
26912691
std::is_reference<T>::value)...>() == 0,
26922692
"passing views as lvalues is disallowed");
2693-
if (FMT_USE_CONSTEVAL) parse_format_string<char>(s, checker(s, arg_pack()));
2693+
if (FMT_USE_CONSTEVAL)
2694+
parse_format_string<char>(str, checker(str, arg_pack()));
26942695
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>();
26952696
(void)unused;
26962697
}
26972698
template <typename S,
26982699
FMT_ENABLE_IF(std::is_convertible<const S&, string_view>::value)>
26992700
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const S& s) : str(s) {
2700-
auto sv = string_view(str);
27012701
if (FMT_USE_CONSTEVAL)
2702-
detail::parse_format_string<char>(sv, checker(sv, arg_pack()));
2702+
detail::parse_format_string<char>(str, checker(str, arg_pack()));
27032703
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>();
27042704
(void)unused;
27052705
}

test/compile-error-test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ if (CMAKE_CXX_STANDARD GREATER_EQUAL 20)
239239
#endif
240240
"
241241
ERROR)
242+
expect_compile(
243+
format-string-embedded-nul-error
244+
"
245+
#if FMT_USE_CONSTEVAL
246+
fmt::format(\"a\\0{}\");
247+
#else
248+
#error
249+
#endif
250+
"
251+
ERROR)
242252

243253
# Compile-time argument name check
244254
expect_compile(

0 commit comments

Comments
 (0)