diff options
author | Job Snijders <job@cvs.openbsd.org> | 2021-09-01 21:43:52 +0000 |
---|---|---|
committer | Job Snijders <job@cvs.openbsd.org> | 2021-09-01 21:43:52 +0000 |
commit | 0dee97ecf05c4857a2f69dffd450788303255eec (patch) | |
tree | 5c41bbfa3e14a3edc60ce4cac6290a0fd3c36c45 /usr.bin/timeout | |
parent | b31a29ebe04967342b1272b96bc69755a1b18e7b (diff) |
Fix overflow / underflow check by moving it up before the return
Also rename 'end' to 'suffix' for readability.
OK beck@
Diffstat (limited to 'usr.bin/timeout')
-rw-r--r-- | usr.bin/timeout/timeout.c | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/usr.bin/timeout/timeout.c b/usr.bin/timeout/timeout.c index 768df18bc0d..1cbd00b8913 100644 --- a/usr.bin/timeout/timeout.c +++ b/usr.bin/timeout/timeout.c @@ -1,6 +1,6 @@ -/* $OpenBSD: timeout.c,v 1.10 2021/09/01 20:18:54 job Exp $ */ +/* $OpenBSD: timeout.c,v 1.11 2021/09/01 21:43:51 job Exp $ */ -/*- +/* * Copyright (c) 2021 Job Snijders <job@openbsd.org> * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org> * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org> @@ -63,19 +63,21 @@ static double parse_duration(const char *duration) { double ret; - char *end; + char *suffix; - ret = strtod(duration, &end); - if (ret == 0 && end == duration) + ret = strtod(duration, &suffix); + if (ret == 0 && suffix == duration) + err(1, "invalid duration"); + if (ret < 0 || ret >= 100000000UL) err(1, "invalid duration"); - if (end == NULL || *end == '\0') + if (suffix == NULL || *suffix == '\0') return (ret); - if (end != NULL && *(end + 1) != '\0') + if (suffix != NULL && *(suffix + 1) != '\0') err(1, "invalid duration"); - switch (*end) { + switch (*suffix) { case 's': break; case 'm': @@ -91,9 +93,6 @@ parse_duration(const char *duration) err(1, "invalid duration"); } - if (ret < 0 || ret >= 100000000UL) - err(1, "invalid duration"); - return (ret); } |