summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorkamil <kamil@NetBSD.org>2018-12-06 06:29:56 +0000
committerkamil <kamil@NetBSD.org>2018-12-06 06:29:56 +0000
commit4276baeae125ae66027229dc8aac12422d01ca67 (patch)
treecb95cc157e80556e216a538bc1cef903cdab7f4e /lib/libc/stdlib
parenta386bc3bdb6763bf0f97b9b1cfe6992729e1c3e7 (diff)
Correct handling of minval > maxval in strtonum(3)
The original implementation in OpenBSD returns "invalid" and avoids reading the input string. The replaced behavior was interpreting the input string ignoring the invalid arguments.
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/strtonum.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/libc/stdlib/strtonum.c b/lib/libc/stdlib/strtonum.c
index a9bd13a3540..35ae6d2bda6 100644
--- a/lib/libc/stdlib/strtonum.c
+++ b/lib/libc/stdlib/strtonum.c
@@ -1,4 +1,4 @@
-/* $NetBSD: strtonum.c,v 1.5 2018/01/04 20:57:29 kamil Exp $ */
+/* $NetBSD: strtonum.c,v 1.6 2018/12/06 06:29:56 kamil Exp $ */
/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
* All rights reserved.
@@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: strtonum.c,v 1.5 2018/01/04 20:57:29 kamil Exp $");
+__RCSID("$NetBSD: strtonum.c,v 1.6 2018/12/06 06:29:56 kamil Exp $");
#include "namespace.h"
@@ -50,6 +50,11 @@ strtonum(const char *nptr, long long minval, long long maxval,
if (errstr == NULL)
errstr = &resp;
+ if (minval > maxval) {
+ *errstr = "invalid";
+ return 0;
+ }
+
rv = (long long)strtoi(nptr, NULL, 10, minval, maxval, &e);
if (e == 0) {