From e052829e3e16dfd82d0adcbb69fd0e30f47a3a6c Mon Sep 17 00:00:00 2001 From: Daniel Tameling Date: Sat, 25 Feb 2023 10:25:51 -0700 Subject: [PATCH] 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 --- usr.bin/uniq/uniq.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/usr.bin/uniq/uniq.c b/usr.bin/uniq/uniq.c index 5d2a7c1f224..36fb037df28 100644 --- a/usr.bin/uniq/uniq.c +++ b/usr.bin/uniq/uniq.c @@ -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;