Bug 84053 - [9 Regression] missing -Warray-bounds accessing a local array across inlined function boundaries
Summary: [9 Regression] missing -Warray-bounds accessing a local array across inlined ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 8.0
: P2 normal
Target Milestone: 10.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2018-01-26 03:45 UTC by Martin Sebor
Modified: 2022-05-27 08:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.6.3
Known to fail: 4.7.4, 4.8.4, 4.9.4, 5.5.0, 6.4.0, 7.2.0, 8.0, 9.0, 9.5.0
Last reconfirmed: 2018-01-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2018-01-26 03:45:31 UTC
Prior to version 4.7, GCC would diagnose out-of-bounds accesses to local arrays across inlined function boundaries.  The test case below shows that these invalid accesses are no longer diagnosed.

int f (int i)
{
  int a[] = { 1, 2 };

  if (i < 3)
    i = 3;

  return a[i];   // -Warray-bounds (good)
}

int g (const int *p, int i)
{
  return p[i];
}

int h (int i)
{
  int a[] = { 2, 3 };

  if (i < 3)
    i = 3;

  return g (a, i);   // missing -Warray-bounds
}
t.c: In function ‘f’:
t.c:8:11: warning: array subscript 3 is above array bounds of ‘int[2]’ [-Warray-bounds]
   return a[i];   // -Warray-bounds (good)
          ~^~~
Comment 1 Martin Sebor 2018-01-26 03:47:12 UTC
Bisection points to r178312 committed in GCC 4.7 as the change that introduced this regression:

r178312 | rguenth | 2011-08-30 10:06:00 -0400 (Tue, 30 Aug 2011) | 30 lines

2011-08-30  Richard Guenther  <rguenther@suse.de>

	PR middle-end/48571
Comment 2 Aldy Hernandez 2018-01-26 19:05:28 UTC
Confirmed.
Comment 3 Richard Biener 2018-12-20 11:08:55 UTC
Fixed in GCC 9.

> ./cc1 -quiet t.c -O2 -Warray-bounds
t.c: In function ‘h’:
t.c:3:13: warning: array subscript [3, 2147483647] is outside array bounds of ‘int[2]’ [-Warray-bounds]
    3 |     return p[i];
      |            ~^~~
t.c:8:9: note: while referencing ‘a’
    8 |     int a[] = { 2, 3 };
      |         ^
Comment 4 Martin Sebor 2018-12-20 16:05:34 UTC
It was fixed in r262893 but none of the tests verifies that it works.  Let me add one.
Comment 5 Martin Sebor 2018-12-20 16:21:38 UTC
Actually, the new test revealed that it doesn't work in ILP32 even in GCC 9:

$ gcc -O2 -S -Wall -m32 pr84053.c 
pr84053.c: In function ‘f’:
pr84053.c:9:11: warning: array subscript 3 is above array bounds of ‘int[2]’ [-Warray-bounds]
    9 |   return a[i];   // -Warray-bounds (good)
      |          ~^~~
pr84053.c:4:7: note: while referencing ‘a’
    4 |   int a[] = { 1, 2 };
      |       ^

Let me add the test anyway and xfail it for ILP32.
Comment 6 Martin Sebor 2018-12-20 16:25:48 UTC
Author: msebor
Date: Thu Dec 20 16:25:13 2018
New Revision: 267302

URL: https://gcc.gnu.org/viewcvs?rev=267302&root=gcc&view=rev
Log:
PR tree-optimization/84053 - missing -Warray-bounds accessing a local array across inlined function boundaries

gcc/testsuite/ChangeLog:
	* gcc.dg/Warray-bounds-36.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/Warray-bounds-36.c
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 7 Martin Sebor 2018-12-20 16:28:16 UTC
Let me also restore the regression marker for 9 since the regression is still present for ILP32.
Comment 8 Jakub Jelinek 2019-05-03 09:18:36 UTC
GCC 9.1 has been released.
Comment 9 Jakub Jelinek 2019-08-12 08:58:23 UTC
GCC 9.2 has been released.
Comment 10 Jakub Jelinek 2020-03-12 11:58:41 UTC
GCC 9.3.0 has been released, adjusting target milestone.
Comment 11 Richard Biener 2021-06-01 08:09:57 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 12 Richard Biener 2022-05-27 08:15:33 UTC
Fixed for GCC 10.