https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85700
Bug ID: 85700 Summary: Spurious -Wstringop-truncation warning with strncat Product: gcc Version: 8.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lopresti at gmail dot com Target Milestone: --- Test program: --- #include <cstring> #include <cstdio> char buf1[256]; void append(const char *s) { std::strncat(buf1, s, sizeof(buf1) - strlen(buf1) - 1); } void doit(int err) { char tmp[256]; std::snprintf(tmp, sizeof(tmp), " errno %d (%s)", err, std::strerror(err)); append(tmp); } --- Compile with "g++ -O3 -Wall -c test.cc". Expected results: No warning, as the documentation says for this idiomatic use of strncat(). Actual results: In function ‘void append(const char*)’, inlined from ‘void doit(int)’ at test.cc:16:9: test.cc:8:15: warning: ‘char* strncat(char*, const char*, size_t)’ output may b\ e truncated copying between 0 and 255 bytes from a string of length 255 [-Wstri\ ngop-truncation] std::strncat(buf1, s, sizeof(buf1) - strlen(buf1) - 1); ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Note that a similar program in C does not trigger the warning.