Bug 84397 - missing -Wstringop-truncation on strncpy into a multidimensional array
Summary: missing -Wstringop-truncation on strncpy into a multidimensional array
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wstringop-truncation
  Show dependency treegraph
 
Reported: 2018-02-15 00:27 UTC by Martin Sebor
Modified: 2019-01-09 23:46 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2018-02-15 00:27:22 UTC
The -Wstringop-truncation detects the potential string truncation in the first strncpy call below but fails to detect the similar problem in the second call.

$ cat a.c && gcc -O2 -S -Wall a.c
char a[2][7];

void f0 (const char *s)
{
  __builtin_strncpy (a[0], s, sizeof a[0]);   // missing -Wstringop-truncation
}

void f1 (const char *s)
{
  __builtin_strncpy (a[1], s, sizeof a[1]);   // -Wstringop-truncation (good)
}

void fi (const char *s, int i)
{
  __builtin_strncpy (a[i], s, sizeof a[i]);   // missing -Wstringop-truncation
}

a.c: In function ‘f1’:
a.c:10:3: warning: ‘__builtin_strncpy’ specified bound 7 equals destination size [-Wstringop-truncation]
   __builtin_strncpy (a[1], s, sizeof a[1]);   // -Wstringop-truncation (good)
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comment 1 Martin Sebor 2018-02-15 00:31:29 UTC
This is probably due to the same underlying problem as bug 84395.  The checker computes the remaining space in the outermost object (via compute_objsize) instead of figuring out the size of the referenced array.