https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83373
Bug ID: 83373 Summary: False positive reported by -Wstringop-overflow Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: bugzi...@poradnik-webmastera.com Target Milestone: --- When code below is compiled, gcc incorrectly complains that memcpy will read data after end of buffer in line marked with star. Looks that gcc does not take into account that 'if' above protects against this. Code was compiles using gcc (GCC) 8.0.0 20171210 (experimental). [code] #include <string.h> char buf[100]; void get_data(char* ptr); __attribute((nonnull(1, 2))) inline char* my_strcpy(char* __restrict__ dst, const char* __restrict__ src, size_t size) { size_t len = strlen(src); if (len < size) memcpy(dst, src, len + 1); else { memcpy(dst, src, size - 1); //* dst[size - 1] = '\0'; } return dst; } void test() { char data[20]; get_data(data); my_strcpy(buf, data, sizeof(buf)); } [/code] [out] $ g++ -c -o test.o test.cc -O2 -Wall In function ‘char* my_strcpy(char*, const char*, size_t)’, inlined from ‘void test()’ at test.cc:25:14: test.cc:15:15: warning: ‘void* memcpy(void*, const void*, size_t)’ reading 99 bytes from a region of size 20 [-Wstringop-overflow=] memcpy(dst, src, size - 1); //* ~~~~~~^~~~~~~~~~~~~~~~~~~~ [/out]