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

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
To be safe, even though %s requires that the argument be a nul-terminated
string, the snprintf optimization would need to be predicated on knowing that
it, in fact, is one (to avoid buffer overflow if snprintf was called with a
non-string argument).  The optimization opportunity is in the length of the
source string not needing to be known.  When the source array is not guaranteed
to be a nul-terminated string then the memccpy optimization described in
pr88814 could take place instead.

So to clarify the example in comment #0, the array size optimization would only
be applicable if s was previously used as a string, such as in an argument to
some string function like in:

  char d[8];
  char s[8];
  char *p;

  void g (void)
  {
    p = __builtin_strchr (s, '/');   // s must be a nul-terminated string
    ...
    __builtin_snprintf (d, sizeof d, "%s", s);   // can be folded to strcpy
  }

Reply via email to