public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94580] New: missing warning accessing an interior flexible array member
@ 2020-04-13 17:46 msebor at gcc dot gnu.org
  2020-04-13 17:48 ` [Bug middle-end/94580] " msebor at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-04-13 17:46 UTC (permalink / raw)
  To: gcc-bugs

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;

}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug middle-end/94580] missing warning accessing an interior flexible array member
  2020-04-13 17:46 [Bug middle-end/94580] New: missing warning accessing an interior flexible array member msebor at gcc dot gnu.org
@ 2020-04-13 17:48 ` msebor at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-04-13 17:48 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Blocks|                            |56456

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
GCC does a little better with zero-length arrays:

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

struct B b;

int f0 (void)
{
  b.a.a[1] = 1;
  int t = b.x;
  b.a.a[1] = 0;
  return b.x - t;
}

int f1 (struct B *p)
{
  p->a.a[1] = 1;
  int t = p->x;
  p->a.a[1] = 0;
  return p->x - t;
}

        .file   "t.c"
        .text
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;
      |   ~~~~~^~~
t.c:1:19: note: while referencing ‘a’
    1 | struct A { int n, a[0]; };
      |                   ^
        .p2align 4
        .globl  f0
        .type   f0, @function
f0:
.LFB0:
        .cfi_startproc
        movl    $0, b+8(%rip)
        xorl    %eax, %eax
        ret
        .cfi_endproc
.LFE0:
        .size   f0, .-f0
t.c: In function ‘f1’:
t.c:18:9: warning: array subscript 1 is outside the bounds of an interior
zero-length array ‘int[0]’ [-Wzero-length-bounds]
   18 |   p->a.a[1] = 0;
      |   ~~~~~~^~~
t.c:1:19: note: while referencing ‘a’
    1 | struct A { int n, a[0]; };
      |                   ^
        .p2align 4
        .globl  f1
        .type   f1, @function
f1:
.LFB1:
        .cfi_startproc
        movl    $0, 8(%rdi)
        xorl    %eax, %eax
        ret
        .cfi_endproc
.LFE1:
        .size   f1, .-f1
        .globl  b
        .bss
        .align 8
        .type   b, @object
        .size   b, 8
b:
        .zero   8
        .ident  "GCC: (GNU) 10.0.1 20200413 (experimental)"
        .section        .note.GNU-stack,"",@progbits


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-04-13 17:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-13 17:46 [Bug middle-end/94580] New: missing warning accessing an interior flexible array member msebor at gcc dot gnu.org
2020-04-13 17:48 ` [Bug middle-end/94580] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).