public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>,
	fortran <fortran@gcc.gnu.org>,
	Jerry DeLisle <jvdelisle@charter.net>
Subject: [Patch] libgfortran: Fix negation for largest integer [PR81986]
Date: Mon, 1 Mar 2021 12:56:37 +0100	[thread overview]
Message-ID: <edc6cd06-4dc4-8e96-0a04-01940988eb5c@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1359 bytes --]

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 /= "  170141183460469231731687303715884105727") stop 1

       write(buffer,*) huge(0_16)-1
       if (buffer /= "  170141183460469231731687303715884105726") stop 2

       write(buffer,*) -(huge(0_16)-1)
       if (buffer /= " -170141183460469231731687303715884105726") stop 3

       ! Undefined behaviour as (-huge(0_16)) overflows:
       write(buffer,*) -huge(0_16)-1
       if (buffer /= " -170141183460469231731687303715884105728") stop 4


I intent to commit the patch later as obvious unless there are further comments.

Tobias

-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstrasse 201, 80634 München Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Frank Thürauf

[-- Attachment #2: uint.diff --]
[-- Type: text/x-patch, Size: 730 bytes --]

libgfortran: Fix negation for largest integer [PR81986]

libgfortran/ChangeLog:

2021-03-01  Vittorio Zecca  <zeccav@gmail.com>
	    Tobias Burnus  <tobias@codesourcery.com>

	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;

                 reply	other threads:[~2021-03-01 11:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=edc6cd06-4dc4-8e96-0a04-01940988eb5c@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jvdelisle@charter.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).