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

            Bug ID: 84474
           Summary: Unexpected -Wstringop-truncation
           Product: gcc
           Version: 8.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: romain.geissler at amadeus dot com
  Target Milestone: ---

Hi,

When migrating some code base to gcc 8, I am hitting an issue on this snippet:

<<EOF
#include <string.h>

void f()
{
    char aBuf[3 + 1];
    strncpy(aBuf, "123", 3);
    aBuf[3] = '\0';
}
EOF

compiled with: -Wall -Werror -Wextra -std=gnu++17

I get:
error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before
terminating nul copying 3 bytes from a string of the same length
[-Werror=stringop-truncation]

Here there is really nothing wrong in using strncpy. You might argue that here
using strncpy is useless, while memcpy would simply do the trick. This is true,
but this was extracted from a bigger code base where using strncpy actually
makes sense ("123" is not hardcoded but is a user provided pointer, that gcc is
clever enough to inline with -O2, thus hitting the very same error).

Cheers,
Romain

Reply via email to