From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 7BBDA386103F; Mon, 1 Mar 2021 11:56:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7BBDA386103F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=Tobias_Burnus@mentor.com IronPort-SDR: WeaooNVKUgKlwIBjNCucKxjfDENKpk+2TdI/jAKO0oDM8KRrEAp9HphxfEnpNhdFJzXxGWuOh/ L6OC5EDWb+IHezC5hDUjzOtgZpr0voi0NlsVLREwPKHuJWQKys2wwhof81U8dDin8jLQIuFr15 uT8ZNG7JQnKA86NXQhxQ6at/6XT55ONqGpnQL1E8ijdDonk0jIL1X+I3XcRwP4aWO3EWjs2OWL HUr3+Xx6pyIQTAR6gnk/is4mdqksSpLMtmfgfCFS7OrusUlTULQ//IR7utOMOix7JFPdClRkRS KCU= X-IronPort-AV: E=Sophos;i="5.81,215,1610438400"; d="diff'?scan'208";a="58604917" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa3.mentor.iphmx.com with ESMTP; 01 Mar 2021 03:56:50 -0800 IronPort-SDR: 01rS+9I85WLK301uvma0RqmFhmasYIyWKGG0bPaIF0XVPP7QEWWMFp26IjlJoroLeG0NIg10H1 Q0RbKtcLfXc3Us4c9mjwQEK73ak4op6CO0oiRZwZYX05cEJNmuj4nMpcSKgFl/J6oqqDJ9nE9U 1X2DXAnFmTqgz7Lx81RuYxTZkEFWOJh5skx1OTzk5TEkE2ESwPBqD8vqpneBTlriSJ1g53pzUv TEBDUbLuCKzkhbNzYDsSnXhxKBRPo4GOwqwaLr+JpwCxKgVYgkSfCpKs6OW0eJsDlG5yCEPjea yz8= To: gcc-patches , fortran , Jerry DeLisle From: Tobias Burnus Subject: [Patch] libgfortran: Fix negation for largest integer [PR81986] Message-ID: Date: Mon, 1 Mar 2021 12:56:37 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------FC72410FDD074F3AC7E7A5C6" Content-Language: en-US X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-09.mgc.mentorg.com (139.181.222.9) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Mar 2021 11:56:53 -0000 --------------FC72410FDD074F3AC7E7A5C6 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: quoted-printable Found by Vittorio Zecca when using the sanitizer on libgomp (bootstrap-ubsan) on pr66311.f90, which uses '(-huge(0_16))-1'. The error then occurs in libgfortran/runtime/string.c's gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len) where (as shown 'n') is signed and 't' is: GFC_UINTEGER_LARGEST t; As proposed by Vittorio (comment 2) and acknowledged by Jakub (comment 3), the conversion to UINT has to happen before the negation. For completeness, the result is the following. (Test is similar in pr66311.f90, albeit slightly different.) write(buffer,*) huge(0_16) if (buffer /=3D " 170141183460469231731687303715884105727") stop 1 write(buffer,*) huge(0_16)-1 if (buffer /=3D " 170141183460469231731687303715884105726") stop 2 write(buffer,*) -(huge(0_16)-1) if (buffer /=3D " -170141183460469231731687303715884105726") stop 3 ! Undefined behaviour as (-huge(0_16)) overflows: write(buffer,*) -huge(0_16)-1 if (buffer /=3D " -170141183460469231731687303715884105728") stop 4 I intent to commit the patch later as obvious unless there are further comm= ents. Tobias ----------------- Mentor Graphics (Deutschland) GmbH, Arnulfstrasse 201, 80634 M=C3=BCnchen R= egistergericht M=C3=BCnchen HRB 106955, Gesch=C3=A4ftsf=C3=BChrer: Thomas H= eurung, Frank Th=C3=BCrauf --------------FC72410FDD074F3AC7E7A5C6 Content-Type: text/x-patch; charset="UTF-8"; name="uint.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="uint.diff" libgfortran: Fix negation for largest integer [PR81986] libgfortran/ChangeLog: 2021-03-01 Vittorio Zecca Tobias Burnus PR libfortran/81986 * runtime/string.c (gfc_itoa): diff --git a/libgfortran/runtime/string.c b/libgfortran/runtime/string.c index 37c4da0a98a..536a9cd3f2b 100644 --- a/libgfortran/runtime/string.c +++ b/libgfortran/runtime/string.c @@ -196,7 +196,7 @@ gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len) if (n < 0) { negative = 1; - t = -n; /*must use unsigned to protect from overflow*/ + t = -(GFC_UINTEGER_LARGEST) n; /* Must use unsigned to protect from overflow. */ } p = buffer + GFC_ITOA_BUF_SIZE - 1; --------------FC72410FDD074F3AC7E7A5C6--