]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
numfmt: fix isblank() usage for some unibyte locales
authorAssaf Gordon <assafgordon@gmail.com>
Tue, 15 Jul 2014 16:25:03 +0000 (12:25 -0400)
committerPádraig Brady <P@draigBrady.com>
Wed, 16 Jul 2014 21:35:17 +0000 (22:35 +0100)
* src/numfmt.c (simple_strtod_int): Replace isdigit() with c_isdigit()
to avoid locale concerns and -Wchar-subscripts warnings on cygwin.
Remove the now redundant locale guard.
(simple_strtod_human): Cast characters to unsigned so that the promoted
int value passed to isblank() is positive, allowing it to work correctly
for all characters in unibyte locales.  Previously character 0xA0,
i.e. non-breaking space, would be misclassified for example.
(process_suffixed_number): Likewise.
(skip_fields): Likewise.
Both issues were triggered by the -Wchar-subscripts warning on GCC 4.8.3
on cygwin, due to the is*() implementations used there, but the issue
is present on all platforms defaulting to signed chars.
* NEWS: Mention the bug fix.

Reported by Eric Blake

NEWS
src/numfmt.c

diff --git a/NEWS b/NEWS
index f691ab3cb65204741ba6274f6e9e2454d0aea931..7f40fad838ce8d21766afd8db7f660eb9a31583a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -86,6 +86,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   ln -sr '' F no longer segfaults.  Now works as expected.
   [bug introduced with the --relative feature in coreutils-8.16]
 
+  numfmt now handles blanks correctly in all unibyte locales.  Previously
+  in locales where character 0xA0 is a blank, numfmt would mishandle it.
+  [bug introduced when numfmt was added in coreutils-8.21]
+
   ptx --format long option parsing no longer falls through into the --help case.
   [bug introduced in TEXTUTILS-1_22i]
 
index 6091bb6bd5824f7d5806654165d9bb57fd44ac6f..206866ac9483f9c08cb91b010fe92bdc113ce849 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "mbsalign.h"
 #include "argmatch.h"
+#include "c-ctype.h"
 #include "error.h"
 #include "quote.h"
 #include "system.h"
@@ -454,14 +455,10 @@ simple_strtod_int (const char *input_str,
     *negative = false;
 
   *endptr = (char *) input_str;
-  while (*endptr && isdigit (**endptr))
+  while (*endptr && c_isdigit (**endptr))
     {
       int digit = (**endptr) - '0';
 
-      /* can this happen in some strange locale?  */
-      if (digit < 0 || digit > 9)
-        return SSE_INVALID_NUMBER;
-
       if (digits > MAX_UNSCALED_DIGITS)
         e = SSE_OK_PRECISION_LOSS;
 
@@ -600,7 +597,7 @@ simple_strtod_human (const char *input_str,
       /* process suffix.  */
 
       /* Skip any blanks between the number and suffix.  */
-      while (isblank (**endptr))
+      while (isblank (to_uchar (**endptr)))
         (*endptr)++;
 
       if (!valid_suffix (**endptr))
@@ -1174,7 +1171,7 @@ process_suffixed_number (char *text, long double *result, size_t *precision)
 
   /* Skip white space - always.  */
   char *p = text;
-  while (*p && isblank (*p))
+  while (*p && isblank (to_uchar (*p)))
     ++p;
   const unsigned int skip_count = text - p;
 
@@ -1229,9 +1226,9 @@ skip_fields (char *buf, int fields)
   else
     while (*ptr && fields--)
       {
-        while (*ptr && isblank (*ptr))
+        while (*ptr && isblank (to_uchar (*ptr)))
           ++ptr;
-        while (*ptr && !isblank (*ptr))
+        while (*ptr && !isblank (to_uchar (*ptr)))
           ++ptr;
       }
   return ptr;