Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 Apr 2025 19:46:23 GMT
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: e7bf1e5f1d62 - main - timeout(1): Improve duration parsing and error messages
Message-ID:  <202504161946.53GJkNOu063495@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by bapt:

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

commit e7bf1e5f1d62c2e735d343c462275e7e2aaf0286
Author:     Aaron LI <aly@aaronly.me>
AuthorDate: 2025-04-02 11:20:02 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2025-04-16 19:45:37 +0000

    timeout(1): Improve duration parsing and error messages
    
    Obtained-from: OpenBSD (via DragonFly BSD)
---
 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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202504161946.53GJkNOu063495>