Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warning #160

Merged
merged 1 commit into from
Feb 3, 2023
Merged

Fix compiler warning #160

merged 1 commit into from
Feb 3, 2023

Conversation

jmcarcell
Copy link
Contributor

@jmcarcell jmcarcell commented Feb 2, 2023

BEGINRELEASENOTES

  • Fix a compiler warning about strncpy usage

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

@tmadlener tmadlener merged commit 2a0d5d1 into iLCSoft:master Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants