uniq(1): use strtonum to parse options

Previously strtol was used and the result was directly cast to an int
without checking for an overflow. Use strtonum instead since it is
safer and tells us what went wrong.

Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/643
This commit is contained in:
Daniel Tameling 2023-02-25 10:25:51 -07:00 committed by Warner Losh
parent 4d59545d0c
commit e052829e3e

View File

@ -102,7 +102,7 @@ main (int argc, char *argv[])
int ch, comp;
size_t prevbuflen, thisbuflen, b1;
char *prevline, *thisline, *p;
const char *ifn;
const char *ifn, *errstr;;
cap_rights_t rights;
(void) setlocale(LC_ALL, "");
@ -131,14 +131,14 @@ main (int argc, char *argv[])
iflag = 1;
break;
case 'f':
numfields = strtol(optarg, &p, 10);
if (numfields < 0 || *p)
errx(1, "illegal field skip value: %s", optarg);
numfields = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr)
errx(1, "field skip value is %s: %s", errstr, optarg);
break;
case 's':
numchars = strtol(optarg, &p, 10);
if (numchars < 0 || *p)
errx(1, "illegal character skip value: %s", optarg);
numchars = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "character skip value is %s: %s", errstr, optarg);
break;
case 'u':
uflag = 1;