From patchwork Mon Sep 18 20:27:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 141562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2933398vqi; Mon, 18 Sep 2023 13:27:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFc9xFGAC8qpzhAJVT/jj2yU5FCjbYgzti++cODqgPxoqETe5ZB4aj1jcGipRY/qk+KBnoB X-Received: by 2002:a17:906:116:b0:9ad:ef08:1f32 with SMTP id 22-20020a170906011600b009adef081f32mr6883295eje.37.1695068876699; Mon, 18 Sep 2023 13:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695068876; cv=none; d=google.com; s=arc-20160816; b=p0a8sisnNyl1gZi8KwqIxlsBJl4D40C8r25mCg2UH5P/eCtiAJ7jSua2pxq9uKWzJf rzl99lfh1rRflpdv87FhL1AiZ1cUbqJywMAUcOwNOubDVpD3yDi/fafqK/Wn7Fi7eiTE uecBjDhL5SAhcd+jcnol+gmXE7wFwJnZKFIiTXdjTIGubWli+TFAuZan9zcbhyFM3Rvy wMjB7k26nxtXXnEthi/NpIA1I35BJIwMqfQSkpz80PSlB5mLHWavZ7kTBIq8Tza1JbNh +MuA7HqjGUWVCyOKvg3/xRVT7m0IAAeG3ebb/YmMlNzU7bFdyfeRiH1rcNA0CtPLYZMT 7Lig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ok70MD87EaJvgro/mZ8NSpTQm5mGV+R7KKTmKRepZ48=; fh=+IEfvAe+9BRgPHWhQEl2uIBTtAiiGDh1ExRZeB5JJoc=; b=h6rCqGD7xTonl3aafnfpa6Y4aHzgMiUamM84d6iQYZynvlEjfUKtBTD426XE9jAd7h BIRhsJ1kFpU0r6/zvthfBf27DB396nKYFBXl7ys8YYyvYIZKeO1Ca5BnqhWzsc5G/2WW XL3kqHungI4/GrwM5x5WYtfmc22ToSfHmsDfrrb/N2fAbt1s7RO85/y0aRCK4SwYe5KE PJ81vzZpgb9I48bIlZhPaSW8NqGv4xo0OairCEn5tY6+iAWtnjDDcgJ+FtbEvcWQ/ygg GiPh8qf/s5kzb0OYWB/dpFwAizi69h9+Y6JNC64mBtQEYVAhAbpaRme2xJxITt+2K43g r8vA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Vm1oS5t2; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l11-20020a170906a40b00b0099367e9043dsi8668417ejz.870.2023.09.18.13.27.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 13:27:56 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Vm1oS5t2; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 810F73858C2A for ; Mon, 18 Sep 2023 20:27:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 810F73858C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695068873; bh=ok70MD87EaJvgro/mZ8NSpTQm5mGV+R7KKTmKRepZ48=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Vm1oS5t208Nm6QGSU4t2Vx4bAjG6/+VFl9QUwjsG5Std4veGaufewGVVOpH0lOduO 1XbI6NUx77cdaiICl1zAPVYS0Vf1+Co3Me4WSF2Ve9hJdBbwGfKUCi+uwyxwcGzpIu Y2u5Ft8+oG1VxGi/HjcATqG/FmgIhOoprOGUIJvA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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: To: fortran , gcc-patches Subject: [PATCH] fortran: fix checking of CHARACTER lengths in array constructors [PR70231] Date: Mon, 18 Sep 2023 22:27:01 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:JtjAU3oHihozll0lhPSFECczlveLAzV/CwrGiNpPhL+Sy9sR+7k7/VLcqI0sd3YSyjByX lb2D8IT/jEmjlgfYKc9iJZtJ+STa7w2hw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U/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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777408542486204553 X-GMAIL-MSGID: 1777408542486204553 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 From 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. --- 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 --- 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 00000000000..6a8dafc27a8 --- /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 -- 2.35.3