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

            Bug ID: 94580
           Summary: missing warning accessing an interior flexible array
                    member
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

In the following (invalid) test case -Warray-bounds only diagnoses one of the
four out-of-bounds references, although it eliminates the seemingly redundant
stores on the (clearly incorrect in this case) assumption that they don't alias
with the reads.  It's fine to eliminate the stores (or take whatever other
reasonable action) as long as the out-of-bounds accesses are diagnosed.

GCC warns about declarations of interior flexible array members, but only with
-Wpedantic, so this kind of bugs can easily go undetected.

$ cat t.c && gcc -O2 -S -Wall -Wextra -fdump-tree-vrp=/dev/stdout t.c
struct A { int n, a[]; };
struct B { struct A a; int x; };

struct B b;

int f0 (void)
{
  b.a.a[1] = 1;   // missing -Warray-bounds (store eliminated)
  int t = b.x;
  b.a.a[1] = 0;   // -Warray-bounds (good)
  return b.x - t;
}

int f1 (struct B *p)
{
  p->a.a[1] = 1;  // missing -Warray-bounds (store eliminated)
  int t = p->x;
  p->a.a[1] = 0;  // missing -Warray-bounds
  return p->x - t;
}


;; Function f0 (f0, funcdef_no=0, decl_uid=1937, cgraph_uid=1, symbol_order=1)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }

Value ranges after VRP:



t.c: In function ‘f0’:
t.c:10:8: warning: array subscript 1 is above array bounds of ‘int[0]’
[-Warray-bounds]
   10 |   b.a.a[1] = 0;   // -Warray-bounds (good)
      |   ~~~~~^~~
t.c:1:19: note: while referencing ‘a’
    1 | struct A { int n, a[]; };
      |                   ^
f0 ()
{
  <bb 2> [local count: 1073741824]:
  b.a.a[1] = 0;
  return 0;

}



;; Function f0 (f0, funcdef_no=0, decl_uid=1937, cgraph_uid=1, symbol_order=1)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }

Value ranges after VRP:



f0 ()
{
  <bb 2> [local count: 1073741824]:
  b.a.a[1] = 0;
  return 0;

}



;; Function f1 (f1, funcdef_no=1, decl_uid=1941, cgraph_uid=2, symbol_order=2)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }

Value ranges after VRP:



f1 (struct B * p)
{
  <bb 2> [local count: 1073741824]:
  p_2(D)->a.a[1] = 0;
  return 0;

}



;; Function f1 (f1, funcdef_no=1, decl_uid=1941, cgraph_uid=2, symbol_order=2)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }

Value ranges after VRP:



f1 (struct B * p)
{
  <bb 2> [local count: 1073741824]:
  p_2(D)->a.a[1] = 0;
  return 0;

}

Reply via email to