git: 4cf7301e9683 - stable/13 - uniq(1): use strtonum to parse options

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 17 Jan 2024 18:28:40 UTC
The branch stable/13 has been updated by des:

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

commit 4cf7301e96833970cc42c6f75b8068eb51abdd37
Author:     Daniel Tameling <tamelingdaniel@gmail.com>
AuthorDate: 2023-02-25 17:25:51 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-01-17 16:16:01 +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
    
    (cherry picked from commit e052829e3e16dfd82d0adcbb69fd0e30f47a3a6c)
---
 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 59676a26e1f8..1513ae185bcf 100644
--- a/usr.bin/uniq/uniq.c
+++ b/usr.bin/uniq/uniq.c
@@ -100,7 +100,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, "");
@@ -129,14 +129,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;