From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id 04DA33856974; Mon, 18 Sep 2023 20:27:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04DA33856974 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1695068821; x=1695673621; i=anlauf@gmx.de; bh=/2dTb1Myfhpn3mAYJZ39nVQsuqsl5foa8RgtbvXSwB4=; h=X-UI-Sender-Class:From:To:Subject:Date; b=ftyI+GwmS+JSjwCbg5VPhZ+Lor5FjDrWzvqM+AhzPs0h6f5pcDoto0GIGX3PN+LOfuoyShUF2mn UBy3OPxG5R7dY25b1Rr6cZVyqw6ebDDXkG1/f716+fqeJQf+BPF/xiCjLYB593PIcT4o5O+6x2GFM cuxCd/jEZPtDOvKIe4pobU9ftTqenjcOZPi00HBNDz7aHGePk52WzqTx+rJXs1TqvvxFNryXfS0Pk CrxD7U6Ii+HPDzaseeLVSWaQoPN72O1rmpv93nZfV/kDdIBkBWmyYZWZjsyKYyuIZVSkwACsZPsTW HH7nTtv1gcg45AMeCs2RtFLC67F1yxDEvhYg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.81.136] ([93.207.81.136]) by web-mail.gmx.net (3c-app-gmx-bap05.server.lan [172.19.172.75]) (via HTTP); Mon, 18 Sep 2023 22:27:01 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] fortran: fix checking of CHARACTER lengths in array constructors [PR70231] Content-Type: multipart/mixed; boundary=kenitram-ca7c06c4-b05f-4232-b267-51f72f3c970a Date: Mon, 18 Sep 2023 22:27:01 +0200 Importance: normal Sensitivity: Normal X-Priority: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E+YYNcqyJlJ8LpZtIpTiNKS94ljkHUQuHtYd3sspUcgkSpw79fDGLLz5WWblMOfMs 2FDxk6vZEACOjRsbFaZn0J9gbZAEBFoBNvkBoSYQF1c6D0GOMGq5U/fI5e1vVkxS03p6xvJf1 Ysrj/fyOZVJrcQD3uMKY5HAoVJy4cmVRLyMqAEx7DmwrI= X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --kenitram-ca7c06c4-b05f-4232-b267-51f72f3c970a Content-Type: text/plain; charset=UTF-8 Dear all, as correctly analyzed by Jerry, the code for checking the consistency of character lengths in array constructors did not properly initialize the auxiliary variable used in "bounds checking". The attached patch resolves this by initializing this auxiliary variable with a length obtained by scanning the constructor. Interestingly, the failure depended on optimization level and was apparent only at -O0, and could exhibit both false-positives and false-negatives. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald --kenitram-ca7c06c4-b05f-4232-b267-51f72f3c970a Content-Type: text/x-patch Content-Disposition: attachment; filename=pr70231.diff Content-Transfer-Encoding: quoted-printable =46rom 14b12cba8b7def5e07a3cac5efd2be8ab49d8133 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 18 Sep 2023 22:11:40 +0200 Subject: [PATCH] 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. =2D-- gcc/fortran/trans-array.cc | 17 ++++++++++++++++ .../gfortran.dg/bounds_check_fail_7.f90 | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_fail_7.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 1640587cd71..e0fc8ebc46b 100644 =2D-- 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 =3D get_array_ctor_strlen (&outer_loop->pre, c, &ss_info->string_length); force_new_cl =3D 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 =3D gfc_evaluate_now (ss_info->string_lengt= h, + &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/tests= uite/gfortran.dg/bounds_check_fail_7.f90 new file mode 100644 index 00000000000..6a8dafc27a8 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_fail_7.f90 @@ -0,0 +1,20 @@ +! { dg-do run } +! { dg-additional-options "-fcheck=3Dbounds -g" } +! { dg-output "At line 18 .*" } +! { dg-shouldfail "Different CHARACTER lengths (32/0) in array constructo= r" } +! +! PR fortran/70231 - CHARACTER lengths in array constructors + +program p + implicit none + integer, parameter :: char_len =3D 32 + integer :: l =3D 0 + character(char_len) :: ch =3D "a" + character(char_len), allocatable :: ch_array(:), res1(:), res2(:) + + allocate(ch_array(0)) + res1 =3D [ ch_array, ch ] ! was false positive + print *, res1 + res2 =3D [[ch_array, ch(1:l)], ch(1:l)] ! was false negative on x86 + print *, res2 +end =2D- 2.35.3 --kenitram-ca7c06c4-b05f-4232-b267-51f72f3c970a--