From 21150aba6ac0b00ce22b01a8d6104e5ea4e7c6ec Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 23 Nov 2018 10:12:16 +0100 Subject: [PATCH] re PR tree-optimization/86614 (duplicate -Warray-bounds for a strncpy call with out-of-bounds offset) PR tree-optimization/86614 * gimple-ssa-warn-restrict.c (maybe_diag_offset_bounds): Return early if TREE_NO_WARNING is set on ref.ref. * c-c++-common/Warray-bounds-2.c (wrap_strncpy_dstarray_diff_neg, call_strncpy_dstarray_diff_neg): Don't expect late -Warray-bounds warnings, just early ones from FE. Remove dg-prune-output. * c-c++-common/Warray-bounds-6.c: New test. From-SVN: r266403 --- gcc/ChangeLog | 6 ++++++ gcc/gimple-ssa-warn-restrict.c | 3 +++ gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/c-c++-common/Warray-bounds-2.c | 10 ++++------ gcc/testsuite/c-c++-common/Warray-bounds-6.c | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Warray-bounds-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 508682f86eb..29ac649d678 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-11-23 Jakub Jelinek + + PR tree-optimization/86614 + * gimple-ssa-warn-restrict.c (maybe_diag_offset_bounds): Return early + if TREE_NO_WARNING is set on ref.ref. + 2018-11-22 Sandra Loosemore Alan Coopersmith diff --git a/gcc/gimple-ssa-warn-restrict.c b/gcc/gimple-ssa-warn-restrict.c index e3cbf42f2d9..035fbf88c11 100644 --- a/gcc/gimple-ssa-warn-restrict.c +++ b/gcc/gimple-ssa-warn-restrict.c @@ -1582,6 +1582,9 @@ maybe_diag_offset_bounds (location_t loc, gimple *call, tree func, int strict, if (!warn_array_bounds) return false; + if (ref.ref && TREE_NO_WARNING (ref.ref)) + return false; + offset_int ooboff[] = { ref.offrange[0], ref.offrange[1] }; tree oobref = ref.offset_out_of_bounds (strict, ooboff); if (!oobref) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 47cba2d35c6..355221c3812 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2018-11-23 Jakub Jelinek + PR tree-optimization/86614 + * c-c++-common/Warray-bounds-2.c (wrap_strncpy_dstarray_diff_neg, + call_strncpy_dstarray_diff_neg): Don't expect late -Warray-bounds + warnings, just early ones from FE. Remove dg-prune-output. + * c-c++-common/Warray-bounds-6.c: New test. + * gfortran.dg/pad_source_1.f: New test. * gfortran.dg/pad_source_2.f: New test. * gfortran.dg/pad_source_3.f: New test. diff --git a/gcc/testsuite/c-c++-common/Warray-bounds-2.c b/gcc/testsuite/c-c++-common/Warray-bounds-2.c index da5f64df9b7..aee35617bac 100644 --- a/gcc/testsuite/c-c++-common/Warray-bounds-2.c +++ b/gcc/testsuite/c-c++-common/Warray-bounds-2.c @@ -201,18 +201,16 @@ void call_strncpy_dst_diff_max (const char *s, size_t n) static void wrap_strncpy_dstarray_diff_neg (char *d, const char *s, ptrdiff_t i, size_t n) { - strncpy (d + i, s, n); /* { dg-warning "offset -\[0-9\]+ is out of the bounds \\\[0, 90] of object .ar10. with type .(struct )?Array ?\\\[2]." "strncpy" } */ -} + strncpy (d + i, s, n); /* { dg-bogus "offset -\[0-9\]+ is out of the bounds \\\[0, 90] of object .ar10. with type .(struct )?Array ?\\\[2]." "strncpy" } */ +} /* { dg-warning "array subscript -1 is outside array bounds" "" { target *-*-* } .-1 } */ void call_strncpy_dstarray_diff_neg (const char *s, size_t n) { - struct Array ar10[2]; /* { dg-message ".ar10. declared here" } */ - sink (&ar10); + struct Array ar10[2]; /* { dg-bogus ".ar10. declared here" } */ + sink (&ar10); /* { dg-message "while referencing" "" { target *-*-* } .-1 } */ int off = (char*)ar10[1].a17 - (char*)ar10 + 1; wrap_strncpy_dstarray_diff_neg (ar10[1].a17, s, -off, n); sink (&ar10); } - -/* { dg-prune-output "outside array bounds" } */ diff --git a/gcc/testsuite/c-c++-common/Warray-bounds-6.c b/gcc/testsuite/c-c++-common/Warray-bounds-6.c new file mode 100644 index 00000000000..6611d5cd916 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Warray-bounds-6.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/86614 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Warray-bounds" } */ + +extern char *strncpy (char *, const char *, __SIZE_TYPE__); + +void sink (void *); + +struct A { char b[17]; } a[2]; + +void g (const char *s, unsigned n) +{ + int i = (char *)a[1].b - (char *)a + 1; + char *d = a[1].b; + /* Ensure the same bug is not diagnosed more than once. */ + strncpy (d + i, s, n); /* { dg-warning "array subscript \[0-9]+ is outside array bounds of" } */ + /* { dg-bogus "offset \[0-9]+ is out of the bounds \\\[0, \[0-9]+\\\] of object 'a' with type" "" { target *-*-* } .-1 } */ +} -- 2.30.2