Bug 85728 - strncpy -Wstringop-truncation
Summary: strncpy -Wstringop-truncation
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Wstringop-truncation
  Show dependency treegraph
 
Reported: 2018-05-10 08:15 UTC by straw618
Modified: 2019-01-09 23:46 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description straw618 2018-05-10 08:15:11 UTC
const char* Status::CopyState(const char* state) {
  const size_t cch = std::strlen(state) + 1; // +1 for the null terminator
  char* const result = new char[cch];
  result[cch - 1] = '\0';
#ifdef OS_WIN
  errno_t ret;
  ret = strncpy_s(result, cch, state, cch - 1);
  assert(ret == 0);
#else
  std::strncpy(result, state, cch - 1);
#endif
  return result;
}


error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
   std::strncpy(result, state, cch);
   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
status.cc:18:33: note: length computed here
   const size_t cch = std::strlen(state);
Comment 1 Martin Sebor 2018-05-10 16:20:04 UTC
Please review https://gcc.gnu.org/bugs to see what information GCC bug reports are expected to include.  This one contains none of the requested information.  As far as I can see the warning message reflects the effect of the strncpy call in the function but the source code printed by the message doesn't correspond to the source code so it's not clear how the diagnostic messages are related to the code snippet, or even what it is you are trying to point out.  Thus this is not a valid report.

If you can provide the requested information and explain what you think is wrong with GCC's output feel free to reopen it.