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

            Bug ID: 87382
           Summary: warn for strncpy with a bound greater than the size of
                    source array
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

I got the following feedback/request for an enhancement to
-Wstringop-truncation (or some other warning).

> Example 3: this warns only if src is nonstring (because then it is
> guaranteed to be an invalid program). The issue here is that, since
> Example 1 warns due to "specified bound 12 equals destination size",
> it may be confusing that this doesn't (because the warning of Example
> 1 does not reference src in any way).
> 
> extern char dst[12];
> extern char src[4];
> void f(void) {
>     // no warning
>     strncpy(dst, src, sizeof(dst));
> }

My response is that: here the assumption is that the source is nul-terminated
but
I agree that there is a potential problem lurking here: why would the
programmer specify a larger bound than the size of the source?  This might be
worth warning about as well, for that reason.  (I.e., not because the result
may not be nul-terminated, but because it suggests that the programmer may have
used the size of the wrong argument as the bound.)

Reply via email to