https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87383
Bug ID: 87383
Summary: improve text and detail in -Wstringop-truncation
warnings
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 from a Linux kernel developer on/suggestions for
improvements in -Wstringop-truncation:
> Example 1: this warns. According to the documentation, because
> strlen(src) could be >= strlen(dst) and therefore dst not
> NUL-terminated. However, the warning message could be improved to
> reflect that (if that is truly the reason we have this warning).
>
> extern char dst[12];
> extern char *src;
> void f(void) {
> // warning: 'strncpy' specified bound 12 equals destination size
> [-Wstringop-truncation]
> strncpy(dst, src, sizeof(dst));
> }
>
> Example 2: this does warn. It seems it is the same case as example 1,
> but now you know the actual (array) size of src so you can give more
> details. Still, I think in example 1 we should put the same
> explanation as here: i.e. something like "if len(src) >= 12, then dst
> might truncated", no? (assuming I am understanding why you warn on
> Example 1).
>
> extern char dst[12];
> extern char src[13];
> void f(void) {
> // warning: 'strncpy' output may be truncated copying 12 bytes from a
> string of length 12 [-Wstringop-truncation]
> strncpy(dst, src, sizeof(dst));
> }
My response was:
I'm not sure off the top of my head what more to say but let me see if I can
come up with better/clearer wording, maybe in a note.
In Example 2, we warn for the same reason as in Example 1 where we know
the size of the source array. In Example 2 we don't, but we could certainly
reword the warning along similar lines. Let me think about that also.