https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106926

            Bug ID: 106926
           Summary: string_view construction from literal string
                    containing null/zero should warn
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com
  Target Milestone: ---

This code compiles but does something the programmer almost certainly does not
want:

    #include <string_view>
    constexpr std::string_view sv = "four\0nine"; // 9 bytes of data
    static_assert(sv.size() == 4); // required by C++, but surprising

GCC can't implement what the programmer intended, so it should warn if a
string_view is constructed or assigned from a string literal which contains
null bytes before the end.

When this happens, the rest of the string literal data is still accessible but
only if you know it's there by some other means.  I expect such usage to be
very rare, so warning about it even at -Wall seems reasonable.  Even if the
warning only comes with -Wextra that would be better than silence.

Reply via email to