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

--- Comment #17 from Gabriel Burca <gburca-gnu at ebixio dot com> ---
Speaking of the size parameter, my workaround for the original issue was to
pre-compute the size argument a different way. This however resulted in a
warning that's both right and wrong. Here's the sample code that must be
compiled with -O3 and -Wall:

https://godbolt.org/z/jbf74994z


```
static constexpr unsigned N = 20;
char dst[N + 1];

int main() {
  char buf[10];
  char *src = buf + sizeof(buf);
  *(--src) = 0;
  *(--src) = 'a';
  strncpy(dst, src, std::min<size_t>(N, strlen(src) + 1));

//   constexpr volatile size_t n = std::min<size_t>(N, strlen(src) + 1);
//   strncpy(dst, src, n);
}
```

<source>: In function 'int main()':
<source>:13:10: warning: 'char* strncpy(char*, const char*, size_t)' specified
bound depends on the length of the source argument [-Wstringop-truncation]
   13 |   strncpy(dst, src, std::min<size_t>(N, strlen(src) + 1));
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:13:47: note: length computed here
   13 |   strncpy(dst, src, std::min<size_t>(N, strlen(src) + 1));
      |                                         ~~~~~~^~~~~

Sure, it depends on the src, but in a "good" way.

The commented out code doesn't produce the warning. An alternative way to make
the warning go away is to change to:

char *src = buf + sizeof(buf) - 1;

Reply via email to