]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cut: fix handling of overlapping ranges
authorCojocaru Alexandru <xojoc@gmx.com>
Tue, 7 May 2013 12:01:46 +0000 (13:01 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 7 May 2013 12:07:02 +0000 (13:07 +0100)
This issue was introduced in commit v8.21-43-g3e466ad

* src/cut.c (set_fields): Process all range pairs when merging.
* tests/misc/cut-huge-range.sh: Add a test for this edge case.
Also fix an issue where we could miss reported errors due
to truncation of the 'err' file.

src/cut.c
tests/misc/cut-huge-range.sh

index b347b307b397703fa93f381e017c0f5a13c8a97c..9501b3aa71d14ea24600aaedf1943e8462b43f9b 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -496,9 +496,9 @@ set_fields (const char *fieldstr)
           if (rp[j].lo <= rp[i].hi)
             {
               rp[i].hi = MAX (rp[j].hi, rp[i].hi);
-              memmove (rp + j, rp + j + 1,
-                       (n_rp - j - 1) * sizeof (struct range_pair));
-              --n_rp;
+              memmove (rp + j, rp + j + 1, (n_rp - j - 1) * sizeof *rp);
+              n_rp--;
+              j--;
             }
           else
             break;
index 887197ae9e078a993c1a15213ece3cc016915c53..9905cd7586cc0ed349d1082f474fbd27822d2a4a 100755 (executable)
@@ -27,7 +27,13 @@ getlimits_
 
 # Up to and including coreutils-8.21, cut would allocate possibly needed
 # memory upfront.  Subsequently memory is allocated as required.
-(ulimit -v 20000; : | cut -b1-$INT_MAX > err 2>&1) || fail=1
+(ulimit -v 20000; : | cut -b1-$INT_MAX >> err 2>&1) || fail=1
+
+# Ensure ranges are merged correctly when large range logic is in effect
+echo 1 > exp
+(dd bs=1MB if=/dev/zero count=1; echo '1') |
+cut -b1-1000000,2-3,4-5,1000001 2>>err | tail -c2 > out || fail=1
+compare exp out || fail=1
 
 compare /dev/null err || fail=1