bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#9128: [PATCH] Fix handling of non-numeric keys with '-n'


From: Zdenek Pavlas
Subject: bug#9128: [PATCH] Fix handling of non-numeric keys with '-n'
Date: Wed, 20 Jul 2011 05:01:08 -0400 (EDT)

There's a possible BUG when using '-n' that any non-numeric key is handled
as equal to '0'. This results in unexpected behavior eg. when using '-nu'.

$ for i in NUMBERS 10 9 0; do echo $i; done| sort -n
NUMBERS
0
9
10

$ for i in NUMBERS 10 9 0; do echo $i; done| sort -nu
NUMBERS
9
10

See the '0' is mysteriously gone.  I believe the semantics should be changed,
either by honoring the string representation of non-numeric key (see patch),
alternatively by ignoring all lines with non-numeric keys.

---
 src/sort.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/src/sort.c b/src/sort.c
index 3d3119d..58f96e6 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1907,6 +1907,17 @@ numcompare (char const *a, char const *b)
   while (blanks[to_uchar (*b)])
     b++;
 
+  /* Put non-numeric keys before all numbers.
+     If both non-numeric, don't return 0 unless equal. */
+  if (!ISDIGIT(*a) && *a != '-') {
+    if (!ISDIGIT(*b) && *b != '-')
+      return strcmp(a, b);
+    return -1;
+  }
+  else if (!ISDIGIT(*b) && *b != '-') {
+    return 1;
+  }
+
   return strnumcmp (a, b, decimal_point, thousands_sep);
 }
 
-- 
1.7.4.4





reply via email to

[Prev in Thread] Current Thread [Next in Thread]