diff options
author | Ron Yorston <rmy@pobox.com> | 2024-10-28 07:57:29 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-10-28 07:57:29 +0000 |
commit | e697a94a2d19c1e74dc2efb168f4c40aeb30e446 (patch) | |
tree | 4af4f5b10b1bc047974e4a62fe9d314d17207db4 | |
parent | a9cf4418d930ba38e3a9e000342fb7fcbd26e8ca (diff) | |
download | busybox-w32-e697a94a2d19c1e74dc2efb168f4c40aeb30e446.tar.gz busybox-w32-e697a94a2d19c1e74dc2efb168f4c40aeb30e446.tar.bz2 busybox-w32-e697a94a2d19c1e74dc2efb168f4c40aeb30e446.zip |
cut: improve detection of invalid ranges
Commit 0068ce2fa (cut: add toybox-compatible options -O OUTSEP,
-D, -F LIST) added detection of reversed ranges. Further
improvements are possible.
- The test for reversed ranges compared the start after it had been
decremented with the end before decrement. It thus missed ranges
of the form 2-1.
- Zero isn't a valid start value for a range. (Nor is it a valid
end value, but that's caught by the test for a reversed range.)
- The code
if (!*ltok)
e = INT_MAX;
duplicates a check that's already been made.
- Display the actual range in the error message to make it easier
to find which range was at fault.
Adds 0-48 bytes.
(GitHub issue #467)
-rw-r--r-- | coreutils/cut.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c index 8cae2eca3..f7b501a46 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -276,8 +276,12 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
276 | s = xatoi_positive(ntok); | 276 | s = xatoi_positive(ntok); |
277 | /* account for the fact that arrays are zero based, while | 277 | /* account for the fact that arrays are zero based, while |
278 | * the user expects the first char on the line to be char #1 */ | 278 | * the user expects the first char on the line to be char #1 */ |
279 | #if !ENABLE_PLATFORM_MINGW32 | ||
279 | if (s != 0) | 280 | if (s != 0) |
280 | s--; | 281 | s--; |
282 | #else | ||
283 | s--; | ||
284 | #endif | ||
281 | } | 285 | } |
282 | 286 | ||
283 | /* get the end pos */ | 287 | /* get the end pos */ |
@@ -287,21 +291,20 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
287 | e = INT_MAX; | 291 | e = INT_MAX; |
288 | } else { | 292 | } else { |
289 | e = xatoi_positive(ltok); | 293 | e = xatoi_positive(ltok); |
290 | /* if the user specified and end position of 0, | 294 | /* if the user specified no end position, |
291 | * that means "til the end of the line" */ | 295 | * that means "til the end of the line" */ |
296 | #if !ENABLE_PLATFORM_MINGW32 | ||
292 | if (!*ltok) | 297 | if (!*ltok) |
293 | e = INT_MAX; | 298 | e = INT_MAX; |
294 | #if !ENABLE_PLATFORM_MINGW32 | ||
295 | else if (e < s) | 299 | else if (e < s) |
296 | bb_error_msg_and_die("%d<%d", e, s); | 300 | bb_error_msg_and_die("%d<%d", e, s); |
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 | 301 | #endif |
302 | e--; /* again, arrays are zero based, lines are 1 based */ | ||
304 | } | 303 | } |
304 | #if ENABLE_PLATFORM_MINGW32 | ||
305 | if (s < 0 || e < s) | ||
306 | bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok); | ||
307 | #endif | ||
305 | 308 | ||
306 | /* add the new list */ | 309 | /* add the new list */ |
307 | cut_lists = xrealloc_vector(cut_lists, 4, nlists); | 310 | cut_lists = xrealloc_vector(cut_lists, 4, nlists); |