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

--- Comment #2 from John Zwinck <jzwinck at gmail dot com> ---
Jonathan, yes it was a real problem, I wrote such buggy code myself.  I was
more complacent with string_view than I might have been with std::string
because everyone knows string_view doesn't have to be null terminated (bad
excuse, but it's mine).

I agree the same problem could happen with std::string.  As for your idea to
add an attribute, I assume you mean something like this:

    string_view(const char* s __attribute__((does_strlen)));

I think this would work but it seems like the attribute would have to be added
in many places.  Instead, the compiler could statically determine that the
length of the string is lost in code like this:

    string_view foo("bad\0string");

And maybe even here:

    const char* bar = "another\0one";

Though that may be a step too far because someone could hard-code the length 12
elsewhere, and there probably is code in the wild doing that.

In general the diagnostic could apply wherever the compiler knows the contents
will be copied.  Since that's not always possible to know, maybe it could
assume copying will happen when the literal is passed to an out-of-line
function.

I recognize this might be too harsh for -Wall; I'd still be quite happy to see
the warning with -Wextra.  The attribute idea is also fine, if you think it's
feasible to apply it in enough places and not too ugly.

Thank you.

Reply via email to