bug-coreutils
[Top][All Lists]
Advanced

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

Re: bug in "sort -n -k1.4,1.2"


From: Pádraig Brady
Subject: Re: bug in "sort -n -k1.4,1.2"
Date: Fri, 12 Jun 2009 10:44:40 +0100
User-agent: Thunderbird 2.0.0.6 (X11/20071008)

Cliff Miller wrote:
> hi,
> 
> i have a bug to report in coreutils-7.4.  the problem occurs with
> empty fields under -n/-g, specifically in sub-field specifications where
> end < start.  according to the docs, 
> 
>> If the start position in a sort field specifier falls after the end of
>> the line or after the end field, the field is empty.
> 
> "after the end field" seems a little unclear; i take it to mean
> "after the end [position] of the field", i.e., lima < texta.

That is a little ambiguous. One could interpret that as
only pertaining to the input data. I.E. one could give
an error when the user specifies the end position before start.
But I think from the existing code, your interpretation is correct.
I also got some friends to check this on solaris and that
just ignores the fields in this case.

> here is a patch.  one could also change the null-insertion code
> in the numeric test branch, but it seems better to correctly set
> lima and limb from the start.

I agree. I tweaked it a little, and added 2 tests.

patch is attached.

thanks!
Pádraig.
>From 167d33f0dcda09c42b529aad990884e5db15b16d Mon Sep 17 00:00:00 2001
From: Cliff Miller <address@hidden>
Date: Thu, 11 Jun 2009 18:30:32 +0100
Subject: [PATCH] sort: Ignore fields where end position is before the start 
position
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

* NEWS: Mention the fix
* THANKS: Add Cliff Miller
* src/sort.c (keycompare): Ensure lima >= texta
* tests/misc/sort: Add 2 correspoding tests

Signed-off-by: Pádraig Brady <address@hidden>
---
 NEWS            |    5 +++++
 THANKS          |    1 +
 src/sort.c      |    8 ++++++--
 tests/misc/sort |    3 +++
 4 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 0455d59..d7695e4 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   truncate -s failed to skip all whitespace in the option argument in
   some locales.
 
+  sort now correctly ignores fields whose ending position is specified
+  before the start position. Previously in numeric mode the remaining
+  part of the line after the start position was used as the sort key.
+  [This bug appears to have been present in "the beginning".]
+
 ** Changes in behavior
 
   ls --color: files with multiple hard links are no longer colored differently
diff --git a/THANKS b/THANKS
index 4392f04..bcd88f0 100644
--- a/THANKS
+++ b/THANKS
@@ -112,6 +112,7 @@ Christophe LYON                     address@hidden
 Chuck Hedrick                       address@hidden
 Clark Morgan                        address@hidden
 Clement Wang                        address@hidden
+Cliff Miller                        address@hidden
 Colin Plumb                         address@hidden
 Colin Watson                        address@hidden
 Collin Rogowski                     address@hidden
diff --git a/src/sort.c b/src/sort.c
index d571ddf..6acec07 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1998,9 +1998,13 @@ keycompare (const struct line *a, const struct line *b)
       char const *translate = key->translate;
       bool const *ignore = key->ignore;
 
+      /* Treat field ends before field starts as empty fields.  */
+      lima = MAX (texta, lima);
+      limb = MAX (textb, limb);
+
       /* Find the lengths. */
-      size_t lena = lima <= texta ? 0 : lima - texta;
-      size_t lenb = limb <= textb ? 0 : limb - textb;
+      size_t lena = lima - texta;
+      size_t lenb = limb - textb;
 
       /* Actually compare the fields. */
 
diff --git a/tests/misc/sort b/tests/misc/sort
index ae3bd8e..418edec 100755
--- a/tests/misc/sort
+++ b/tests/misc/sort
@@ -134,6 +134,9 @@ my @Tests =
 ["07d", '+1 -3', {IN=>"y k b\nz k a\n"}, {OUT=>"z k a\ny k b\n"}],
 # ensure a character position of 0 includes whole field
 ["07e", '-k 2,3.0', {IN=>"a a b\nz a a\n"}, {OUT=>"z a a\na a b\n"}],
+# ensure fields with end position before start are ignored
+["07f", '-n -k1.3,1.1', {IN=>"a 2\nb 1\n"}, {OUT=>"a 2\nb 1\n"}],
+["07g", '-n -k2.2,1.2', {IN=>"aa 2\nbb 1\n"}, {OUT=>"aa 2\nbb 1\n"}],
 #
 # report an error for `.' without following char spec
 ["08a", '-k 2.,3', {EXIT=>2},
-- 
1.5.3.6


reply via email to

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