00db10
From 8f5e8b01a1da2a207228f2072c934fa5918554b8 Mon Sep 17 00:00:00 2001
00db10
From: Joseph Myers <joseph@codesourcery.com>
00db10
Date: Fri, 4 Dec 2015 20:36:28 +0000
00db10
Subject: [PATCH] Fix nan functions handling of payload strings (bug 16961, bug
00db10
 16962).
00db10
00db10
The nan, nanf and nanl functions handle payload strings by doing e.g.:
00db10
00db10
  if (tagp[0] != '\0')
00db10
    {
00db10
      char buf[6 + strlen (tagp)];
00db10
      sprintf (buf, "NAN(%s)", tagp);
00db10
      return strtod (buf, NULL);
00db10
    }
00db10
00db10
This is an unbounded stack allocation based on the length of the
00db10
argument.  Furthermore, if the argument starts with an n-char-sequence
00db10
followed by ')', that n-char-sequence is wrongly treated as
00db10
significant for determining the payload of the resulting NaN, when ISO
00db10
C says the call should be equivalent to strtod ("NAN", NULL), without
00db10
being affected by that initial n-char-sequence.  This patch fixes both
00db10
those problems by using the __strtod_nan etc. functions recently
00db10
factored out of strtod etc. for that purpose, with those functions
00db10
being exported from libc at version GLIBC_PRIVATE.
00db10
00db10
Tested for x86_64, x86, mips64 and powerpc.
00db10
00db10
	[BZ #16961]
00db10
	[BZ #16962]
00db10
	* math/s_nan.c (__nan): Use __strtod_nan instead of constructing a
00db10
	string on the stack for strtod.
00db10
	* math/s_nanf.c (__nanf): Use __strtof_nan instead of constructing
00db10
	a string on the stack for strtof.
00db10
	* math/s_nanl.c (__nanl): Use __strtold_nan instead of
00db10
	constructing a string on the stack for strtold.
00db10
	* stdlib/Versions (libc): Add __strtof_nan, __strtod_nan and
00db10
	__strtold_nan to GLIBC_PRIVATE.
00db10
	* math/test-nan-overflow.c: New file.
00db10
	* math/test-nan-payload.c: Likewise.
00db10
	* math/Makefile (tests): Add test-nan-overflow and
00db10
	test-nan-payload.
00db10
00db10
From e02cabecf0d025ec4f4ddee290bdf7aadb873bb3 Mon Sep 17 00:00:00 2001
00db10
From: Joseph Myers <joseph@codesourcery.com>
00db10
Date: Tue, 24 Nov 2015 22:24:52 +0000
00db10
Subject: [PATCH] Refactor strtod parsing of NaN payloads.
00db10
00db10
The nan* functions handle their string argument by constructing a
00db10
NAN(...) string on the stack as a VLA and passing it to strtod
00db10
functions.
00db10
00db10
This approach has problems discussed in bug 16961 and bug 16962: the
00db10
stack usage is unbounded, and it gives incorrect results in certain
00db10
cases where the argument is not a valid n-char-sequence.
00db10
00db10
The natural fix for both issues is to refactor the NaN payload parsing
00db10
out of strtod into a separate function that the nan* functions can
00db10
call directly, so that no temporary string needs constructing on the
00db10
stack at all.  This patch does that refactoring in preparation for
00db10
fixing those bugs (but without actually using the new functions from
00db10
nan* - which will also require exporting them from libc at version
00db10
GLIBC_PRIVATE).  This patch is not intended to change any user-visible
00db10
behavior, so no tests are added (fixes for the above bugs will of
00db10
course add tests for them).
00db10
00db10
This patch builds on my recent fixes for strtol and strtod issues in
00db10
Turkish locales.  Given those fixes, the parsing of NaN payloads is
00db10
locale-independent; thus, the new functions do not need to take a
00db10
locale_t argument.
00db10
00db10
Tested for x86_64, x86, mips64 and powerpc.
00db10
00db10
	* stdlib/strtod_nan.c: New file.
00db10
	* stdlib/strtod_nan_double.h: Likewise.
00db10
	* stdlib/strtod_nan_float.h: Likewise.
00db10
	* stdlib/strtod_nan_main.c: Likewise.
00db10
	* stdlib/strtod_nan_narrow.h: Likewise.
00db10
	* stdlib/strtod_nan_wide.h: Likewise.
00db10
	* stdlib/strtof_nan.c: Likewise.
00db10
	* stdlib/strtold_nan.c: Likewise.
00db10
	* sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h: Likewise.
00db10
	* sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h: Likewise.
00db10
	* sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h: Likewise.
00db10
	* wcsmbs/wcstod_nan.c: Likewise.
00db10
	* wcsmbs/wcstof_nan.c: Likewise.
00db10
	* wcsmbs/wcstold_nan.c: Likewise.
00db10
	* stdlib/Makefile (routines): Add strtof_nan, strtod_nan and
00db10
	strtold_nan.
00db10
	* wcsmbs/Makefile (routines): Add wcstod_nan, wcstold_nan and
00db10
	wcstof_nan.
00db10
	* include/stdlib.h (__strtof_nan): Declare and use
00db10
	libc_hidden_proto.
00db10
	(__strtod_nan): Likewise.
00db10
	(__strtold_nan): Likewise.
00db10
	(__wcstof_nan): Likewise.
00db10
	(__wcstod_nan): Likewise.
00db10
	(__wcstold_nan): Likewise.
00db10
	* include/wchar.h (____wcstoull_l_internal): Declare.
00db10
	* stdlib/strtod_l.c: Do not include <ieee754.h>.
00db10
	(____strtoull_l_internal): Remove declaration.
00db10
	(STRTOF_NAN): Define macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	(STRTOULL): Likewise.
00db10
	(____STRTOF_INTERNAL): Use STRTOF_NAN to parse NaN payload.
00db10
	* stdlib/strtof_l.c (____strtoull_l_internal): Remove declaration.
00db10
	(STRTOF_NAN): Define macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	* sysdeps/ieee754/ldbl-128/strtold_l.c (STRTOF_NAN): Define macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	* sysdeps/ieee754/ldbl-128ibm/strtold_l.c (STRTOF_NAN): Define
00db10
	macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	* sysdeps/ieee754/ldbl-64-128/strtold_l.c (STRTOF_NAN): Define
00db10
	macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	* sysdeps/ieee754/ldbl-96/strtold_l.c (STRTOF_NAN): Define macro.
00db10
	(SET_MANTISSA): Remove macro.
00db10
	* wcsmbs/wcstod_l.c (____wcstoull_l_internal): Remove declaration.
00db10
	* wcsmbs/wcstof_l.c (____wcstoull_l_internal): Likewise.
00db10
	* wcsmbs/wcstold_l.c (____wcstoull_l_internal): Likewise.
00db10
00db10
diff -rupN a/include/stdlib.h b/include/stdlib.h
00db10
--- a/include/stdlib.h	2017-03-02 16:34:01.000000000 -0500
00db10
+++ b/include/stdlib.h	2017-03-02 16:45:05.457639119 -0500
00db10
@@ -193,6 +193,24 @@ libc_hidden_proto (strtoll)
00db10
 libc_hidden_proto (strtoul)
00db10
 libc_hidden_proto (strtoull)
00db10
 
00db10
+extern float __strtof_nan (const char *, char **, char) internal_function;
00db10
+extern double __strtod_nan (const char *, char **, char) internal_function;
00db10
+extern long double __strtold_nan (const char *, char **, char)
00db10
+     internal_function;
00db10
+extern float __wcstof_nan (const wchar_t *, wchar_t **, wchar_t)
00db10
+     internal_function;
00db10
+extern double __wcstod_nan (const wchar_t *, wchar_t **, wchar_t)
00db10
+     internal_function;
00db10
+extern long double __wcstold_nan (const wchar_t *, wchar_t **, wchar_t)
00db10
+     internal_function;
00db10
+
00db10
+libc_hidden_proto (__strtof_nan)
00db10
+libc_hidden_proto (__strtod_nan)
00db10
+libc_hidden_proto (__strtold_nan)
00db10
+libc_hidden_proto (__wcstof_nan)
00db10
+libc_hidden_proto (__wcstod_nan)
00db10
+libc_hidden_proto (__wcstold_nan)
00db10
+
00db10
 extern char *__ecvt (double __value, int __ndigit, int *__restrict __decpt,
00db10
 		     int *__restrict __sign);
00db10
 extern char *__fcvt (double __value, int __ndigit, int *__restrict __decpt,
00db10
diff -rupN a/include/wchar.h b/include/wchar.h
00db10
--- a/include/wchar.h	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/include/wchar.h	2017-03-02 16:45:05.461639109 -0500
00db10
@@ -52,6 +52,9 @@ extern unsigned long long int __wcstoull
00db10
 						   __restrict __endptr,
00db10
 						   int __base,
00db10
 						   int __group) __THROW;
00db10
+extern unsigned long long int ____wcstoull_l_internal (const wchar_t *,
00db10
+						       wchar_t **, int, int,
00db10
+						       __locale_t);
00db10
 libc_hidden_proto (__wcstof_internal)
00db10
 libc_hidden_proto (__wcstod_internal)
00db10
 libc_hidden_proto (__wcstold_internal)
00db10
diff -rupN a/math/Makefile b/math/Makefile
00db10
--- a/math/Makefile	2017-03-02 16:34:02.000000000 -0500
00db10
+++ b/math/Makefile	2017-03-02 16:44:30.659725844 -0500
00db10
@@ -88,7 +88,8 @@ long-c-yes = $(calls:=l)
00db10
 tests = test-matherr test-fenv atest-exp atest-sincos atest-exp2 basic-test \
00db10
 	test-misc test-fpucw tst-definitions test-tgmath test-tgmath-ret \
00db10
 	bug-nextafter bug-nexttoward bug-tgmath1 test-tgmath-int \
00db10
-	test-tgmath2 test-powl tst-CMPLX tst-CMPLX2
00db10
+	test-tgmath2 test-powl tst-CMPLX tst-CMPLX2 \
00db10
+	test-nan-overflow test-nan-payload
00db10
 # We do the `long double' tests only if this data type is available and
00db10
 # distinct from `double'.
00db10
 test-longdouble-yes = test-ldouble test-ildoubl
00db10
diff -rupN a/math/s_nan.c b/math/s_nan.c
00db10
--- a/math/s_nan.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/math/s_nan.c	2017-03-02 16:43:01.680999065 -0500
00db10
@@ -28,14 +28,7 @@
00db10
 double
00db10
 __nan (const char *tagp)
00db10
 {
00db10
-  if (tagp[0] != '\0')
00db10
-    {
00db10
-      char buf[6 + strlen (tagp)];
00db10
-      sprintf (buf, "NAN(%s)", tagp);
00db10
-      return strtod (buf, NULL);
00db10
-    }
00db10
-
00db10
-  return NAN;
00db10
+  return __strtod_nan (tagp, NULL, 0);
00db10
 }
00db10
 weak_alias (__nan, nan)
00db10
 #ifdef NO_LONG_DOUBLE
00db10
diff -rupN a/math/s_nanf.c b/math/s_nanf.c
00db10
--- a/math/s_nanf.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/math/s_nanf.c	2017-03-02 16:43:01.683999054 -0500
00db10
@@ -28,13 +28,6 @@
00db10
 float
00db10
 __nanf (const char *tagp)
00db10
 {
00db10
-  if (tagp[0] != '\0')
00db10
-    {
00db10
-      char buf[6 + strlen (tagp)];
00db10
-      sprintf (buf, "NAN(%s)", tagp);
00db10
-      return strtof (buf, NULL);
00db10
-    }
00db10
-
00db10
-  return NAN;
00db10
+  return __strtof_nan (tagp, NULL, 0);
00db10
 }
00db10
 weak_alias (__nanf, nanf)
00db10
diff -rupN a/math/s_nanl.c b/math/s_nanl.c
00db10
--- a/math/s_nanl.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/math/s_nanl.c	2017-03-02 16:43:01.686999044 -0500
00db10
@@ -28,13 +28,6 @@
00db10
 long double
00db10
 __nanl (const char *tagp)
00db10
 {
00db10
-  if (tagp[0] != '\0')
00db10
-    {
00db10
-      char buf[6 + strlen (tagp)];
00db10
-      sprintf (buf, "NAN(%s)", tagp);
00db10
-      return strtold (buf, NULL);
00db10
-    }
00db10
-
00db10
-  return NAN;
00db10
+  return __strtold_nan (tagp, NULL, 0);
00db10
 }
00db10
 weak_alias (__nanl, nanl)
00db10
diff -rupN a/math/test-nan-overflow.c b/math/test-nan-overflow.c
00db10
--- a/math/test-nan-overflow.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/math/test-nan-overflow.c	2017-03-02 16:43:01.689999033 -0500
00db10
@@ -0,0 +1,66 @@
00db10
+/* Test nan functions stack overflow (bug 16962).
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <math.h>
00db10
+#include <stdio.h>
00db10
+#include <string.h>
00db10
+#include <sys/resource.h>
00db10
+
00db10
+#define STACK_LIM 1048576
00db10
+#define STRING_SIZE (2 * STACK_LIM)
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int result = 0;
00db10
+  struct rlimit lim;
00db10
+  getrlimit (RLIMIT_STACK, &lim);
00db10
+  lim.rlim_cur = STACK_LIM;
00db10
+  setrlimit (RLIMIT_STACK, &lim);
00db10
+  char *nanstr = malloc (STRING_SIZE);
00db10
+  if (nanstr == NULL)
00db10
+    {
00db10
+      puts ("malloc failed, cannot test");
00db10
+      return 77;
00db10
+    }
00db10
+  memset (nanstr, '0', STRING_SIZE - 1);
00db10
+  nanstr[STRING_SIZE - 1] = 0;
00db10
+#define NAN_TEST(TYPE, FUNC)			\
00db10
+  do						\
00db10
+    {						\
00db10
+      char *volatile p = nanstr;		\
00db10
+      volatile TYPE v = FUNC (p);		\
00db10
+      if (isnan (v))				\
00db10
+	puts ("PASS: " #FUNC);			\
00db10
+      else					\
00db10
+	{					\
00db10
+	  puts ("FAIL: " #FUNC);		\
00db10
+	  result = 1;				\
00db10
+	}					\
00db10
+    }						\
00db10
+  while (0)
00db10
+  NAN_TEST (float, nanf);
00db10
+  NAN_TEST (double, nan);
00db10
+#ifndef NO_LONG_DOUBLE
00db10
+  NAN_TEST (long double, nanl);
00db10
+#endif
00db10
+  return result;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff -rupN a/math/test-nan-payload.c b/math/test-nan-payload.c
00db10
--- a/math/test-nan-payload.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/math/test-nan-payload.c	2017-03-02 16:43:01.693999019 -0500
00db10
@@ -0,0 +1,122 @@
00db10
+/* Test nan functions payload handling (bug 16961).
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <float.h>
00db10
+#include <math.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+
00db10
+/* Avoid built-in functions.  */
00db10
+#define WRAP_NAN(FUNC, STR) \
00db10
+  ({ const char *volatile wns = (STR); FUNC (wns); })
00db10
+#define WRAP_STRTO(FUNC, STR) \
00db10
+  ({ const char *volatile wss = (STR); FUNC (wss, NULL); })
00db10
+
00db10
+#define CHECK_IS_NAN(TYPE, A)			\
00db10
+  do						\
00db10
+    {						\
00db10
+      if (isnan (A))				\
00db10
+	puts ("PASS: " #TYPE " " #A);		\
00db10
+      else					\
00db10
+	{					\
00db10
+	  puts ("FAIL: " #TYPE " " #A);		\
00db10
+	  result = 1;				\
00db10
+	}					\
00db10
+    }						\
00db10
+  while (0)
00db10
+
00db10
+#define CHECK_SAME_NAN(TYPE, A, B)			\
00db10
+  do							\
00db10
+    {							\
00db10
+      if (memcmp (&(A), &(B), sizeof (A)) == 0)		\
00db10
+	puts ("PASS: " #TYPE " " #A " = " #B);		\
00db10
+      else						\
00db10
+	{						\
00db10
+	  puts ("FAIL: " #TYPE " " #A " = " #B);	\
00db10
+	  result = 1;					\
00db10
+	}						\
00db10
+    }							\
00db10
+  while (0)
00db10
+
00db10
+#define CHECK_DIFF_NAN(TYPE, A, B)			\
00db10
+  do							\
00db10
+    {							\
00db10
+      if (memcmp (&(A), &(B), sizeof (A)) != 0)		\
00db10
+	puts ("PASS: " #TYPE " " #A " != " #B);		\
00db10
+      else						\
00db10
+	{						\
00db10
+	  puts ("FAIL: " #TYPE " " #A " != " #B);	\
00db10
+	  result = 1;					\
00db10
+	}						\
00db10
+    }							\
00db10
+  while (0)
00db10
+
00db10
+/* Cannot test payloads by memcmp for formats where NaNs have padding
00db10
+   bits.  */
00db10
+#define CAN_TEST_EQ(MANT_DIG) ((MANT_DIG) != 64 && (MANT_DIG) != 106)
00db10
+
00db10
+#define RUN_TESTS(TYPE, SFUNC, FUNC, MANT_DIG)		\
00db10
+  do							\
00db10
+    {							\
00db10
+     TYPE n123 = WRAP_NAN (FUNC, "123");		\
00db10
+     CHECK_IS_NAN (TYPE, n123);				\
00db10
+     TYPE s123 = WRAP_STRTO (SFUNC, "NAN(123)");	\
00db10
+     CHECK_IS_NAN (TYPE, s123);				\
00db10
+     TYPE n456 = WRAP_NAN (FUNC, "456");		\
00db10
+     CHECK_IS_NAN (TYPE, n456);				\
00db10
+     TYPE s456 = WRAP_STRTO (SFUNC, "NAN(456)");	\
00db10
+     CHECK_IS_NAN (TYPE, s456);				\
00db10
+     TYPE n123x = WRAP_NAN (FUNC, "123)");		\
00db10
+     CHECK_IS_NAN (TYPE, n123x);			\
00db10
+     TYPE nemp = WRAP_NAN (FUNC, "");			\
00db10
+     CHECK_IS_NAN (TYPE, nemp);				\
00db10
+     TYPE semp = WRAP_STRTO (SFUNC, "NAN()");		\
00db10
+     CHECK_IS_NAN (TYPE, semp);				\
00db10
+     TYPE sx = WRAP_STRTO (SFUNC, "NAN");		\
00db10
+     CHECK_IS_NAN (TYPE, sx);				\
00db10
+     if (CAN_TEST_EQ (MANT_DIG))			\
00db10
+       CHECK_SAME_NAN (TYPE, n123, s123);		\
00db10
+     if (CAN_TEST_EQ (MANT_DIG))			\
00db10
+       CHECK_SAME_NAN (TYPE, n456, s456);		\
00db10
+     if (CAN_TEST_EQ (MANT_DIG))			\
00db10
+       CHECK_SAME_NAN (TYPE, nemp, semp);		\
00db10
+     if (CAN_TEST_EQ (MANT_DIG))			\
00db10
+       CHECK_SAME_NAN (TYPE, n123x, sx);		\
00db10
+     CHECK_DIFF_NAN (TYPE, n123, n456);			\
00db10
+     CHECK_DIFF_NAN (TYPE, n123, nemp);			\
00db10
+     CHECK_DIFF_NAN (TYPE, n123, n123x);		\
00db10
+     CHECK_DIFF_NAN (TYPE, n456, nemp);			\
00db10
+     CHECK_DIFF_NAN (TYPE, n456, n123x);		\
00db10
+    }							\
00db10
+  while (0)
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int result = 0;
00db10
+  RUN_TESTS (float, strtof, nanf, FLT_MANT_DIG);
00db10
+  RUN_TESTS (double, strtod, nan, DBL_MANT_DIG);
00db10
+#ifndef NO_LONG_DOUBLE
00db10
+  RUN_TESTS (long double, strtold, nanl, LDBL_MANT_DIG);
00db10
+#endif
00db10
+  return result;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff -rupN a/stdlib/Makefile b/stdlib/Makefile
00db10
--- a/stdlib/Makefile	2017-03-02 16:34:02.000000000 -0500
00db10
+++ b/stdlib/Makefile	2017-03-02 16:45:05.463639105 -0500
00db10
@@ -47,6 +47,7 @@ routines	:=							      \
00db10
 	strtol_l strtoul_l strtoll_l strtoull_l				      \
00db10
 	strtof strtod strtold						      \
00db10
 	strtof_l strtod_l strtold_l					      \
00db10
+	strtof_nan strtod_nan strtold_nan				      \
00db10
 	system canonicalize						      \
00db10
 	a64l l64a							      \
00db10
 	rpmatch strfmon strfmon_l getsubopt xpg_basename fmtmsg		      \
00db10
diff -rupN a/stdlib/Versions b/stdlib/Versions
00db10
--- a/stdlib/Versions	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/stdlib/Versions	2017-03-02 16:44:52.140671064 -0500
00db10
@@ -114,5 +114,6 @@ libc {
00db10
     __abort_msg;
00db10
     # Used from other libraries
00db10
     __libc_secure_getenv;
00db10
+    __strtof_nan; __strtod_nan; __strtold_nan;
00db10
   }
00db10
 }
00db10
diff -rupN a/stdlib/strtod_l.c b/stdlib/strtod_l.c
00db10
--- a/stdlib/strtod_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/stdlib/strtod_l.c	2017-03-02 16:59:50.224590342 -0500
00db10
@@ -20,8 +20,6 @@
00db10
 #include <xlocale.h>
00db10
 
00db10
 extern double ____strtod_l_internal (const char *, char **, int, __locale_t);
00db10
-extern unsigned long long int ____strtoull_l_internal (const char *, char **,
00db10
-						       int, int, __locale_t);
00db10
 
00db10
 /* Configuration part.  These macros are defined by `strtold.c',
00db10
    `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
00db10
@@ -33,28 +31,20 @@ extern unsigned long long int ____strtou
00db10
 # ifdef USE_WIDE_CHAR
00db10
 #  define STRTOF	wcstod_l
00db10
 #  define __STRTOF	__wcstod_l
00db10
+#  define STRTOF_NAN	__wcstod_nan
00db10
 # else
00db10
 #  define STRTOF	strtod_l
00db10
 #  define __STRTOF	__strtod_l
00db10
+#  define STRTOF_NAN	__strtod_nan
00db10
 # endif
00db10
 # define MPN2FLOAT	__mpn_construct_double
00db10
 # define FLOAT_HUGE_VAL	HUGE_VAL
00db10
-# define SET_MANTISSA(flt, mant) \
00db10
-  do { union ieee754_double u;						      \
00db10
-       u.d = (flt);							      \
00db10
-       if ((mant & 0xfffffffffffffULL) == 0)				      \
00db10
-	 mant = 0x8000000000000ULL;					      \
00db10
-       u.ieee.mantissa0 = ((mant) >> 32) & 0xfffff;			      \
00db10
-       u.ieee.mantissa1 = (mant) & 0xffffffff;				      \
00db10
-       (flt) = u.d;							      \
00db10
-  } while (0)
00db10
 #endif
00db10
 /* End of configuration part.  */
00db10
 
00db10
 #include <ctype.h>
00db10
 #include <errno.h>
00db10
 #include <float.h>
00db10
-#include <ieee754.h>
00db10
 #include "../locale/localeinfo.h"
00db10
 #include <locale.h>
00db10
 #include <math.h>
00db10
@@ -105,7 +95,6 @@ extern unsigned long long int ____strtou
00db10
 # define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
00db10
 # define STRNCASECMP(S1, S2, N) \
00db10
   __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
00db10
-# define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0, loc)
00db10
 #else
00db10
 # define STRING_TYPE char
00db10
 # define CHAR_TYPE char
00db10
@@ -117,7 +106,6 @@ extern unsigned long long int ____strtou
00db10
 # define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
00db10
 # define STRNCASECMP(S1, S2, N) \
00db10
   __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
00db10
-# define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0, loc)
00db10
 #endif
00db10
 
00db10
 
00db10
@@ -649,33 +637,14 @@ ____STRTOF_INTERNAL (nptr, endptr, group
00db10
 	  if (*cp == L_('('))
00db10
 	    {
00db10
 	      const STRING_TYPE *startp = cp;
00db10
-	      do
00db10
-		++cp;
00db10
-	      while ((*cp >= L_('0') && *cp <= L_('9'))
00db10
-		     || ({ CHAR_TYPE lo = TOLOWER (*cp);
00db10
-			   lo >= L_('a') && lo <= L_('z'); })
00db10
-		     || *cp == L_('_'));
00db10
-
00db10
-	      if (*cp != L_(')'))
00db10
-		/* The closing brace is missing.  Only match the NAN
00db10
-		   part.  */
00db10
-		cp = startp;
00db10
+	      STRING_TYPE *endp;
00db10
+	      retval = STRTOF_NAN (cp + 1, &endp, L_(')'));
00db10
+	      if (*endp == L_(')'))
00db10
+		/* Consume the closing parenthesis.  */
00db10
+		cp = endp + 1;
00db10
 	      else
00db10
-		{
00db10
-		  /* This is a system-dependent way to specify the
00db10
-		     bitmask used for the NaN.  We expect it to be
00db10
-		     a number which is put in the mantissa of the
00db10
-		     number.  */
00db10
-		  STRING_TYPE *endp;
00db10
-		  unsigned long long int mant;
00db10
-
00db10
-		  mant = STRTOULL (startp + 1, &endp, 0);
00db10
-		  if (endp == cp)
00db10
-		    SET_MANTISSA (retval, mant);
00db10
-
00db10
-		  /* Consume the closing brace.  */
00db10
-		  ++cp;
00db10
-		}
00db10
+		/* Only match the NAN part.  */
00db10
+		cp = startp;
00db10
 	    }
00db10
 
00db10
 	  if (endptr != NULL)
00db10
diff -rupN a/stdlib/strtod_nan.c b/stdlib/strtod_nan.c
00db10
--- a/stdlib/strtod_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan.c	2017-03-02 16:45:05.473639081 -0500
00db10
@@ -0,0 +1,24 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Narrow
00db10
+   strings, double.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <strtod_nan_narrow.h>
00db10
+#include <strtod_nan_double.h>
00db10
+
00db10
+#define STRTOD_NAN __strtod_nan
00db10
+#include <strtod_nan_main.c>
00db10
diff -rupN a/stdlib/strtod_nan_double.h b/stdlib/strtod_nan_double.h
00db10
--- a/stdlib/strtod_nan_double.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan_double.h	2017-03-02 16:45:05.477639072 -0500
00db10
@@ -0,0 +1,30 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  For double.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define FLOAT		double
00db10
+#define SET_MANTISSA(flt, mant)				\
00db10
+  do							\
00db10
+    {							\
00db10
+      union ieee754_double u;				\
00db10
+      u.d = (flt);					\
00db10
+      u.ieee_nan.mantissa0 = (mant) >> 32;		\
00db10
+      u.ieee_nan.mantissa1 = (mant);			\
00db10
+      if ((u.ieee.mantissa0 | u.ieee.mantissa1) != 0)	\
00db10
+	(flt) = u.d;					\
00db10
+    }							\
00db10
+  while (0)
00db10
diff -rupN a/stdlib/strtod_nan_float.h b/stdlib/strtod_nan_float.h
00db10
--- a/stdlib/strtod_nan_float.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan_float.h	2017-03-02 16:45:05.480639065 -0500
00db10
@@ -0,0 +1,29 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  For float.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define	FLOAT		float
00db10
+#define SET_MANTISSA(flt, mant)			\
00db10
+  do						\
00db10
+    {						\
00db10
+      union ieee754_float u;			\
00db10
+      u.f = (flt);				\
00db10
+      u.ieee_nan.mantissa = (mant);		\
00db10
+      if (u.ieee.mantissa != 0)			\
00db10
+	(flt) = u.f;				\
00db10
+    }						\
00db10
+  while (0)
00db10
diff -rupN a/stdlib/strtod_nan_main.c b/stdlib/strtod_nan_main.c
00db10
--- a/stdlib/strtod_nan_main.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan_main.c	2017-03-02 16:45:05.483639058 -0500
00db10
@@ -0,0 +1,63 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <ieee754.h>
00db10
+#include <locale.h>
00db10
+#include <math.h>
00db10
+#include <stdlib.h>
00db10
+#include <wchar.h>
00db10
+
00db10
+
00db10
+/* If STR starts with an optional n-char-sequence as defined by ISO C
00db10
+   (a sequence of ASCII letters, digits and underscores), followed by
00db10
+   ENDC, return a NaN whose payload is set based on STR.  Otherwise,
00db10
+   return a default NAN.  If ENDPTR is not NULL, set *ENDPTR to point
00db10
+   to the character after the initial n-char-sequence.  */
00db10
+
00db10
+internal_function
00db10
+FLOAT
00db10
+STRTOD_NAN (const STRING_TYPE *str, STRING_TYPE **endptr, STRING_TYPE endc)
00db10
+{
00db10
+  const STRING_TYPE *cp = str;
00db10
+
00db10
+  while ((*cp >= L_('0') && *cp <= L_('9'))
00db10
+	 || (*cp >= L_('A') && *cp <= L_('Z'))
00db10
+	 || (*cp >= L_('a') && *cp <= L_('z'))
00db10
+	 || *cp == L_('_'))
00db10
+    ++cp;
00db10
+
00db10
+  FLOAT retval = NAN;
00db10
+  if (*cp != endc)
00db10
+    goto out;
00db10
+
00db10
+  /* This is a system-dependent way to specify the bitmask used for
00db10
+     the NaN.  We expect it to be a number which is put in the
00db10
+     mantissa of the number.  */
00db10
+  STRING_TYPE *endp;
00db10
+  unsigned long long int mant;
00db10
+
00db10
+  mant = STRTOULL (str, &endp, 0);
00db10
+  if (endp == cp)
00db10
+    SET_MANTISSA (retval, mant);
00db10
+
00db10
+ out:
00db10
+  if (endptr != NULL)
00db10
+    *endptr = (STRING_TYPE *) cp;
00db10
+  return retval;
00db10
+}
00db10
+libc_hidden_def (STRTOD_NAN)
00db10
diff -rupN a/stdlib/strtod_nan_narrow.h b/stdlib/strtod_nan_narrow.h
00db10
--- a/stdlib/strtod_nan_narrow.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan_narrow.h	2017-03-02 16:45:05.486639051 -0500
00db10
@@ -0,0 +1,22 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Narrow strings.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define STRING_TYPE char
00db10
+#define L_(Ch) Ch
00db10
+#define STRTOULL(S, E, B) ____strtoull_l_internal ((S), (E), (B), 0,	\
00db10
+						   _nl_C_locobj_ptr)
00db10
diff -rupN a/stdlib/strtod_nan_wide.h b/stdlib/strtod_nan_wide.h
00db10
--- a/stdlib/strtod_nan_wide.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtod_nan_wide.h	2017-03-02 16:45:05.489639044 -0500
00db10
@@ -0,0 +1,22 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Wide strings.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define STRING_TYPE wchar_t
00db10
+#define L_(Ch) L##Ch
00db10
+#define STRTOULL(S, E, B) ____wcstoull_l_internal ((S), (E), (B), 0,	\
00db10
+						   _nl_C_locobj_ptr)
00db10
diff -rupN a/stdlib/strtof_l.c b/stdlib/strtof_l.c
00db10
--- a/stdlib/strtof_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/stdlib/strtof_l.c	2017-03-02 17:06:34.349227993 -0500
00db10
@@ -20,27 +20,19 @@
00db10
 #include <xlocale.h>
00db10
 
00db10
 extern float ____strtof_l_internal (const char *, char **, int, __locale_t);
00db10
-extern unsigned long long int ____strtoull_l_internal (const char *, char **,
00db10
-						       int, int, __locale_t);
00db10
 
00db10
 #define	FLOAT		float
00db10
 #define	FLT		FLT
00db10
 #ifdef USE_WIDE_CHAR
00db10
 # define STRTOF		wcstof_l
00db10
 # define __STRTOF	__wcstof_l
00db10
+# define STRTOF_NAN	__wcstof_nan
00db10
 #else
00db10
 # define STRTOF		strtof_l
00db10
 # define __STRTOF	__strtof_l
00db10
+# define STRTOF_NAN	__strtof_nan
00db10
 #endif
00db10
 #define	MPN2FLOAT	__mpn_construct_float
00db10
 #define	FLOAT_HUGE_VAL	HUGE_VALF
00db10
-#define SET_MANTISSA(flt, mant) \
00db10
-  do { union ieee754_float u;						      \
00db10
-       u.f = (flt);							      \
00db10
-       if ((mant & 0x7fffff) == 0)					      \
00db10
-	 mant = 0x400000;						      \
00db10
-       u.ieee.mantissa = (mant) & 0x7fffff;				      \
00db10
-       (flt) = u.f;							      \
00db10
-  } while (0)
00db10
 
00db10
 #include "strtod_l.c"
00db10
diff -rupN a/stdlib/strtof_nan.c b/stdlib/strtof_nan.c
00db10
--- a/stdlib/strtof_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtof_nan.c	2017-03-02 16:45:05.498639023 -0500
00db10
@@ -0,0 +1,24 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Narrow
00db10
+   strings, float.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <strtod_nan_narrow.h>
00db10
+#include <strtod_nan_float.h>
00db10
+
00db10
+#define STRTOD_NAN __strtof_nan
00db10
+#include <strtod_nan_main.c>
00db10
diff -rupN a/stdlib/strtold_nan.c b/stdlib/strtold_nan.c
00db10
--- a/stdlib/strtold_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/stdlib/strtold_nan.c	2017-03-02 16:45:05.501639016 -0500
00db10
@@ -0,0 +1,30 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Narrow
00db10
+   strings, long double.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <math.h>
00db10
+
00db10
+/* This function is unused if long double and double have the same
00db10
+   representation.  */
00db10
+#ifndef __NO_LONG_DOUBLE_MATH
00db10
+# include <strtod_nan_narrow.h>
00db10
+# include <strtod_nan_ldouble.h>
00db10
+
00db10
+# define STRTOD_NAN __strtold_nan
00db10
+# include <strtod_nan_main.c>
00db10
+#endif
00db10
diff -rupN a/sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h b/sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h
00db10
--- a/sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-128/strtod_nan_ldouble.h	2017-03-02 16:45:05.502639014 -0500
00db10
@@ -0,0 +1,33 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  For ldbl-128.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define FLOAT		long double
00db10
+#define SET_MANTISSA(flt, mant)				\
00db10
+  do							\
00db10
+    {							\
00db10
+      union ieee854_long_double u;			\
00db10
+      u.d = (flt);					\
00db10
+      u.ieee_nan.mantissa0 = 0;				\
00db10
+      u.ieee_nan.mantissa1 = 0;				\
00db10
+      u.ieee_nan.mantissa2 = (mant) >> 32;		\
00db10
+      u.ieee_nan.mantissa3 = (mant);			\
00db10
+      if ((u.ieee.mantissa0 | u.ieee.mantissa1		\
00db10
+	   | u.ieee.mantissa2 | u.ieee.mantissa3) != 0)	\
00db10
+	(flt) = u.d;					\
00db10
+    }							\
00db10
+  while (0)
00db10
diff -rupN a/sysdeps/ieee754/ldbl-128/strtold_l.c b/sysdeps/ieee754/ldbl-128/strtold_l.c
00db10
--- a/sysdeps/ieee754/ldbl-128/strtold_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-128/strtold_l.c	2017-03-02 17:07:43.540018882 -0500
00db10
@@ -25,20 +25,13 @@
00db10
 #ifdef USE_WIDE_CHAR
00db10
 # define STRTOF		wcstold_l
00db10
 # define __STRTOF	__wcstold_l
00db10
+# define STRTOF_NAN	__wcstold_nan
00db10
 #else
00db10
 # define STRTOF		strtold_l
00db10
 # define __STRTOF	__strtold_l
00db10
+# define STRTOF_NAN	__strtold_nan
00db10
 #endif
00db10
 #define MPN2FLOAT	__mpn_construct_long_double
00db10
 #define FLOAT_HUGE_VAL	HUGE_VALL
00db10
-#define SET_MANTISSA(flt, mant) \
00db10
-  do { union ieee854_long_double u;					      \
00db10
-       u.d = (flt);							      \
00db10
-       u.ieee.mantissa0 = 0x8000;					      \
00db10
-       u.ieee.mantissa1 = 0;						      \
00db10
-       u.ieee.mantissa2 = ((mant) >> 32);	      			      \
00db10
-       u.ieee.mantissa3 = (mant) & 0xffffffff;				      \
00db10
-       (flt) = u.d;							      \
00db10
-  } while (0)
00db10
 
00db10
 #include <strtod_l.c>
00db10
diff -rupN a/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h b/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h
00db10
--- a/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-128ibm/strtod_nan_ldouble.h	2017-03-02 16:45:05.505639007 -0500
00db10
@@ -0,0 +1,30 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  For ldbl-128ibm.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define FLOAT		long double
00db10
+#define SET_MANTISSA(flt, mant)					\
00db10
+  do								\
00db10
+    {								\
00db10
+      union ibm_extended_long_double u;				\
00db10
+      u.ld = (flt);						\
00db10
+      u.d[0].ieee_nan.mantissa0 = (mant) >> 32;			\
00db10
+      u.d[0].ieee_nan.mantissa1 = (mant);			\
00db10
+      if ((u.d[0].ieee.mantissa0 | u.d[0].ieee.mantissa1) != 0)	\
00db10
+	(flt) = u.ld;						\
00db10
+    }								\
00db10
+  while (0)
00db10
diff -rupN a/sysdeps/ieee754/ldbl-128ibm/strtold_l.c b/sysdeps/ieee754/ldbl-128ibm/strtold_l.c
00db10
--- a/sysdeps/ieee754/ldbl-128ibm/strtold_l.c	2017-03-02 16:33:54.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-128ibm/strtold_l.c	2017-03-02 17:10:22.516584043 -0500
00db10
@@ -30,25 +30,19 @@ extern long double ____new_wcstold_l (co
00db10
 # define STRTOF		__new_wcstold_l
00db10
 # define __STRTOF	____new_wcstold_l
00db10
 # define ____STRTOF_INTERNAL ____wcstold_l_internal
00db10
+# define STRTOF_NAN	__wcstold_nan
00db10
 #else
00db10
 extern long double ____new_strtold_l (const char *, char **, __locale_t);
00db10
 # define STRTOF		__new_strtold_l
00db10
 # define __STRTOF	____new_strtold_l
00db10
 # define ____STRTOF_INTERNAL ____strtold_l_internal
00db10
+# define STRTOF_NAN	__strtold_nan
00db10
 #endif
00db10
 extern __typeof (__STRTOF) STRTOF;
00db10
 libc_hidden_proto (__STRTOF)
00db10
 libc_hidden_proto (STRTOF)
00db10
 #define MPN2FLOAT	__mpn_construct_long_double
00db10
 #define FLOAT_HUGE_VAL	HUGE_VALL
00db10
-# define SET_MANTISSA(flt, mant) \
00db10
-  do { union ibm_extended_long_double u;				      \
00db10
-       u.ld = (flt);							      \
00db10
-       u.d[0].ieee_nan.mantissa0 = (mant) >> 32;				      \
00db10
-       u.d[0].ieee_nan.mantissa1 = (mant);				   	      \
00db10
-       if ((u.d[0].ieee.mantissa0 | u.d[0].ieee.mantissa1) != 0)	      \
00db10
-         (flt) = u.ld;							      \
00db10
-  } while (0)
00db10
 
00db10
 #include <strtod_l.c>
00db10
 
00db10
diff -rupN a/sysdeps/ieee754/ldbl-64-128/strtold_l.c b/sysdeps/ieee754/ldbl-64-128/strtold_l.c
00db10
--- a/sysdeps/ieee754/ldbl-64-128/strtold_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-64-128/strtold_l.c	2017-03-02 17:11:06.062475088 -0500
00db10
@@ -30,26 +30,19 @@ extern long double ____new_wcstold_l (co
00db10
 # define STRTOF		__new_wcstold_l
00db10
 # define __STRTOF	____new_wcstold_l
00db10
 # define ____STRTOF_INTERNAL ____wcstold_l_internal
00db10
+# define STRTOF_NAN	__wcstold_nan
00db10
 #else
00db10
 extern long double ____new_strtold_l (const char *, char **, __locale_t);
00db10
 # define STRTOF		__new_strtold_l
00db10
 # define __STRTOF	____new_strtold_l
00db10
 # define ____STRTOF_INTERNAL ____strtold_l_internal
00db10
+# define STRTOF_NAN	__strtold_nan
00db10
 #endif
00db10
 extern __typeof (__STRTOF) STRTOF;
00db10
 libc_hidden_proto (__STRTOF)
00db10
 libc_hidden_proto (STRTOF)
00db10
 #define MPN2FLOAT	__mpn_construct_long_double
00db10
 #define FLOAT_HUGE_VAL	HUGE_VALL
00db10
-#define SET_MANTISSA(flt, mant) \
00db10
-  do { union ieee854_long_double u;					      \
00db10
-       u.d = (flt);							      \
00db10
-       u.ieee.mantissa0 = 0x8000;					      \
00db10
-       u.ieee.mantissa1 = 0;						      \
00db10
-       u.ieee.mantissa2 = ((mant) >> 32);	      			      \
00db10
-       u.ieee.mantissa3 = (mant) & 0xffffffff;				      \
00db10
-       (flt) = u.d;							      \
00db10
-  } while (0)
00db10
 
00db10
 #include <strtod_l.c>
00db10
 
00db10
diff -rupN a/sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h b/sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h
00db10
--- a/sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-96/strtod_nan_ldouble.h	2017-03-02 16:45:05.521638969 -0500
00db10
@@ -0,0 +1,30 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  For ldbl-96.
00db10
+   Copyright (C) 1997-2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#define FLOAT		long double
00db10
+#define SET_MANTISSA(flt, mant)				\
00db10
+  do							\
00db10
+    {							\
00db10
+      union ieee854_long_double u;			\
00db10
+      u.d = (flt);					\
00db10
+      u.ieee_nan.mantissa0 = (mant) >> 32;		\
00db10
+      u.ieee_nan.mantissa1 = (mant);			\
00db10
+      if ((u.ieee.mantissa0 | u.ieee.mantissa1) != 0)	\
00db10
+	(flt) = u.d;					\
00db10
+    }							\
00db10
+  while (0)
00db10
diff -rupN a/sysdeps/ieee754/ldbl-96/strtold_l.c b/sysdeps/ieee754/ldbl-96/strtold_l.c
00db10
--- a/sysdeps/ieee754/ldbl-96/strtold_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/sysdeps/ieee754/ldbl-96/strtold_l.c	2017-03-02 17:11:52.927362322 -0500
00db10
@@ -25,20 +25,13 @@
00db10
 #ifdef USE_WIDE_CHAR
00db10
 # define STRTOF		wcstold_l
00db10
 # define __STRTOF	__wcstold_l
00db10
+# define STRTOF_NAN	__wcstold_nan
00db10
 #else
00db10
 # define STRTOF		strtold_l
00db10
 # define __STRTOF	__strtold_l
00db10
+# define STRTOF_NAN	__strtold_nan
00db10
 #endif
00db10
 #define MPN2FLOAT	__mpn_construct_long_double
00db10
 #define FLOAT_HUGE_VAL	HUGE_VALL
00db10
-#define SET_MANTISSA(flt, mant) \
00db10
-  do { union ieee854_long_double u;					      \
00db10
-       u.d = (flt);							      \
00db10
-       if ((mant & 0x7fffffffffffffffULL) == 0)				      \
00db10
-	 mant = 0x4000000000000000ULL;					      \
00db10
-       u.ieee.mantissa0 = (((mant) >> 32) & 0x7fffffff) | 0x80000000;	      \
00db10
-       u.ieee.mantissa1 = (mant) & 0xffffffff;				      \
00db10
-       (flt) = u.d;							      \
00db10
-  } while (0)
00db10
 
00db10
 #include <stdlib/strtod_l.c>
00db10
diff -rupN a/wcsmbs/Makefile b/wcsmbs/Makefile
00db10
--- a/wcsmbs/Makefile	2017-03-02 16:33:59.000000000 -0500
00db10
+++ b/wcsmbs/Makefile	2017-03-02 16:45:05.529638950 -0500
00db10
@@ -32,6 +32,7 @@ routines := wcscat wcschr wcscmp wcscpy
00db10
 	    wcstol wcstoul wcstoll wcstoull wcstod wcstold wcstof \
00db10
 	    wcstol_l wcstoul_l wcstoll_l wcstoull_l \
00db10
 	    wcstod_l wcstold_l wcstof_l \
00db10
+	    wcstod_nan wcstold_nan wcstof_nan \
00db10
 	    wcscoll wcsxfrm \
00db10
 	    wcwidth wcswidth \
00db10
 	    wcscoll_l wcsxfrm_l \
00db10
diff -rupN a/wcsmbs/wcstod_l.c b/wcsmbs/wcstod_l.c
00db10
--- a/wcsmbs/wcstod_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/wcsmbs/wcstod_l.c	2017-03-02 16:45:05.532638943 -0500
00db10
@@ -23,9 +23,6 @@
00db10
 
00db10
 extern double ____wcstod_l_internal (const wchar_t *, wchar_t **, int,
00db10
 				     __locale_t);
00db10
-extern unsigned long long int ____wcstoull_l_internal (const wchar_t *,
00db10
-						       wchar_t **, int, int,
00db10
-						       __locale_t);
00db10
 
00db10
 #define	USE_WIDE_CHAR	1
00db10
 
00db10
diff -rupN a/wcsmbs/wcstod_nan.c b/wcsmbs/wcstod_nan.c
00db10
--- a/wcsmbs/wcstod_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/wcsmbs/wcstod_nan.c	2017-03-02 16:45:05.535638936 -0500
00db10
@@ -0,0 +1,23 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Wide strings, double.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include "../stdlib/strtod_nan_wide.h"
00db10
+#include "../stdlib/strtod_nan_double.h"
00db10
+
00db10
+#define STRTOD_NAN __wcstod_nan
00db10
+#include "../stdlib/strtod_nan_main.c"
00db10
diff -rupN a/wcsmbs/wcstof_l.c b/wcsmbs/wcstof_l.c
00db10
--- a/wcsmbs/wcstof_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/wcsmbs/wcstof_l.c	2017-03-02 16:45:05.538638929 -0500
00db10
@@ -25,8 +25,5 @@
00db10
 
00db10
 extern float ____wcstof_l_internal (const wchar_t *, wchar_t **, int,
00db10
 				    __locale_t);
00db10
-extern unsigned long long int ____wcstoull_l_internal (const wchar_t *,
00db10
-						       wchar_t **, int, int,
00db10
-						       __locale_t);
00db10
 
00db10
 #include <stdlib/strtof_l.c>
00db10
diff -rupN a/wcsmbs/wcstof_nan.c b/wcsmbs/wcstof_nan.c
00db10
--- a/wcsmbs/wcstof_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/wcsmbs/wcstof_nan.c	2017-03-02 16:45:05.541638922 -0500
00db10
@@ -0,0 +1,23 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Wide strings, float.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include "../stdlib/strtod_nan_wide.h"
00db10
+#include "../stdlib/strtod_nan_float.h"
00db10
+
00db10
+#define STRTOD_NAN __wcstof_nan
00db10
+#include "../stdlib/strtod_nan_main.c"
00db10
diff -rupN a/wcsmbs/wcstold_l.c b/wcsmbs/wcstold_l.c
00db10
--- a/wcsmbs/wcstold_l.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ b/wcsmbs/wcstold_l.c	2017-03-02 16:45:05.544638915 -0500
00db10
@@ -24,8 +24,5 @@
00db10
 
00db10
 extern long double ____wcstold_l_internal (const wchar_t *, wchar_t **, int,
00db10
 					   __locale_t);
00db10
-extern unsigned long long int ____wcstoull_l_internal (const wchar_t *,
00db10
-						       wchar_t **, int, int,
00db10
-						       __locale_t);
00db10
 
00db10
 #include <strtold_l.c>
00db10
diff -rupN a/wcsmbs/wcstold_nan.c b/wcsmbs/wcstold_nan.c
00db10
--- a/wcsmbs/wcstold_nan.c	1969-12-31 19:00:00.000000000 -0500
00db10
+++ b/wcsmbs/wcstold_nan.c	2017-03-02 16:45:05.547638908 -0500
00db10
@@ -0,0 +1,30 @@
00db10
+/* Convert string for NaN payload to corresponding NaN.  Wide strings,
00db10
+   long double.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <math.h>
00db10
+
00db10
+/* This function is unused if long double and double have the same
00db10
+   representation.  */
00db10
+#ifndef __NO_LONG_DOUBLE_MATH
00db10
+# include "../stdlib/strtod_nan_wide.h"
00db10
+# include <strtod_nan_ldouble.h>
00db10
+
00db10
+# define STRTOD_NAN __wcstold_nan
00db10
+# include "../stdlib/strtod_nan_main.c"
00db10
+#endif