git: 78e30818a06c - stable/14 - lockf: switch to strtonum() for parsing timeout

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 15 Dec 2023 16:51:02 UTC
The branch stable/14 has been updated by kevans:

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

commit 78e30818a06c0cb934637811c3339ea8da9ed16a
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-11-26 04:27:11 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-12-15 00:58:33 +0000

    lockf: switch to strtonum() for parsing timeout
    
    Convert waitsec to a long long to be able to hold the full domain of
    alarm(3) timeout on all platforms, and let strtonum(3) handle the input
    validation.  strtonum(3) also happens to provide a neater interface for
    error handling, and it already includes our pre-existing empty input
    check.
    
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit e4967d4d48b0a7d873e4f778c0a6e560e09d4dc3)
---
 usr.bin/lockf/lockf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c
index db45f7a6b2e7..dd02bf2a5417 100644
--- a/usr.bin/lockf/lockf.c
+++ b/usr.bin/lockf/lockf.c
@@ -92,7 +92,8 @@ fdlock_implied(const char *name, long *ofd)
 int
 main(int argc, char **argv)
 {
-	int ch, flags, silent, status, waitsec;
+	int ch, flags, silent, status;
+	long long waitsec;
 	pid_t child;
 	union lock_subject subj;
 
@@ -112,9 +113,10 @@ main(int argc, char **argv)
 			break;
 		case 't':
 		{
-			char *endptr;
-			waitsec = strtol(optarg, &endptr, 0);
-			if (*optarg == '\0' || *endptr != '\0' || waitsec < 0)
+			const char *errstr;
+
+			waitsec = strtonum(optarg, 0, UINT_MAX, &errstr);
+			if (errstr != NULL)
 				errx(EX_USAGE,
 				    "invalid timeout \"%s\"", optarg);
 		}
@@ -181,7 +183,7 @@ main(int argc, char **argv)
 		sigemptyset(&act.sa_mask);
 		act.sa_flags = 0;	/* Note that we do not set SA_RESTART. */
 		sigaction(SIGALRM, &act, NULL);
-		alarm(waitsec);
+		alarm((unsigned int)waitsec);
 	}
 	/*
 	 * If the "-k" option is not given, then we must not block when