This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[PATCH] Fix strtod and *scanf


Hi!

strtod("Infinity", &x) incorrectly points x to 'i', not '\0',
sscanf("+Inf","%e") incorrectly returns 0 (ISO C99 requires that it handles
optionally signed number, infinity, ..., and that it should accept the same
thing as strtod). This patch should fix it.

2001-08-06  Jakub Jelinek  <jakub@redhat.com>

	* stdlib/strtod.c (STRTOF): Skip whole infinity, not just inf.
	* stdio-common/vfscanf.c (__vfscanf): +- can be followed by
	i in +-Inf.

	* stdlib/tst-strtod.c (tests): Add Inf tests.
	* stdio-common/tstscanf.c (main): Add tests for +- before
	Inf.

--- libc/stdlib/strtod.c.jj	Mon Aug  6 17:41:53 2001
+++ libc/stdlib/strtod.c	Mon Aug  6 19:19:31 2001
@@ -573,15 +573,14 @@ INTERNAL (STRTOF) (nptr, endptr, group L
 #endif
   else if (c < L_('0') || c > L_('9'))
     {
-      int matched = 0;
       /* Check for `INF' or `INFINITY'.  */
-      if (TOLOWER (c) == L_('i')
-	  && ((STRNCASECMP (cp, L_("inf"), 3) == 0 && (matched = 3))
-	      || (STRNCASECMP (cp, L_("infinity"), 8) == 0 && (matched = 8))))
+      if (TOLOWER (c) == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
 	{
 	  /* Return +/- infinity.  */
 	  if (endptr != NULL)
-	    *endptr = (STRING_TYPE *) (cp + matched);
+	    *endptr = (STRING_TYPE *)
+		      (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
+			     ? 8 : 3));
 
 	  return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
 	}
--- libc/stdlib/tst-strtod.c.jj	Wed Jan 31 16:35:06 2001
+++ libc/stdlib/tst-strtod.c	Mon Aug  6 19:14:49 2001
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
+#include <math.h>
 
 struct ltest
   {
@@ -64,6 +65,9 @@ static const struct ltest tests[] =
     { "0x0.8p-1022",
       1.11253692925360069154511635866620203210960799023116591527666e-308,
       '\0', 0 },
+    { "Inf", HUGE_VAL, '\0', 0 },
+    { "-Inf", -HUGE_VAL, '\0', 0 },
+    { "+InFiNiTy", HUGE_VAL, '\0', 0 },
     { NULL, 0, '\0', 0 }
   };
 
--- libc/stdio-common/vfscanf.c.jj	Mon Aug  6 18:14:59 2001
+++ libc/stdio-common/vfscanf.c	Mon Aug  6 18:20:45 2001
@@ -1604,7 +1604,7 @@ __vfscanf (FILE *s, const char *format, 
 	      if (width == 0 || inchar () == EOF)
 		/* EOF is only an input error before we read any chars.  */
 		conv_error ();
-	      if (! ISDIGIT (c))
+	      if (! ISDIGIT (c) && TOLOWER (c) != L_('i'))
 		{
 #ifdef COMPILE_WSCANF
 		  if (c != decimal)
--- libc/stdio-common/tstscanf.c.jj	Wed Jan 31 16:35:05 2001
+++ libc/stdio-common/tstscanf.c	Mon Aug  6 19:01:12 2001
@@ -21,6 +21,7 @@
 #else
 #include <stdio.h>
 #endif
+#include <math.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -331,6 +332,25 @@ main (int argc, char **argv)
       }
   }
 
+  fputs ("Test 13:\n", stdout);
+  {
+    float value;
+    int res;
+
+    res = sscanf ("-InF", "%f", &value);
+    if (res != 1 || isinf (value) != -1)
+      {
+	fputs ("test failed!\n", stdout);
+	result = 1;
+      }
+
+    res = sscanf ("+InfiNiTY", "%f", &value);
+    if (res != 1 || isinf (value) != 1)
+      {
+	fputs ("test failed!\n", stdout);
+	result = 1;
+      }
+  }
 
   return result;
 }

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]