From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1778) id 5C01B3858012; Mon, 24 Oct 2022 18:48:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5C01B3858012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666637333; bh=SOWiqbRUIVc6LVOaokv6SeUWOHxwx+2/5w6d0D5AFIM=; h=From:To:Subject:Date:From; b=x5Tw6x0aNmN1yK/21u5oImggd6Wy4X5drsqlRaVsq5C1jTJPBUuZpdwQGADyjqOdv kvhapYlUaGS/Ryvb4HLRbD0KuQr+YHJWMgb9gzjOHlz5W5/r+PvYtr2hD5giMyciGS GvRTVbjelkmV3jqKQo0cI8HZdfQEmFKMsoNQ+Lco= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Aurelien Jarno To: glibc-cvs@sourceware.org Subject: [glibc] Avoid undefined behaviour in ibm128 implementation of llroundl (BZ #29488) X-Act-Checkin: glibc X-Git-Author: Aurelien Jarno X-Git-Refname: refs/heads/master X-Git-Oldrev: be930668eccf842d5a1a068a3e01b660d5bcb813 X-Git-Newrev: 2b5478569e72ee4820a6e163d306690c9c0eaf5e Message-Id: <20221024184853.5C01B3858012@sourceware.org> Date: Mon, 24 Oct 2022 18:48:53 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=2b5478569e72ee4820a6e163d306690c9c0eaf5e commit 2b5478569e72ee4820a6e163d306690c9c0eaf5e Author: Aurelien Jarno Date: Mon Oct 10 00:39:33 2022 +0200 Avoid undefined behaviour in ibm128 implementation of llroundl (BZ #29488) Detecting an overflow edge case depended on signed overflow of a long long. Replace the additions and the overflow checks by __builtin_add_overflow(). Reviewed-by: Tulio Magno Quites Machado Filho Diff: --- sysdeps/ieee754/ldbl-128ibm/s_llroundl.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c index d85154e73a..d8c0de1faf 100644 --- a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c +++ b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c @@ -66,38 +66,35 @@ __llroundl (long double x) /* Peg at max/min values, assuming that the above conversions do so. Strictly speaking, we can return anything for values that overflow, but this is more useful. */ - 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 (__builtin_add_overflow (hi, lo, &res))) goto overflow; xh -= lo; ldbl_canonicalize (&xh, &xl); - hi = res; if (xh > 0.5) { - res += 1; + if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res))) + goto overflow; } else if (xh == 0.5) { if (xl > 0.0 || (xl == 0.0 && res >= 0)) - res += 1; + if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res))) + goto overflow; } else if (-xh > 0.5) { - res -= 1; + if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res))) + goto overflow; } else if (-xh == 0.5) { if (xl < 0.0 || (xl == 0.0 && res <= 0)) - res -= 1; + if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res))) + goto overflow; } - if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0))) - goto overflow; - return res; } else