diff options
author | Ron Yorston <rmy@pobox.com> | 2024-10-22 09:15:18 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-10-22 09:15:18 +0100 |
commit | b2ad30bab23920704a34dc39b16db6fd5d91ea1e (patch) | |
tree | bbcf8eaabd995478f8e1eb3f66a43d22dc25320d | |
parent | f0dea66749bb599f65762991f4fded229c3df5a3 (diff) | |
download | busybox-w32-b2ad30bab23920704a34dc39b16db6fd5d91ea1e.tar.gz busybox-w32-b2ad30bab23920704a34dc39b16db6fd5d91ea1e.tar.bz2 busybox-w32-b2ad30bab23920704a34dc39b16db6fd5d91ea1e.zip |
cut: detect error when bounds are reversed
The command 'cut -b 3-2' failed to detect that the bounds were
incorrectly ordered, though the check worked when the difference
between the bounds was larger.
The comparison was made after the lower bound has been decremented
but before the upper bound had.
Adds 0-16 bytes.
(GitHub issue #467)
-rw-r--r-- | coreutils/cut.c | 7 | ||||
-rwxr-xr-x | testsuite/cut.tests | 5 |
2 files changed, 11 insertions, 1 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c index 067e69b12..8cae2eca3 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -291,9 +291,16 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
291 | * that means "til the end of the line" */ | 291 | * that means "til the end of the line" */ |
292 | if (!*ltok) | 292 | if (!*ltok) |
293 | e = INT_MAX; | 293 | e = INT_MAX; |
294 | #if !ENABLE_PLATFORM_MINGW32 | ||
294 | else if (e < s) | 295 | else if (e < s) |
295 | bb_error_msg_and_die("%d<%d", e, s); | 296 | bb_error_msg_and_die("%d<%d", e, s); |
296 | e--; /* again, arrays are zero based, lines are 1 based */ | 297 | e--; /* again, arrays are zero based, lines are 1 based */ |
298 | #else | ||
299 | else if (e != 0) | ||
300 | e--; /* again, zero based arrays, one based lines */ | ||
301 | if (e < s) | ||
302 | bb_error_msg_and_die("%d<%d", e, s); | ||
303 | #endif | ||
297 | } | 304 | } |
298 | 305 | ||
299 | /* add the new list */ | 306 | /* add the new list */ |
diff --git a/testsuite/cut.tests b/testsuite/cut.tests index 2458c019c..a31f41f7f 100755 --- a/testsuite/cut.tests +++ b/testsuite/cut.tests | |||
@@ -31,7 +31,10 @@ testing "-b encapsulated" "cut -b 3-8,4-6 input" "e:two:\npha:be\ne quic\n" \ | |||
31 | #testing "cut -bO overlaps" \ | 31 | #testing "cut -bO overlaps" \ |
32 | # "cut --output-delimiter ' ' -b 1-3,2-5,7-9,9-10 input" \ | 32 | # "cut --output-delimiter ' ' -b 1-3,2-5,7-9,9-10 input" \ |
33 | # "one:t o:th\nalpha beta\nthe q ick \n" "$abc" "" | 33 | # "one:t o:th\nalpha beta\nthe q ick \n" "$abc" "" |
34 | testing "cut high-low error" "cut -b 8-3 abc.txt 2>/dev/null || echo err" "err\n" \ | 34 | testing "cut high-low error" "cut -b 8-3 input 2>/dev/null || echo err" "err\n" \ |
35 | "$abc" "" | ||
36 | |||
37 | testing "cut -b 2-1 error" "cut -b 2-1 input 2>/dev/null || echo err" "err\n" \ | ||
35 | "$abc" "" | 38 | "$abc" "" |
36 | 39 | ||
37 | testing "cut -c a-b" "cut -c 4-10 input" ":two:th\nha:beta\n quick \n" "$abc" "" | 40 | testing "cut -c a-b" "cut -c 4-10 input" ":two:th\nha:beta\n quick \n" "$abc" "" |