aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-12-13 20:22:05 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2024-12-13 20:22:05 +0100
commit7624077772878db25d8221fc4d6f731e29ebcdba (patch)
treefcd18c1c42a303cd124a324b65df14460ba4ee2b
parentf4f8dc68645c061d4ccec5481bc3a659b02bc84f (diff)
downloadbusybox-w32-7624077772878db25d8221fc4d6f731e29ebcdba.tar.gz
busybox-w32-7624077772878db25d8221fc4d6f731e29ebcdba.tar.bz2
busybox-w32-7624077772878db25d8221fc4d6f731e29ebcdba.zip
cut: "it's legal to pass an empty list" seems to be untrue
function old new delta cut_main 1344 1339 -5 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/cut.c90
1 files changed, 44 insertions, 46 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 6eac7793f..9a99ad05c 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -336,61 +336,59 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
336 * valid list formats: N, N-, N-M, -M 336 * valid list formats: N, N-, N-M, -M
337 * more than one list can be separated by commas 337 * more than one list can be separated by commas
338 */ 338 */
339 { 339 /* take apart the lists, one by one (they are separated with commas) */
340 while ((ltok = strsep(&sopt, ",")) != NULL) {
340 char *ntok; 341 char *ntok;
341 int s = 0, e = 0; 342 int s, e;
342
343 /* take apart the lists, one by one (they are separated with commas) */
344 while ((ltok = strsep(&sopt, ",")) != NULL) {
345
346 /* it's actually legal to pass an empty list */
347 if (!ltok[0])
348 continue;
349 343
350 /* get the start pos */ 344 /* it's actually legal to pass an empty list */
351 ntok = strsep(&ltok, "-"); 345 //if (!ltok[0])
352 if (!ntok[0]) { 346 // continue;
353 s = 0; 347 //^^^ testcase?
354 } else {
355 /* account for the fact that arrays are zero based, while
356 * the user expects the first char on the line to be char #1 */
357 s = xatoi_positive(ntok) - 1;
358 }
359
360 /* get the end pos */
361 if (ltok == NULL) {
362 e = s;
363 } else if (!ltok[0]) {
364 /* if the user specified no end position,
365 * that means "til the end of the line" */
366 e = INT_MAX;
367 } else {
368 /* again, arrays are zero based, lines are 1 based */
369 e = xatoi_positive(ltok) - 1;
370 }
371 348
372 if (s < 0 || e < s) 349 /* get the start pos */
373 bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok); 350 ntok = strsep(&ltok, "-");
351 if (!ntok[0]) {
352 s = 0;
353 } else {
354 /* account for the fact that arrays are zero based, while
355 * the user expects the first char on the line to be char #1 */
356 s = xatoi_positive(ntok) - 1;
357 }
374 358
375 /* add the new list */ 359 /* get the end pos */
376 cut_list = xrealloc_vector(cut_list, 4, nlists); 360 if (ltok == NULL) {
377 /* NB: startpos is always >= 0 */ 361 e = s;
378 cut_list[nlists].startpos = s; 362 } else if (!ltok[0]) {
379 cut_list[nlists].endpos = e; 363 /* if the user specified no end position,
380 nlists++; 364 * that means "til the end of the line" */
365 e = INT_MAX;
366 } else {
367 /* again, arrays are zero based, lines are 1 based */
368 e = xatoi_positive(ltok) - 1;
381 } 369 }
382 370
383 /* make sure we got some cut positions out of all that */ 371 if (s < 0 || e < s)
384 if (nlists == 0) 372 bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok);
385 bb_simple_error_msg_and_die("missing list of positions");
386 373
387 /* now that the lists are parsed, we need to sort them to make life 374 /* add the new list */
388 * easier on us when it comes time to print the chars / fields / lines 375 cut_list = xrealloc_vector(cut_list, 4, nlists);
389 */ 376 /* NB: startpos is always >= 0 */
390 if (!(opt & OPT_NOSORT)) 377 cut_list[nlists].startpos = s;
391 qsort(cut_list, nlists, sizeof(cut_list[0]), cmpfunc); 378 cut_list[nlists].endpos = e;
379 nlists++;
392 } 380 }
393 381
382 /* make sure we got some cut positions out of all that */
383 if (nlists == 0)
384 bb_simple_error_msg_and_die("missing list of positions");
385
386 /* now that the lists are parsed, we need to sort them to make life
387 * easier on us when it comes time to print the chars / fields / lines
388 */
389 if (!(opt & OPT_NOSORT))
390 qsort(cut_list, nlists, sizeof(cut_list[0]), cmpfunc);
391
394#if ENABLE_FEATURE_CUT_REGEX 392#if ENABLE_FEATURE_CUT_REGEX
395 if (opt & OPT_REGEX) { 393 if (opt & OPT_REGEX) {
396 xregcomp(&reg, delim, REG_EXTENDED); 394 xregcomp(&reg, delim, REG_EXTENDED);