git: e052829e3e16 - main - uniq(1): use strtonum to parse options

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sat, 25 Feb 2023 17:27:09 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=e052829e3e16dfd82d0adcbb69fd0e30f47a3a6c

commit e052829e3e16dfd82d0adcbb69fd0e30f47a3a6c
Author:     Daniel Tameling <tamelingdaniel@gmail.com>
AuthorDate: 2023-02-25 17:25:51 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-02-25 17:25:51 +0000

    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 5d2a7c1f2245..36fb037df283 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;