From d912240d907b6b595e27fc8c3cf027dfc10dd359 Mon Sep 17 00:00:00 2001 From: Francois-Xavier Coudert Date: Sat, 24 Mar 2007 20:19:51 +0000 Subject: [PATCH] re PR fortran/30655 (Undue out-of-bounds warning) PR fortran/30655 * expr.c (check_dimension): Fix logic of comparisons. * gfortran.dg/bounds_check_6.f90: New test. From-SVN: r123187 --- gcc/fortran/ChangeLog | 5 ++ gcc/fortran/resolve.c | 71 +++++++++++--------- gcc/testsuite/ChangeLog | 5 ++ gcc/testsuite/gfortran.dg/bounds_check_6.f90 | 8 +++ 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_6.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 99c67fc6764a..d99b31f567ac 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,8 @@ +2007-03-24 Francois-Xavier Coudert + + PR fortran/30655 + * expr.c (check_dimension): Fix logic of comparisons. + 2007-03-24 Paul Thomas PR fortran/31215 diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index a72047e3ffb7..164a0cb9020a 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -2507,48 +2507,53 @@ check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as) break; case AR_SECTION: - if (compare_bound_int (ar->stride[i], 0) == CMP_EQ) - { - gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]); - return FAILURE; - } - + { #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i]) #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i]) - if (compare_bound (AR_START, AR_END) == CMP_EQ - && (compare_bound (AR_START, as->lower[i]) == CMP_LT - || compare_bound (AR_START, as->upper[i]) == CMP_GT)) - goto bound; + comparison comp_start_end = compare_bound (AR_START, AR_END); - if (((compare_bound_int (ar->stride[i], 0) == CMP_GT - || ar->stride[i] == NULL) - && compare_bound (AR_START, AR_END) != CMP_GT) - || (compare_bound_int (ar->stride[i], 0) == CMP_LT - && compare_bound (AR_START, AR_END) != CMP_LT)) - { - if (compare_bound (AR_START, as->lower[i]) == CMP_LT) - goto bound; - if (compare_bound (AR_START, as->upper[i]) == CMP_GT) - goto bound; - } + /* Check for zero stride, which is not allowed. */ + if (compare_bound_int (ar->stride[i], 0) == CMP_EQ) + { + gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]); + return FAILURE; + } - mpz_init (last_value); - if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i], - last_value)) - { - if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT - || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT) - { - mpz_clear (last_value); + /* if start == len || (stride > 0 && start < len) + || (stride < 0 && start > len), + then the array section contains at least one element. In this + case, there is an out-of-bounds access if + (start < lower || start > upper). */ + if (compare_bound (AR_START, AR_END) == CMP_EQ + || ((compare_bound_int (ar->stride[i], 0) == CMP_GT + || ar->stride[i] == NULL) && comp_start_end == CMP_LT) + || (compare_bound_int (ar->stride[i], 0) == CMP_LT + && comp_start_end == CMP_GT)) + { + if (compare_bound (AR_START, as->lower[i]) == CMP_LT + || compare_bound (AR_START, as->upper[i]) == CMP_GT) goto bound; - } - } - mpz_clear (last_value); + } + + /* If we can compute the highest index of the array section, + then it also has to be between lower and upper. */ + mpz_init (last_value); + if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i], + last_value)) + { + if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT + || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT) + { + mpz_clear (last_value); + goto bound; + } + } + mpz_clear (last_value); #undef AR_START #undef AR_END - + } break; default: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 39365b756b54..e8650e49b798 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2007-03-24 Francois-Xavier Coudert + + PR fortran/30655 + * gfortran.dg/bounds_check_6.f90: New test. + 2007-03-23 Michael Meissner * gcc.dg/dfp/convert-dfp.c: Wrap __STDC_WANT_DEC_FP__ with diff --git a/gcc/testsuite/gfortran.dg/bounds_check_6.f90 b/gcc/testsuite/gfortran.dg/bounds_check_6.f90 new file mode 100644 index 000000000000..6535db760052 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_6.f90 @@ -0,0 +1,8 @@ +! { dg-do run } +! { dg-options "-fbounds-check" } +! +! Testcase for PR30655, we used to issue a compile-time warning + integer i(12), j + j = -1 + i(0:j) = 42 + end