iLCSoft / LCIO

Linear Collider I/O
BSD 3-Clause "New" or "Revised" License
17 stars 34 forks source link

Fix compiler warning #160

Closed jmcarcell closed 1 year ago

jmcarcell commented 1 year ago

BEGINRELEASENOTES

ENDRELEASENOTES

There is this warning when compiling:

LCIO/src/cpp/src/UTIL/lXDR.cc: In member function ‘void UTIL::lXDR::setFileName(const char*, bool)’:
LCIO/src/cpp/src/UTIL/lXDR.cc:78:11: warning: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
   78 |    strncpy(_fileName, filename, n);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
LCIO/src/cpp/src/UTIL/lXDR.cc:76:18: note: length computed here
   76 |    int n = strlen(filename);

this is being picky about how the copying is done, because we can use strncpy to copy the \0 character without having to do it manually (strncpy will put the \0 once it exhausts the second argument (source)). All tests pass, but I'm not sure this is tested for explicitly.

I think the rest of the warnings will be gone when #151 is done

Simple example:

int main () {
  char src[6] = "hello";
  int n = strlen(src) + 1;
  char *dst = new char [n];
  strncpy(dst, src, n);
  cout << dst << " " << (dst[n-1] == '\0') << endl; 
}

Output:

hello 1