This is the mail archive of the libc-hacker@sourceware.org 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]
Other format: [Raw text]

[PATCH] Fix strtod handling of multi-byte thousands separator


Hi!

While looking at BZ#3855 I noticed that non-wide thousands separators can't
work properly if they are longer than one byte (as shown in the attached
testcase).  Fixed thusly:

2007-01-11  Jakub Jelinek  <jakub@redhat.com>

	* stdlib/strtod_l.c (____STRTOF_INTERNAL): Fix handling of multi-byte
	thousands separators.
	* stdlib/Makefile: Add rules to build and run tst-strtod4.
	* stdlib/tst-strtod4.c: New test.

--- libc/stdlib/strtod_l.c.jj	2007-01-11 17:10:27.000000000 +0100
+++ libc/stdlib/strtod_l.c	2007-01-11 17:10:16.000000000 +0100
@@ -651,10 +651,11 @@ ____STRTOF_INTERNAL (nptr, endptr, group
 	  if (c != '0')
 	    {
 	      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
-		if (c != thousands[cnt])
+		if (thousands[cnt] != cp[cnt])
 		  break;
 	      if (thousands[cnt] != '\0')
 		break;
+	      cp += cnt - 1;
 	    }
 	  c = *++cp;
 	}
@@ -725,6 +726,7 @@ ____STRTOF_INTERNAL (nptr, endptr, group
 		  break;
 	      if (thousands[cnt] != '\0')
 		break;
+	      cp += cnt - 1;
 	    }
 #endif
 	}
--- libc/stdlib/Makefile.jj	2007-01-10 17:04:12.000000000 +0100
+++ libc/stdlib/Makefile	2007-01-11 17:11:01.000000000 +0100
@@ -68,7 +68,7 @@ tests		:= tst-strtol tst-strtod testmb t
 		   tst-limits tst-rand48 bug-strtod tst-setcontext	    \
 		   test-a64l tst-qsort tst-system testmb2 bug-strtod2	    \
 		   tst-atof1 tst-atof2 tst-strtod2 tst-strtod3 tst-rand48-2 \
-		   tst-makecontext
+		   tst-makecontext tst-strtod4
 
 include ../Makeconfig
 
@@ -114,6 +114,7 @@ test-canon-ARGS = --test-dir=${common-ob
 
 tst-strtod-ENV = LOCPATH=$(common-objpfx)localedata
 tst-strtod3-ENV = LOCPATH=$(common-objpfx)localedata
+tst-strtod4-ENV = LOCPATH=$(common-objpfx)localedata
 testmb2-ENV = LOCPATH=$(common-objpfx)localedata
 
 # Run a test on the header files we use.
--- libc/stdlib/tst-strtod4.c.jj	2007-01-11 16:52:17.000000000 +0100
+++ libc/stdlib/tst-strtod4.c	2007-01-11 17:01:26.000000000 +0100
@@ -0,0 +1,56 @@
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define NBSP "\xc2\xa0"
+
+static const struct
+{
+  const char *in;
+  const char *out;
+  double expected;
+} tests[] =
+  {
+    { "000"NBSP"000"NBSP"000", "", 0.0 },
+    { "1"NBSP"000"NBSP"000,5x", "x", 1000000.5 }
+  };
+#define NTESTS (sizeof (tests) / sizeof (tests[0]))
+
+
+static int
+do_test (void)
+{
+  if (setlocale (LC_ALL, "cs_CZ.UTF-8") == NULL)
+    {
+      puts ("could not set locale");
+      return 1;
+    }
+
+  int status = 0;
+
+  for (int i = 0; i < NTESTS; ++i)
+    {
+      char *ep;
+      double r = __strtod_internal (tests[i].in, &ep, 1);
+
+      if (strcmp (ep, tests[i].out) != 0)
+	{
+	  printf ("%d: got rest string \"%s\", expected \"%s\"\n",
+		  i, ep, tests[i].out);
+	  status = 1;
+	}
+
+      if (r != tests[i].expected)
+	{
+	  printf ("%d: got wrong results %g, expected %g\n",
+		  i, r, tests[i].expected);
+	  status = 1;
+	}
+    }
+
+  return status;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"

	Jakub


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