From patchwork Thu Feb 8 21:03:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 198578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp452264dyd; Thu, 8 Feb 2024 13:06:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHX6TAVO6L7trhJ7q1hzJRwWJBGYzv+pOasB48oNyfNucf43owKp2Rf9ugADLauCSf851r X-Received: by 2002:a9d:6e1a:0:b0:6da:61d6:2db9 with SMTP id e26-20020a9d6e1a000000b006da61d62db9mr767766otr.1.1707426392293; Thu, 08 Feb 2024 13:06:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707426392; cv=pass; d=google.com; s=arc-20160816; b=X7/tLkORfeE8VJNPZRLoF01EPsKPTYDN0+qNHVcab+GixX2XpG1qhHmBT32jyEgQO1 uj41X+fIuixhC7v/XAtv1SS3p+xY8EXbKcAEF/GsEnAMmAtmey5vktylbRioWFA4w6ok gzkHBBqGeXnAN8vKqbND5CxaABJ7L1Oy5tfDL8+Yp+35y6U42sXin2VuAUdKmWXWd4O3 VcXUsQMcZbSydvMkjaQTkG4D/RhZr5lqTcSl1gK1+AAHPo4OtgbRxFRc6HKeKYfnqTMV SI1KlqXpls5o7iCnFMdXOKd14QnoNUAND4FJd+GO27yaEgXXwG3rc0czSgY1xza1fPVv YRjA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:ui-outboundreport:sensitivity :importance:date:subject:to:from:message-id:mime-version :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=2/8/sZSOLAMam3XVwVnH/UsZ0NCWvYf35ZPgz4Vdx2o=; fh=w9njIRfNermQP8FD6SBbi40p76YS2ezvIJLXvBEzf5s=; b=RNjRVNBmLu0A4JvEF4C9ow5wNgU/yjMJuT3Ke3f32api4Jzz+KmGFeRFApKNAtwESW ITALSx3qCnQf9wEcwMfWV3X9NEYlPoHPe31xSEoxo4xI79HUh+IBDYF60cHrczAFSm9c OvMV4EDch4XhmNxQRPrKP5PnFh/OR2kbVgpBFx5Nr0mGo+ITCgCdO43uYmILmvY1T/Zp x4dlSAVRTZZIJtaBAV7JSjbvB28jHfpWdendWrZ/PUa+yTmPlEZXv/Ax21OsWDu+zSto P9ZPx/znEy9140HAsIrlK1PI1jvEMNY8dpSmk7zjvpuUz+fp67A67tknIYQPyh1YhcV0 Ll2A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=ZktpnRrW; arc=pass (i=1); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de X-Forwarded-Encrypted: i=2; AJvYcCXqLD4ZqqlySBkXvx6Xq3Yh9UXkboikKc0/qQuQnDyudQjfV1okg+o3W3WCMIy+dGjAIMJsGf+fcfHkKSkG0fv2lng//w== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z6-20020a05622a028600b0042c00d7cc50si350683qtw.52.2024.02.08.13.06.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 13:06:32 -0800 (PST) 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=@gmx.de header.s=s31663417 header.b=ZktpnRrW; arc=pass (i=1); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F28783858C35 for ; Thu, 8 Feb 2024 21:06:31 +0000 (GMT) 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 4E8423858C60; Thu, 8 Feb 2024 21:03:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E8423858C60 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4E8423858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.17.22 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707426210; cv=none; b=IKf3xXSNFJlz79DPqrKkI7vAKauCHFJNbadYodq58HXKMzqlqN/bzrp7UdOJSnWgWLYRe+fJg2FkJM6TZL0t1RRpXtZlyTrP47QaG8B0Qa9eeq3b6b0meJxh2CGRQE21pQj1Y9Ud+A9XZ31Yw4/+Ck59k63KZRzS8scfylBhdFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707426210; c=relaxed/simple; bh=sGLn83PExsViaIscKSCsaFMCF42t1w5HN+T5YuTjLRw=; h=DKIM-Signature:MIME-Version:Message-ID:From:To:Subject:Date; b=TjU2CXfa1chIGE3yW2+UrrQvcuMfm5437OG3FekJj/y2mW7uPIsd1FOI03zOZRhF/BxSiZOgrwMoZtvFh2/b6iMUFOvu9HXv66I1aIjtcN+dpLooSqseVIMLKxOcYYon2MREv0vGYJaYbTeFzY+xn7GMZJKl8GcrIr1KbqGSg8Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1707426201; x=1708031001; i=anlauf@gmx.de; bh=sGLn83PExsViaIscKSCsaFMCF42t1w5HN+T5YuTjLRw=; h=X-UI-Sender-Class:From:To:Subject:Date; b=ZktpnRrWROUVK4fmVNvDtGWrbh3e5CSPtoNRGFi9AaAWn7+Dpw7WADrL+/D9Lj65 AYaksDLc/HyX88YSlB98fIi1u6+DyfkQq4Q5qI2iYImdGE3SkjXkJqHbwvdCdtM3o WeYGEHY7pEo1dsX7s9ZdjExj66eLiTIv4K3W4etpltNear8wzV1CPfqYzUV9RItnn IHhJpD78wrBHO4aQkUg/l7LoD2dnrdh3x1Gd5RVyU75/riqrpSxKVx7lsQqfun+2A pk2o+VmYyeMNFz3oQuvNTycpWm9fds+9B905GkAfsGBjw89SGAQhxCOekx7qOcABO QvnjjlGltXdM+80nDQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.150.45] ([79.232.150.45]) by web-mail.gmx.net (3c-app-gmx-bap20.server.lan [172.19.172.90]) (via HTTP); Thu, 8 Feb 2024 22:03:21 +0100 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: error recovery on arithmetic overflow on unary operations [PR113799] Date: Thu, 8 Feb 2024 22:03:21 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:CdilBxzI93RH+o42tPp65fLvzXEDnYYsqoatXJJmyHWrZP5BjSd+s5RjxFLWzweCP2ueN tSNy8TepqTQ47+a2T++EEeU6t3IumxAnDLYzfBZt/ujo8xgZ5EMkVJqTVcRGxfi9KiAccLa5vx6W RUiIWZpF911NgDf8RcBcx8mIRCP9cgEyM8mW97xLlDtYYLkGCSu3US20ZdA5G4kOVmd72/Go0Oxt 96IMOfL3XUxDIkJNqhJed6VUfWLvs9m54xmgT83CiLMOQQWp69goesU01Vndhb0xwhsfnGYP7y6k q4= UI-OutboundReport: notjunk:1;M01:P0:z5TsI25MkiI=;KU84Ngp0J+oWKIBoWsjTihT88ru Vuotfy6/PMBT8EY2V/mX328/SkxyCM5opv9dtsBVymlcdiQXWKQkuSlRS4ctodDFJHAdBMoaD sF/SILxZpJHY/9o9adcojVGDp2ylharKS+ahDMEe3ZfJosTnhnhPRUu3uRZuTCnwXEd7+lTR8 BGT+Z6kRHcGQf0EnQY0nHlIx1WVmyXwv3ubcNDBwIp0TAMAgCbSzSu13f7YECZRK2OfaeIflv TQr4RBg+0bKuhzAyByPcXZnkouuf229OG0HyX/cjAfdE4RdYm1g89MvlLVAPk0rez9ZPiFN+V f6tfFkHW9yd5gskxJGl0VzSuaD/V+y9p6ZzVZixkuD4D2JaNfXGqEgoz71BSDVtgZkiA5RXsy 8WsBv0qPvrLkH4ueC2cOGWCTMqaC7PDfcCH0lRfjdzlzeWKHbcWqTICS2cEFxbkUwuRKafoxZ 8nKw8+gqGLYQP4wxr6rdjIi6J+UFmdrITtR8Dv+wqbmRVWI4LvmIcIlccME6FDMrMIy/iGmai gEMHB4f/PTukJLwZGh11/bFjBu5FqfJfeERkYg7ZL8pR/NDJqms9iwWZN3IAtdkVsFPMgFHwN j04ouO4XY7OPHfPIBEUtXhaiOag4I+oleBcuwg0mO+cTSgp+kMyFbWZL663ns/3mTXR0daw7+ N2LqqSn7pQuI3mejPucOVkWkK3Ob/GJWAWeixlsBYyiLhhjgLC07c7Mt8wHFbt4= X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790366336944944938 X-GMAIL-MSGID: 1790366336944944938 Dear all, the attached patch improves error recovery when we encounter an array constructor where a unary operator (e.g. minus) is applied and -frange-check is active. The solution is not to terminate early in that case to avoid inconsistencies between check_result and reduce_unary when such a situation occurs. (There might be similar issues for binary operators, not treated here.) Regtested on x86_64-pc-linux-gnu. OK for mainline? The ICE/memory corruption is actually a 10+ regression. Do we need a backport? Thanks, Harald From eec039211e396e35204b55588013d74289a984cd Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 8 Feb 2024 21:51:38 +0100 Subject: [PATCH] Fortran: error recovery on arithmetic overflow on unary operations [PR113799] PR fortran/113799 gcc/fortran/ChangeLog: * arith.cc (reduce_unary): Remember any overflow encountered during reduction of unary arithmetic operations on array constructors and continue, and return error status, but terminate on serious errors. gcc/testsuite/ChangeLog: * gfortran.dg/arithmetic_overflow_2.f90: New test. --- gcc/fortran/arith.cc | 11 ++++++++--- gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 | 12 ++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index 0598f6ac51b..d17d1aaa1d9 100644 --- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -1323,6 +1323,7 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op, gfc_constructor *c; gfc_expr *r; arith rc; + bool ov = false; if (op->expr_type == EXPR_CONSTANT) return eval (op, result); @@ -1336,13 +1337,17 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op, { rc = reduce_unary (eval, c->expr, &r); - if (rc != ARITH_OK) + /* Remember any overflow encountered during reduction and continue, + but terminate on serious errors. */ + if (rc == ARITH_OVERFLOW) + ov = true; + else if (rc != ARITH_OK) break; gfc_replace_expr (c->expr, r); } - if (rc != ARITH_OK) + if (rc != ARITH_OK && rc != ARITH_OVERFLOW) gfc_constructor_free (head); else { @@ -1363,7 +1368,7 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op, *result = r; } - return rc; + return ov ? ARITH_OVERFLOW : rc; } diff --git a/gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 b/gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 new file mode 100644 index 00000000000..6ca27f74215 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 @@ -0,0 +1,12 @@ +! { dg-do compile } +! { dg-additional-options "-frange-check" } +! +! PR fortran/113799 - handle arithmetic overflow on unary minus + +program p + implicit none + real, parameter :: inf = real(z'7F800000') + real, parameter :: someInf(*) = [inf, 0.] + print *, -someInf ! { dg-error "Arithmetic overflow" } + print *, minval(-someInf) ! { dg-error "Arithmetic overflow" } +end -- 2.35.3