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.
This commit is contained in:
Kyle Evans 2023-11-25 22:27:11 -06:00
parent 09a7fe0a55
commit e4967d4d48

View file

@ -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