Skip to content

Avoid dropping null byte in format string#4732

Open
user202729 wants to merge 1 commit intofmtlib:mainfrom
user202729:avoid-drop-null
Open

Avoid dropping null byte in format string#4732
user202729 wants to merge 1 commit intofmtlib:mainfrom
user202729:avoid-drop-null

Conversation

@user202729
Copy link
Copy Markdown
Contributor

@user202729 user202729 commented Apr 6, 2026

Consider

fmt::format("a\0{}");
fmt::format("{}\0a");

Both ought to raise a compile-time error because of missing format argument, but before this change, only the second one will. This is because previously s instead of str was passed in the checking function, which uses the following constructor overload

#if FMT_GCC_VERSION
  FMT_ALWAYS_INLINE
#endif
  FMT_CONSTEXPR basic_string_view(const Char* s) : data_(s) {
#if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
    if (std::is_same<Char, char>::value && !detail::is_constant_evaluated()) {
      size_ = __builtin_strlen(detail::narrow(s));  // strlen is not constexpr.
      return;
    }
#endif
    size_t len = 0;
    while (*s++) ++len;
    size_ = len;
  }

and drop everything after the null byte.

Also simplify the other constructor since str's type is already string_view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant