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
          Reporter: romain.geissler at amadeus dot com
  Target Milestone: ---


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

#include <string.h>

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

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

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).


Reply via email to