aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarald Anlauf <anlauf@gmx.de>2023-09-18 22:11:40 +0200
committerHarald Anlauf <anlauf@gmx.de>2023-09-19 19:02:16 +0200
commit15acabb80f91b12836bcf4ffcb92ea10fe4e8272 (patch)
tree792857936e925f1237d0032f0a1a0dcb82233a68
parent36eec7995b4d53083c3ee7824bd765b5eba8b1a1 (diff)
downloadgcc-15acabb80f91b12836bcf4ffcb92ea10fe4e8272.zip
gcc-15acabb80f91b12836bcf4ffcb92ea10fe4e8272.tar.gz
gcc-15acabb80f91b12836bcf4ffcb92ea10fe4e8272.tar.bz2
fortran: fix checking of CHARACTER lengths in array constructors [PR70231]
gcc/fortran/ChangeLog: PR fortran/70231 * trans-array.cc (trans_array_constructor): In absence of a typespec, use string length determined by get_array_ctor_strlen() to reasonably initialize auxiliary variable for bounds-checking. gcc/testsuite/ChangeLog: PR fortran/70231 * gfortran.dg/bounds_check_fail_7.f90: New test.
-rw-r--r--gcc/fortran/trans-array.cc17
-rw-r--r--gcc/testsuite/gfortran.dg/bounds_check_fail_7.f9020
2 files changed, 37 insertions, 0 deletions
diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 1640587..e0fc8eb 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -2852,6 +2852,23 @@ trans_array_constructor (gfc_ss * ss, locus * where)
const_string = get_array_ctor_strlen (&outer_loop->pre, c,
&ss_info->string_length);
force_new_cl = true;
+
+ /* Initialize "len" with string length for bounds checking. */
+ if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
+ && !typespec_chararray_ctor
+ && ss_info->string_length)
+ {
+ gfc_se length_se;
+
+ gfc_init_se (&length_se, NULL);
+ gfc_add_modify (&length_se.pre, first_len_val,
+ fold_convert (TREE_TYPE (first_len_val),
+ ss_info->string_length));
+ ss_info->string_length = gfc_evaluate_now (ss_info->string_length,
+ &length_se.pre);
+ gfc_add_block_to_block (&outer_loop->pre, &length_se.pre);
+ gfc_add_block_to_block (&outer_loop->post, &length_se.post);
+ }
}
/* Complex character array constructors should have been taken care of
diff --git a/gcc/testsuite/gfortran.dg/bounds_check_fail_7.f90 b/gcc/testsuite/gfortran.dg/bounds_check_fail_7.f90
new file mode 100644
index 0000000..6a8dafc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bounds_check_fail_7.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+! { dg-additional-options "-fcheck=bounds -g" }
+! { dg-output "At line 18 .*" }
+! { dg-shouldfail "Different CHARACTER lengths (32/0) in array constructor" }
+!
+! PR fortran/70231 - CHARACTER lengths in array constructors
+
+program p
+ implicit none
+ integer, parameter :: char_len = 32
+ integer :: l = 0
+ character(char_len) :: ch = "a"
+ character(char_len), allocatable :: ch_array(:), res1(:), res2(:)
+
+ allocate(ch_array(0))
+ res1 = [ ch_array, ch ] ! was false positive
+ print *, res1
+ res2 = [[ch_array, ch(1:l)], ch(1:l)] ! was false negative on x86
+ print *, res2
+end