git: b57a0597fc1a - stable/14 - timeout(1): Improve duration parsing and error messages

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 16 Jun 2025 08:53:53 UTC
The branch stable/14 has been updated by kib:

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

commit b57a0597fc1adc207cd5c4298288916c65699908
Author:     Aaron LI <aly@aaronly.me>
AuthorDate: 2025-04-02 11:20:02 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-06-16 08:51:51 +0000

    timeout(1): Improve duration parsing and error messages
    
    (cherry picked from commit e7bf1e5f1d62c2e735d343c462275e7e2aaf0286)
---
 bin/timeout/timeout.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/bin/timeout/timeout.c b/bin/timeout/timeout.c
index 429ca64349e3..83893ba0a601 100644
--- a/bin/timeout/timeout.c
+++ b/bin/timeout/timeout.c
@@ -68,19 +68,19 @@ static double
 parse_duration(const char *duration)
 {
 	double ret;
-	char *end;
+	char *suffix;
 
-	ret = strtod(duration, &end);
-	if (ret == 0 && end == duration)
-		errx(EXIT_INVALID, "invalid duration");
+	ret = strtod(duration, &suffix);
+	if (suffix == duration)
+		errx(EXIT_INVALID, "duration is not a number");
 
-	if (end == NULL || *end == '\0')
+	if (*suffix == '\0')
 		return (ret);
 
-	if (end != NULL && *(end + 1) != '\0')
-		errx(EXIT_INVALID, "invalid duration");
+	if (suffix[1] != '\0')
+		errx(EXIT_INVALID, "duration unit suffix too long");
 
-	switch (*end) {
+	switch (*suffix) {
 	case 's':
 		break;
 	case 'm':
@@ -93,11 +93,11 @@ parse_duration(const char *duration)
 		ret *= 60 * 60 * 24;
 		break;
 	default:
-		errx(EXIT_INVALID, "invalid duration");
+		errx(EXIT_INVALID, "duration unit suffix invalid");
 	}
 
 	if (ret < 0 || ret >= 100000000UL)
-		errx(EXIT_INVALID, "invalid duration");
+		errx(EXIT_INVALID, "duration out of range");
 
 	return (ret);
 }