diff options
author | Joseph Myers <joseph@codesourcery.com> | 2015-11-13 12:03:46 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2015-11-13 12:03:46 +0000 |
commit | 909f8e14dbe4073d75a776b4a9f50e5cc450f161 (patch) | |
tree | 18700b3f961d37bdec7b9080852691bf3fe97e83 /sysdeps | |
parent | 21378ae0d390c45ef382e8c95cc3593845ebb6ea (diff) | |
download | glibc-909f8e14dbe4073d75a776b4a9f50e5cc450f161.zip glibc-909f8e14dbe4073d75a776b4a9f50e5cc450f161.tar.gz glibc-909f8e14dbe4073d75a776b4a9f50e5cc450f161.tar.bz2 |
Fix ldbl-128ibm strtold overflow handling (bug 14551).
For ldbl-128ibm, if the result of strtold overflows in the final
conversion from MPN to IBM long double (because the exponent for a
106-bit IEEE result is 1023 but the high part would end up as
0x1p1024, which overflows), that conversion code fails to handle this
and produces an invalid long double value (high part infinite, low
part not zero) without raising exceptions or setting errno. This
patch adds an explicit check for this case to ensure an appropriate
result is returned in a way that ensures the right exceptions are
raised, with errno set.
Tested for powerpc.
[BZ #14551]
* sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c: Include <errno.h>.
(__mpn_construct_long_double): If high part overflows to infinity,
set errno and recompute overflowed result of the correct sign.
* sysdeps/ieee754/ldbl-128ibm/Makefile
[$(subdir) = stdlib] (tests): Add tst-strtold-ldbl-128ibm.
[$(subdir) = stdlib] ($(objpfx)tst-strtold-ldbl-128ibm): Depend on
$(libm).
* sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c: New file.
Diffstat (limited to 'sysdeps')
-rw-r--r-- | sysdeps/ieee754/ldbl-128ibm/Makefile | 5 | ||||
-rw-r--r-- | sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c | 9 | ||||
-rw-r--r-- | sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c | 85 |
3 files changed, 99 insertions, 0 deletions
diff --git a/sysdeps/ieee754/ldbl-128ibm/Makefile b/sysdeps/ieee754/ldbl-128ibm/Makefile index 8e1e2e6..5591814 100644 --- a/sysdeps/ieee754/ldbl-128ibm/Makefile +++ b/sysdeps/ieee754/ldbl-128ibm/Makefile @@ -3,3 +3,8 @@ # when -mlong-double-64 is not used). long-double-fcts = yes sysdep-CFLAGS += -mlong-double-128 + +ifeq ($(subdir),stdlib) +tests += tst-strtold-ldbl-128ibm +$(objpfx)tst-strtold-ldbl-128ibm: $(libm) +endif diff --git a/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c b/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c index bacb2cc..90dae93 100644 --- a/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c +++ b/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c @@ -18,6 +18,7 @@ #include "gmp.h" #include "gmp-impl.h" #include <ieee754.h> +#include <errno.h> #include <float.h> #include <math.h> @@ -111,6 +112,14 @@ __mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign) { hi >>= 1; u.d[0].ieee.exponent++; + if (u.d[0].ieee.exponent == IEEE754_DOUBLE_BIAS + DBL_MAX_EXP) + { + /* Overflow. The appropriate overflowed result must + be produced (if an infinity, that means the low + part must be zero). */ + __set_errno (ERANGE); + return (sign ? -LDBL_MAX : LDBL_MAX) * LDBL_MAX; + } } u.d[1].ieee.negative = !sign; lo = (1LL << 53) - lo; diff --git a/sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c b/sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c new file mode 100644 index 0000000..382071d --- /dev/null +++ b/sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c @@ -0,0 +1,85 @@ +/* Test for ldbl-128ibm strtold overflow to infinity (bug 14551). + Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <fenv.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +static int +test_strtold_value (const char *s, double exp_hi, double exp_lo, int exp_exc, + int exp_errno) +{ + int result = 0; + union { long double ld; double d[2]; } x; + feclearexcept (FE_ALL_EXCEPT); + errno = 0; + x.ld = strtold (s, NULL); + int exc = fetestexcept (FE_ALL_EXCEPT); + int new_errno = errno; + printf ("strtold (\"%s\") returned (%a, %a), exceptions 0x%x, errno %d\n", + s, x.d[0], x.d[1], exc, new_errno); + if (x.d[0] == exp_hi) + printf ("PASS: strtold (\"%s\") high == %a\n", s, exp_hi); + else + { + printf ("FAIL: strtold (\"%s\") high == %a\n", s, exp_hi); + result = 1; + } + if (x.d[1] == exp_lo) + printf ("PASS: strtold (\"%s\") low == %a\n", s, exp_lo); + else + { + printf ("FAIL: strtold (\"%s\") low == %a\n", s, exp_lo); + result = 1; + } + if (exc == exp_exc) + printf ("PASS: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc); + else + { + printf ("FAIL: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc); + result = 1; + } + if (new_errno == exp_errno) + printf ("PASS: strtold (\"%s\") errno %d\n", s, exp_errno); + else + { + printf ("FAIL: strtold (\"%s\") errno %d\n", s, exp_errno); + result = 1; + } + return result; +} + +static int +do_test (void) +{ + int result = 0; + result |= test_strtold_value ("0x1.fffffffffffff8p+1023", INFINITY, 0, + FE_OVERFLOW | FE_INEXACT, ERANGE); + result |= test_strtold_value ("-0x1.fffffffffffff8p+1023", -INFINITY, 0, + FE_OVERFLOW | FE_INEXACT, ERANGE); + result |= test_strtold_value ("0x1.ffffffffffffffp+1023", INFINITY, 0, + FE_OVERFLOW | FE_INEXACT, ERANGE); + result |= test_strtold_value ("-0x1.ffffffffffffffp+1023", -INFINITY, 0, + FE_OVERFLOW | FE_INEXACT, ERANGE); + return result; +} + +#define TEST_FUNCTION do_test () +#include "../../../test-skeleton.c" |