From patchwork Wed Oct 30 16:41:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Richard Thomas X-Patchwork-Id: 99833 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8A9F385843D for ; Wed, 30 Oct 2024 16:42:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id D257E3858D28; Wed, 30 Oct 2024 16:41:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D257E3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D257E3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::535 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730306518; cv=none; b=wKIdvTzUrtezyQrfmlF5OCVGSnPd58wkgnOMWMNgO4Bit5qk1sd/oIYq86QKK76NY0HyUjCb4jDfsP03MNzpftvXJ4N1vv76N/G3TOyfEP90kzQp5i9wc8lVhLGH9yYZejPuR2PhaZEPs0p26ql1BIedp+3jKlbrf1/XbAROxP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730306518; c=relaxed/simple; bh=wVWGWmdm2MMbcv1vZYiofHRL49h0+MD1T3+t5oFT8k0=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=xqOBvAjMFJc1oKUlnumHk/EcXvW0Y84fN0yys4gvOVWsAlqLiHGXjpZnPhdNrixvoaMNVOJnQp3WzCYTECgn3QtgTtCrffrF+ME9iFkRS5bT78zJogQMefFYpA3NfOVwTRfOOOxMhvPltFYVFq/TVfathdAkYHR0kMhINgRId8w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-7e6d04f74faso19532a12.1; Wed, 30 Oct 2024 09:41:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730306512; x=1730911312; darn=gcc.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=wVWGWmdm2MMbcv1vZYiofHRL49h0+MD1T3+t5oFT8k0=; b=TKBtwR+AuF4c7N2vNCb1rI1pOHSEnzDnr4NaIvnMGCc5qP3zo0CiZWeDnXtT8oBHLG HMbfKDDHFYKsObMRCpxEa5AnAPXrRLwD/Z3wObCJXcD2oBnnpsnO9DmH8zKg0w5jJRhd n8qOWKiSLX14mOm13Scged202hGT25fRqUvm0Sa34VI17TMeRXS42xmwe57GB1GqOOAY +9QH5hLo034KmDvgLcmGAo6zPc5GKjOZOjRt0jQD9uhyyOI/EolI8Sae1NT7Dk/beVOe ciKEAFzomCctgT3+/OFaFKNJ7s8T9LVUy5Tz4ClF6CDV/ddC3tyLkM5uSUknhy2jwlIM 6u8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730306512; x=1730911312; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=wVWGWmdm2MMbcv1vZYiofHRL49h0+MD1T3+t5oFT8k0=; b=m1iEty8pZ7IwXOlrgA2RmnkjkvEyqk+ZCtCWYKkKVSa8BHYVeyIzYFuJXwkOsNykr+ 6vUSXiM+O6ZS6E3HKfeBGcl4qAj2BysjholVf48esYOHBZLGHCSCDWvbWL+Pvt+wtYxV tSNCxgtXD2esTpw5qXjcX9LAkRo8Ov3vdAwFuLmIo2+IsKJPBa48vX3FxdchcHIYa5mE 3uycQzF8E+ff29sj7/rwiNaKw76wQhviNkHn+RP6MyoQbziSnG9eSMCqb/peM0ywqkEM Mo3ECDo8UtAs6npUjmP7MHgWMliVsRZvgjOb1UwgozbBTqic9Zt+I750i7TXM53VeL5y S2RA== X-Forwarded-Encrypted: i=1; AJvYcCUAhiLNtOylu550uVI2ndY+E33RaAPLKqcfNh4KMDZ5y5NLF55YUiTe+H6+fnbzsQJPnNLpBsbozMSoKw==@gcc.gnu.org X-Gm-Message-State: AOJu0YwZbWc+0LuNFTb00wyritiobjKL3bZ57Hubl1kwenGjzm3BkRfI D1Acdh0jIifibO8R9c6n04Ejd8Ex+rEWJeC8GM/JLxJzOD7mb024D40Vhb4yrQ79vOlgs2FNfvz p3PfvBtZN4dWKcUqsEnWolPvGdnn12g== X-Google-Smtp-Source: AGHT+IHcODz/nPUtUKPbWsFAO/y0VlYoD9sH6JBvWT3btydqQ+a1wsIvJ4UPrAM3sKB29zDFWYb0M4tvoDV4gaZbQ0o= X-Received: by 2002:a05:6a20:cf8c:b0:1d8:fd0f:c35b with SMTP id adf61e73a8af0-1d9e1f7170bmr9470609637.16.1730306512294; Wed, 30 Oct 2024 09:41:52 -0700 (PDT) MIME-Version: 1.0 From: Paul Richard Thomas Date: Wed, 30 Oct 2024 16:41:40 +0000 Message-ID: Subject: [Patch, fortran] PR115700 - comment 5: uninitialized string length in ASSOCIATE To: "fortran@gcc.gnu.org" , gcc-patches , Harald Anlauf X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, 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: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org This wrinkle to PR115700 came about because the associate-name string length was not being initialized, when an array selector had a substring reference with non-constant start or end. This, of course, caused subsequent references to fail. The ChangeLog provides an adequate explanation of the attached patch. OK for mainline and backporting to 14-branch? Paul diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index 565d4aa5fe9..8045deddd8a 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -6153,6 +6153,15 @@ resolve_variable (gfc_expr *e) e->ref = newref; } } + else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred) + { + gfc_ref *ref; + for (ref = e->ref; ref; ref = ref->next) + if (ref->type == REF_SUBSTRING) + break; + if (ref == NULL) + e->ts = sym->ts; + } if (e->ref && !gfc_resolve_ref (e)) return false; @@ -9871,6 +9880,15 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target) /* Fix up the type-spec for CHARACTER types. */ if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary) { + gfc_ref *ref; + for (ref = target->ref; ref; ref = ref->next) + if (ref->type == REF_SUBSTRING + && ((ref->u.ss.start + && ref->u.ss.start->expr_type != EXPR_CONSTANT) + || (ref->u.ss.end + && ref->u.ss.end->expr_type != EXPR_CONSTANT))) + break; + if (!sym->ts.u.cl) sym->ts.u.cl = target->ts.u.cl; @@ -9889,9 +9907,10 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target) gfc_get_int_expr (gfc_charlen_int_kind, NULL, target->value.character.length); } - else if ((!sym->ts.u.cl->length - || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT) + else if (((!sym->ts.u.cl->length + || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT) && target->expr_type != EXPR_VARIABLE) + || ref) { if (!sym->ts.deferred) { @@ -9901,7 +9920,10 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target) /* This is reset in trans-stmt.cc after the assignment of the target expression to the associate name. */ - sym->attr.allocatable = 1; + if (ref && sym->as) + sym->attr.pointer = 1; + else + sym->attr.allocatable = 1; } } @@ -11508,8 +11530,9 @@ resolve_block_construct (gfc_code* code) { gfc_namespace *ns = code->ext.block.ns; - /* For an ASSOCIATE block, the associations (and their targets) are already - resolved during resolve_symbol. Resolve the BLOCK's namespace. */ + /* For an ASSOCIATE block, the associations (and their targets) will be + resolved by gfc_resolve_symbol, during resolution of the BLOCK's + namespace. */ gfc_resolve (ns); } diff --git a/gcc/testsuite/gfortran.dg/associate_70.f90 b/gcc/testsuite/gfortran.dg/associate_70.f90 new file mode 100644 index 00000000000..397754c0b52 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/associate_70.f90 @@ -0,0 +1,40 @@ +! { dg-do run } +! ( dg-options "-Wuninitialized" ) +! +! Test fix for PR115700 comment 5, in which ‘.tmp4’ is used uninitialized and +! both normal and scalarized array references did not work correctly. +! +! Contributed by Harald Anlauf +! + character(4), dimension(3) :: chr = ['abcd', 'efgh', 'ijkl'] + call mvce (chr) + if (any (chr /= ['ABcd', 'EFgh', 'IJkl'])) stop 1 +contains + subroutine mvce(x) + implicit none + character(len=*), dimension(:), intent(inOUT), target :: x + integer :: i + i = len(x) + +! This was broken + associate (tmp1 => x(:)(1:i/2)) + if (len (tmp1) /= i/2) stop 2 + if (tmp1(2) /= 'ef') stop 3 + if (any (tmp1 /= ['ab', 'ef', 'ij'])) stop 4 + tmp1 = ['AB','EF','IJ'] + end associate + +! Retest things that worked previously. + associate (tmp2 => x(:)(1:2)) + if (len (tmp2) /= i/2) stop 5 + if (tmp2(2) /= 'EF') stop 6 + if (any (tmp2 /= ['AB','EF','IJ'])) stop 7 + end associate + + associate (tmp3 => x(3)(1:i/2)) + if (len (tmp3) /= i/2) stop 8 + if (tmp3 /= 'IJ') stop 9 + end associate + + end subroutine mvce +end