From e02cabecf0d025ec4f4ddee290bdf7aadb873bb3 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Tue, 24 Nov 2015 22:24:52 +0000 Subject: Refactor strtod parsing of NaN payloads. The nan* functions handle their string argument by constructing a NAN(...) string on the stack as a VLA and passing it to strtod functions. This approach has problems discussed in bug 16961 and bug 16962: the stack usage is unbounded, and it gives incorrect results in certain cases where the argument is not a valid n-char-sequence. The natural fix for both issues is to refactor the NaN payload parsing out of strtod into a separate function that the nan* functions can call directly, so that no temporary string needs constructing on the stack at all. This patch does that refactoring in preparation for fixing those bugs (but without actually using the new functions from nan* - which will also require exporting them from libc at version GLIBC_PRIVATE). This patch is not intended to change any user-visible behavior, so no tests are added (fixes for the above bugs will of course add tests for them). This patch builds on my recent fixes for strtol and strtod issues in Turkish locales. Given those fixes, the parsing of NaN payloads is locale-independent; thus, the new functions do not need to take a locale_t argument. Tested for x86_64, x86, mips64 and powerpc. * stdlib/strtod_nan.c: New file. * stdlib/strtod_nan_double.h: Likewise. * stdlib/strtod_nan_float.h: Likewise. * stdlib/strtod_nan_main.c: Likewise. * stdlib/strtod_nan_narrow.h: Likewise. * stdlib/strtod_nan_wide.h: Likewise. * stdlib/strtof_nan.c: Likewise. * stdlib/strtold_nan.c: Likewise. * sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h: Likewise. * sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h: Likewise. * sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h: Likewise. * wcsmbs/wcstod_nan.c: Likewise. * wcsmbs/wcstof_nan.c: Likewise. * wcsmbs/wcstold_nan.c: Likewise. * stdlib/Makefile (routines): Add strtof_nan, strtod_nan and strtold_nan. * wcsmbs/Makefile (routines): Add wcstod_nan, wcstold_nan and wcstof_nan. * include/stdlib.h (__strtof_nan): Declare and use libc_hidden_proto. (__strtod_nan): Likewise. (__strtold_nan): Likewise. (__wcstof_nan): Likewise. (__wcstod_nan): Likewise. (__wcstold_nan): Likewise. * include/wchar.h (____wcstoull_l_internal): Declare. * stdlib/strtod_l.c: Do not include . (____strtoull_l_internal): Remove declaration. (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. (STRTOULL): Likewise. (____STRTOF_INTERNAL): Use STRTOF_NAN to parse NaN payload. * stdlib/strtof_l.c (____strtoull_l_internal): Remove declaration. (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. * sysdeps/ieee754/ldbl-128/strtold_l.c (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. * sysdeps/ieee754/ldbl-128ibm/strtold_l.c (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. * sysdeps/ieee754/ldbl-64-128/strtold_l.c (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. * sysdeps/ieee754/ldbl-96/strtold_l.c (STRTOF_NAN): Define macro. (SET_MANTISSA): Remove macro. * wcsmbs/wcstod_l.c (____wcstoull_l_internal): Remove declaration. * wcsmbs/wcstof_l.c (____wcstoull_l_internal): Likewise. * wcsmbs/wcstold_l.c (____wcstoull_l_internal): Likewise. --- sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h | 30 ++++++++++++++++++++++++ sysdeps/ieee754/ldbl-128ibm/strtold_l.c | 10 ++------ 2 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h (limited to 'sysdeps/ieee754/ldbl-128ibm') diff --git a/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h b/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h new file mode 100644 index 0000000000..876a4bba03 --- /dev/null +++ b/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h @@ -0,0 +1,30 @@ +/* Convert string for NaN payload to corresponding NaN. For ldbl-128ibm. + Copyright (C) 1997-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 + . */ + +#define FLOAT long double +#define SET_MANTISSA(flt, mant) \ + do \ + { \ + union ibm_extended_long_double u; \ + u.ld = (flt); \ + u.d[0].ieee_nan.mantissa0 = (mant) >> 32; \ + u.d[0].ieee_nan.mantissa1 = (mant); \ + if ((u.d[0].ieee.mantissa0 | u.d[0].ieee.mantissa1) != 0) \ + (flt) = u.ld; \ + } \ + while (0) diff --git a/sysdeps/ieee754/ldbl-128ibm/strtold_l.c b/sysdeps/ieee754/ldbl-128ibm/strtold_l.c index 3e2f69e353..6cd963b2cc 100644 --- a/sysdeps/ieee754/ldbl-128ibm/strtold_l.c +++ b/sysdeps/ieee754/ldbl-128ibm/strtold_l.c @@ -30,25 +30,19 @@ extern long double ____new_wcstold_l (const wchar_t *, wchar_t **, __locale_t); # define STRTOF __new_wcstold_l # define __STRTOF ____new_wcstold_l # define ____STRTOF_INTERNAL ____wcstold_l_internal +# define STRTOF_NAN __wcstold_nan #else extern long double ____new_strtold_l (const char *, char **, __locale_t); # define STRTOF __new_strtold_l # define __STRTOF ____new_strtold_l # define ____STRTOF_INTERNAL ____strtold_l_internal +# define STRTOF_NAN __strtold_nan #endif extern __typeof (__STRTOF) STRTOF; libc_hidden_proto (__STRTOF) libc_hidden_proto (STRTOF) #define MPN2FLOAT __mpn_construct_long_double #define FLOAT_HUGE_VAL HUGE_VALL -# define SET_MANTISSA(flt, mant) \ - do { union ibm_extended_long_double u; \ - u.ld = (flt); \ - u.d[0].ieee_nan.mantissa0 = (mant) >> 32; \ - u.d[0].ieee_nan.mantissa1 = (mant); \ - if ((u.d[0].ieee.mantissa0 | u.d[0].ieee.mantissa1) != 0) \ - (flt) = u.ld; \ - } while (0) #include -- cgit 1.4.1