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

            Bug ID: 85728
           Summary: strncpy -Wstringop-truncation
           Product: gcc
           Version: 8.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: evk55 at 126 dot com
  Target Milestone: ---

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

Reply via email to