From 08ddab21215fba57be1ebdb495b9374953a6b679 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Schl=C3=BCter?= Date: Sat, 13 Oct 2007 23:43:49 +0200 Subject: [PATCH] re PR fortran/33254 (Diagnose different string lengths in array constructors at run time) 2007-10-13 Tobias Schlueter Paul Thomas PR fortran/33254 PR fortran/33727 fortran/ * trans-array.c (get_array_ctor_var_strlen): Check upper bound for constness instead of lower bound. (get_array_ctor_strlen): Add bounds-checking code. testsuite/ * bounds_check_10.f90: New. Co-Authored-By: Paul Thomas From-SVN: r129286 --- gcc/fortran/ChangeLog | 9 +++++++++ gcc/fortran/trans-array.c | 20 ++++++++++++++++++- gcc/testsuite/ChangeLog | 7 +++++++ gcc/testsuite/gfortran.dg/bounds_check_10.f90 | 15 ++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_10.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 92a757096bf..eddaa91fb09 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,12 @@ +2007-10-13 Tobias Schlüter + Paul Thomas + + PR fortran/33254 + PR fortran/33727 + * trans-array.c (get_array_ctor_var_strlen): Check upper bound for + constness instead of lower bound. + (get_array_ctor_strlen): Add bounds-checking code. + 2007-10-12 Paul Thomas PR fortran/33542 diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c index 2edc95b0572..4fb1fdaab53 100644 --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -1340,7 +1340,7 @@ get_array_ctor_var_strlen (gfc_expr * expr, tree * len) case REF_SUBSTRING: if (ref->u.ss.start->expr_type != EXPR_CONSTANT - || ref->u.ss.start->expr_type != EXPR_CONSTANT) + || ref->u.ss.end->expr_type != EXPR_CONSTANT) break; mpz_init_set_ui (char_len, 1); mpz_add (char_len, char_len, ref->u.ss.end->value.integer); @@ -1413,6 +1413,7 @@ bool get_array_ctor_strlen (stmtblock_t *block, gfc_constructor * c, tree * len) { bool is_const; + tree first_len = NULL_TREE; is_const = TRUE; @@ -1447,6 +1448,23 @@ get_array_ctor_strlen (stmtblock_t *block, gfc_constructor * c, tree * len) get_array_ctor_all_strlen (block, c->expr, len); break; } + if (flag_bounds_check) + { + if (!first_len) + first_len = *len; + else + { + /* Verify that all constructor elements are of the same + length. */ + tree cond = fold_build2 (NE_EXPR, boolean_type_node, + first_len, *len); + gfc_trans_runtime_check + (cond, block, &c->expr->where, + "Different CHARACTER lengths (%ld/%ld) in array constructor", + fold_convert (long_integer_type_node, first_len), + fold_convert (long_integer_type_node, *len)); + } + } } return is_const; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7cd56c396b2..83f025c9a80 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2007-10-13 Tobias Schlüter + Paul Thomas + + PR fortran/33254 + PR fortran/33727 + * bounds_check_10.f90: New. + 2007-10-13 David Edelsohn * gcc.target/powerpc/parity-1.c: POWER5 feature, not POWER6. diff --git a/gcc/testsuite/gfortran.dg/bounds_check_10.f90 b/gcc/testsuite/gfortran.dg/bounds_check_10.f90 new file mode 100644 index 00000000000..02101afeacb --- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_10.f90 @@ -0,0 +1,15 @@ +! { dg-do run } +! { dg-options "-fbounds-check" } +! { dg-shouldfail "Different CHARACTER lengths" } +! PR fortran/33254: No bounds checking for array constructors +program array_char +implicit none +character (len=2) :: x, y +character (len=2) :: z(3) +x = "a " +y = "cd" +z = [y(1:1), x(1:len(trim(x)))] ! should work +z = [trim(x), trim(y), "aaaa"] ! [ "a", "cd", "aaaa" ] should catch first error +end program array_char + +! { dg-output "Different CHARACTER lengths .1/2. in array constructor" } -- 2.30.2