From 129ef157d4c0f369de9a5966b9f64bf9634e7d83 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 13 Mar 2019 17:19:43 +0000 Subject: [PATCH] PR tree-optimization/89662 - -Warray-bounds ICE on void* arithmetic gcc/ChangeLog: PR tree-optimization/89662 * tree-vrp.c (vrp_prop::check_mem_ref): Avoid assuming every type has a size. gcc/testsuite/ChangeLog: PR tree-optimization/89662 * gcc.dg/Warray-bounds-41.c: New test. From-SVN: r269655 --- gcc/ChangeLog | 6 +++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gcc.dg/Warray-bounds-41.c | 33 +++++++++++++++++++++++++ gcc/tree-vrp.c | 11 ++++++--- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Warray-bounds-41.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 86e210b4e27..cda44b8e0ff 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2019-03-13 Martin Sebor + + PR tree-optimization/89662 + * tree-vrp.c (vrp_prop::check_mem_ref): Avoid assuming every type + has a size. + 2019-03-13 Richard Biener PR middle-end/89677 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index dc261ed89bc..f4dc0a009d9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-03-13 Martin Sebor + + PR tree-optimization/89662 + * gcc.dg/Warray-bounds-41.c: New test. + 2019-03-13 Paolo Carlini PR c++/63508 diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-41.c b/gcc/testsuite/gcc.dg/Warray-bounds-41.c new file mode 100644 index 00000000000..fd795902825 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Warray-bounds-41.c @@ -0,0 +1,33 @@ +/* PR tree-optimization/89662- -Warray-bounds ICE on void* arithmetic + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +void* vptr (void *c) +{ + return c; +} + +void sink (void*); + +void test_vptr_arith_vla_cst (void) +{ + int n = 1; + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" } */ +} + +void test_vptr_arith_vla_range (int n) +{ + if (n < 1 || 4 < n) + return; + + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" "pr82608" { xfail *-*-* } } */ +} + +void test_vptr_arith_vla_var (int n) +{ + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" "pr82608" { xfail *-*-* } } */ +} + diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index bf1d947fbf7..1092fe045e2 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -4718,13 +4718,16 @@ vrp_prop::check_mem_ref (location_t location, tree ref, { /* Extract the element type out of MEM_REF and use its size to compute the index to print in the diagnostic; arrays - in MEM_REF don't mean anything. */ + in MEM_REF don't mean anything. A type with no size like + void is as good as having a size of 1. */ tree type = TREE_TYPE (ref); while (TREE_CODE (type) == ARRAY_TYPE) type = TREE_TYPE (type); - tree size = TYPE_SIZE_UNIT (type); - offrange[0] = offrange[0] / wi::to_offset (size); - offrange[1] = offrange[1] / wi::to_offset (size); + if (tree size = TYPE_SIZE_UNIT (type)) + { + offrange[0] = offrange[0] / wi::to_offset (size); + offrange[1] = offrange[1] / wi::to_offset (size); + } } else { -- 2.30.2