Bug 92307 - missing -Wstringop-overflow on a memcpy into an array with out-of-bounds variable offset
Summary: missing -Wstringop-overflow on a memcpy into an array with out-of-bounds vari...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: 10.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic
Depends on:
Blocks: Wstringop-overflow
  Show dependency treegraph
 
Reported: 2019-10-31 16:37 UTC by Martin Sebor
Modified: 2020-04-13 21:38 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 10.0
Known to fail: 8.3.0, 9.2.0
Last reconfirmed: 2019-10-31 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2019-10-31 16:37:42 UTC
Even with PR89427 resolved, GCC still fails to detect the invalid accesses in the functions below (_FORTIFY_SOURCE doesn't help because it doesn't try to detect sizes from pointers involving variable offsets):

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

void f (int i, const char *s)
{
  if (i < 1 || 2 < i) i = 1;
  char *p = &a[i] - 9;
  __builtin_memcpy (p, s, 2);   // writing before the beginning of a
}

void g (int i, const char *s)
{
  if (i < 1 || 2 < i) i = 1;
  char *p = &a[i] + 9;
  __builtin_memcpy (p, s, 2);   // writing past the end of a
}
Comment 1 Martin Sebor 2019-10-31 16:40:37 UTC
I'm testing a patch that diagnoses this.
Comment 2 Martin Sebor 2020-04-13 21:38:12 UTC
Fixed in GCC 10 via r279248 which now prints:

pr92307.c: In function ‘f’:
pr92307.c:2:3: warning: writing 2 bytes into a region of size 1 [-Wstringop-overflow=]
    2 |   __builtin_memcpy (d, s, n)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
pr92307.c:10:3: note: in expansion of macro ‘memcpy’
   10 |   memcpy (p, s, 2);   // writing before the beginning of a
      |   ^~~~~~
pr92307.c:4:6: note: at offset 0 to object ‘a’ with size 2 declared here
    4 | char a[2];
      |      ^
pr92307.c: In function ‘g’:
pr92307.c:2:3: warning: writing 2 bytes into a region of size 0 [-Wstringop-overflow=]
    2 |   __builtin_memcpy (d, s, n)
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
pr92307.c:17:3: note: in expansion of macro ‘memcpy’
   17 |   memcpy (p, s, 2);   // writing past the end of a
      |   ^~~~~~
pr92307.c:4:6: note: at offset 0 to object ‘a’ with size 2 declared here
    4 | char a[2];
      |      ^