public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Michael Hudson-Doyle <michael.hudson@canonical.com>
To: libc-alpha@sourceware.org
Subject: [PATCH] Avoid undefined behaviour in ibm128 implementation of llroundl
Date: Sun, 21 Aug 2022 22:44:46 +1200	[thread overview]
Message-ID: <20220821104446.46521-1-michael.hudson@canonical.com> (raw)

Detecting an overflow edge case depended on signed overflow of a long
long. Replace the signed long long with unsigned and cast it back to
unsigned before comparisons (which is implementation defined behaviour,
but I guess glibc does not support any one's complement
architectures...).

BZ #29488
---
 sysdeps/ieee754/ldbl-128ibm/s_llroundl.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
index d85154e73a..e8117bfc2b 100644
--- a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
+++ b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
@@ -28,7 +28,8 @@ long long
 __llroundl (long double x)
 {
   double xh, xl;
-  long long res, hi, lo;
+  unsigned long long res;
+  long long hi, lo;
 
   ldbl_unpack (x, &xh, &xl);
 
@@ -69,7 +70,7 @@ __llroundl (long double x)
       res = hi + lo;
 
       /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
-      if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
+      if (__glibc_unlikely (((~(hi ^ lo) & (((long long)res) ^ hi)) < 0)))
 	goto overflow;
 
       xh -= lo;
@@ -95,7 +96,7 @@ __llroundl (long double x)
 	    res -= 1;
 	}
 
-      if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
+      if (__glibc_unlikely (((~(hi ^ (((long long)res) - hi)) & (((long long)res) ^ hi)) < 0)))
 	goto overflow;
 
       return res;
-- 
2.34.1


             reply	other threads:[~2022-08-21 10:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-21 10:44 Michael Hudson-Doyle [this message]
2022-08-21 19:24 ` Michael Hudson-Doyle
2022-08-22  2:04 ` [PATCH v2] " Michael Hudson-Doyle
2022-08-22  7:54   ` Florian Weimer
2022-08-22  8:34   ` Andreas Schwab
2022-08-22  8:52     ` Michael Hudson-Doyle

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=20220821104446.46521-1-michael.hudson@canonical.com \
    --to=michael.hudson@canonical.com \
    --cc=libc-alpha@sourceware.org \
    /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).