From d58ba21415b0e852a5faa721c74bf990c56e366a Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Mon, 8 Jul 2024 12:49:11 +0200 Subject: [PATCH] tools/nut-scanner/nut-scanner.c: `-t timeout`: fix to use strtol() not atol() [#2244] Signed-off-by: Jim Klimov --- tools/nut-scanner/nut-scanner.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/nut-scanner/nut-scanner.c b/tools/nut-scanner/nut-scanner.c index 97effe3dca..0c208911e8 100644 --- a/tools/nut-scanner/nut-scanner.c +++ b/tools/nut-scanner/nut-scanner.c @@ -957,12 +957,28 @@ int main(int argc, char *argv[]) switch(opt_ret) { case 't': - timeout = (useconds_t)atol(optarg) * 1000 * 1000; /*in usec*/ - if (timeout <= 0) { - fprintf(stderr, - "Illegal timeout value, using default %ds\n", - DEFAULT_NETWORK_TIMEOUT); - timeout = DEFAULT_NETWORK_TIMEOUT * 1000 * 1000; + { // scoping + long l; + char *s = NULL; + int errno_saved; + + errno = 0; + l = strtol(optarg, &s, 10); + errno_saved = errno; + upsdebugx(6, "errno=%d s='%s'(%p) input='%s'(%p) output=%ld", + errno_saved, NUT_STRARG(s), (void *)s, + optarg, (void *)(optarg), l); + + if (errno_saved || (s && *s != '\0') || l <= 0) { + /* TODO: Any max limit? At least, + * max(useconds_t)/1000000 ? */ + fprintf(stderr, + "Illegal timeout value, using default %ds\n", + DEFAULT_NETWORK_TIMEOUT); + timeout = DEFAULT_NETWORK_TIMEOUT * 1000 * 1000; + } else { + timeout = (useconds_t)l * 1000 * 1000; /*in usec*/ + } } break; case 's':