From c3eb16d014687b088da5e0351028cf82653ba4be Mon Sep 17 00:00:00 2001 From: job Date: Wed, 1 Sep 2021 21:43:51 +0000 Subject: [PATCH] Fix overflow / underflow check by moving it up before the return Also rename 'end' to 'suffix' for readability. OK beck@ --- usr.bin/timeout/timeout.c | 21 ++++++++++----------- 1 file 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 * Copyright (c) 2014 Baptiste Daroussin * Copyright (c) 2014 Vsevolod Stakhov @@ -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); } -- 2.20.1