aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-12-15 12:26:07 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2024-12-15 12:27:33 +0100
commite8622f0d8524baf8a08a734b7d0fb6aac31b5ee2 (patch)
tree32a6194741470a204569667213a00e1ec9363f0c
parentb25ea3f156c6b5927b358f9b12b90f1852f30311 (diff)
downloadbusybox-w32-e8622f0d8524baf8a08a734b7d0fb6aac31b5ee2.tar.gz
busybox-w32-e8622f0d8524baf8a08a734b7d0fb6aac31b5ee2.tar.bz2
busybox-w32-e8622f0d8524baf8a08a734b7d0fb6aac31b5ee2.zip
cut: disallow -f '' and -f '-'
function old new delta cut_main 1391 1410 +19 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/cut.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 3abebe7ad..20138075c 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -85,8 +85,8 @@
85#define opt_REGEX (option_mask32 & OPT_REGEX) 85#define opt_REGEX (option_mask32 & OPT_REGEX)
86 86
87struct cut_list { 87struct cut_list {
88 int startpos; 88 unsigned startpos;
89 int endpos; 89 unsigned endpos;
90}; 90};
91 91
92static int cmpfunc(const void *a, const void *b) 92static int cmpfunc(const void *a, const void *b)
@@ -116,7 +116,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
116 116
117 /* print the chars specified in each cut list */ 117 /* print the chars specified in each cut list */
118 for (; cl_pos < nlists; cl_pos++) { 118 for (; cl_pos < nlists; cl_pos++) {
119 int spos; 119 unsigned spos;
120 for (spos = cut_list[cl_pos].startpos; spos < linelen;) { 120 for (spos = cut_list[cl_pos].startpos; spos < linelen;) {
121 if (!printed[spos]) { 121 if (!printed[spos]) {
122 printed[spos] = 'X'; 122 printed[spos] = 'X';
@@ -171,7 +171,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
171 /* Cut by fields */ 171 /* Cut by fields */
172 } else { 172 } else {
173 unsigned next = 0, start = 0, end = 0; 173 unsigned next = 0, start = 0, end = 0;
174 int dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */ 174 unsigned dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */
175 175
176 /* Blank line? Check -s (later check for -s does not catch empty lines) */ 176 /* Blank line? Check -s (later check for -s does not catch empty lines) */
177 if (linelen == 0) { 177 if (linelen == 0) {
@@ -340,10 +340,10 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
340 340
341 /* 341 /*
342 * parse list and put values into startpos and endpos. 342 * parse list and put values into startpos and endpos.
343 * valid list formats: N, N-, N-M, -M 343 * valid range formats: N, N-, N-M, -M
344 * more than one list can be separated by commas 344 * more than one range can be separated by commas
345 */ 345 */
346 /* take apart the lists, one by one (they are separated with commas) */ 346 /* take apart the ranges, one by one (separated with commas) */
347 while ((ltok = strsep(&sopt, ",")) != NULL) { 347 while ((ltok = strsep(&sopt, ",")) != NULL) {
348 char *ntok; 348 char *ntok;
349 int s, e; 349 int s, e;
@@ -356,22 +356,26 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
356 /* get the start pos */ 356 /* get the start pos */
357 ntok = strsep(&ltok, "-"); 357 ntok = strsep(&ltok, "-");
358 if (!ntok[0]) { 358 if (!ntok[0]) {
359 s = 0; 359 if (!ltok) /* testcase: -f '' */
360 bb_show_usage();
361 if (!ltok[0]) /* testcase: -f - */
362 bb_show_usage();
363 s = 0; /* "-M" means "1-M" */
360 } else { 364 } else {
361 /* account for the fact that arrays are zero based, while 365 /* "N" or "N-[M]" */
362 * the user expects the first char on the line to be char #1 */ 366 /* arrays are zero based, while the user expects
367 * the first field/char on the line to be char #1 */
363 s = xatoi_positive(ntok) - 1; 368 s = xatoi_positive(ntok) - 1;
364 } 369 }
365 370
366 /* get the end pos */ 371 /* get the end pos */
367 if (ltok == NULL) { 372 if (!ltok) {
368 e = s; 373 e = s; /* "N" means "N-N" */
369 } else if (!ltok[0]) { 374 } else if (!ltok[0]) {
370 /* if the user specified no end position, 375 /* "N-" means "until the end of the line" */
371 * that means "til the end of the line" */
372 e = INT_MAX; 376 e = INT_MAX;
373 } else { 377 } else {
374 /* again, arrays are zero based, lines are 1 based */ 378 /* again, arrays are zero based, fields are 1 based */
375 e = xatoi_positive(ltok) - 1; 379 e = xatoi_positive(ltok) - 1;
376 } 380 }
377 381