summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2014-09-13 20:10:13 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2014-09-13 20:10:13 +0000
commit2dcb748989a77eda5919f6460bbcee0365c5538d (patch)
tree287dc35292ac2a824f536a0c130424ea6018fa0e /lib/libc
parent5de60ac521905d61b6fc62afc6ed7be616694051 (diff)
Make sure that the following functions return 0 and EINVAL as
required by the C standard when called with an invalid base: strtoll(), strtoimax(), strtoul(), strtoull(), and strtoumax(). Same behaviour for strtoq() and strtouq() even though not standardized. No functional change in strtol(), it was the only one already correct. While here, simplify the conditional expression for checking the base and sync whitespace and comments among the six files. ok millert@
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdlib/strtoimax.c16
-rw-r--r--lib/libc/stdlib/strtol.c7
-rw-r--r--lib/libc/stdlib/strtoll.c15
-rw-r--r--lib/libc/stdlib/strtoul.c11
-rw-r--r--lib/libc/stdlib/strtoull.c15
-rw-r--r--lib/libc/stdlib/strtoumax.c16
6 files changed, 60 insertions, 20 deletions
diff --git a/lib/libc/stdlib/strtoimax.c b/lib/libc/stdlib/strtoimax.c
index 2c77f416503..2fc04e48506 100644
--- a/lib/libc/stdlib/strtoimax.c
+++ b/lib/libc/stdlib/strtoimax.c
@@ -1,6 +1,5 @@
-/* $OpenBSD: strtoimax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
-
-/*-
+/* $OpenBSD: strtoimax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $ */
+/*
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
@@ -48,6 +47,17 @@ strtoimax(const char *nptr, char **endptr, int base)
int neg, any, cutlim;
/*
+ * Ensure that base is between 2 and 36 inclusive, or the special
+ * value of 0.
+ */
+ if (base < 0 || base == 1 || base > 36) {
+ if (endptr != 0)
+ *endptr = (char *)nptr;
+ errno = EINVAL;
+ return 0;
+ }
+
+ /*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
diff --git a/lib/libc/stdlib/strtol.c b/lib/libc/stdlib/strtol.c
index dc2cf8871cc..86cec350864 100644
--- a/lib/libc/stdlib/strtol.c
+++ b/lib/libc/stdlib/strtol.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: strtol.c,v 1.9 2013/04/17 17:40:35 tedu Exp $ */
-/*-
+/* $OpenBSD: strtol.c,v 1.10 2014/09/13 20:10:12 schwarze Exp $ */
+/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
@@ -33,7 +33,6 @@
#include <limits.h>
#include <stdlib.h>
-
/*
* Convert a string to a long integer.
*
@@ -52,7 +51,7 @@ strtol(const char *nptr, char **endptr, int base)
* Ensure that base is between 2 and 36 inclusive, or the special
* value of 0.
*/
- if (base != 0 && (base < 2 || base > 36)) {
+ if (base < 0 || base == 1 || base > 36) {
if (endptr != 0)
*endptr = (char *)nptr;
errno = EINVAL;
diff --git a/lib/libc/stdlib/strtoll.c b/lib/libc/stdlib/strtoll.c
index 4bcc5565be2..cf82c8e1a6f 100644
--- a/lib/libc/stdlib/strtoll.c
+++ b/lib/libc/stdlib/strtoll.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: strtoll.c,v 1.7 2013/03/28 18:09:38 martynas Exp $ */
-/*-
+/* $OpenBSD: strtoll.c,v 1.8 2014/09/13 20:10:12 schwarze Exp $ */
+/*
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
@@ -50,6 +50,17 @@ strtoll(const char *nptr, char **endptr, int base)
int neg, any, cutlim;
/*
+ * Ensure that base is between 2 and 36 inclusive, or the special
+ * value of 0.
+ */
+ if (base < 0 || base == 1 || base > 36) {
+ if (endptr != 0)
+ *endptr = (char *)nptr;
+ errno = EINVAL;
+ return 0;
+ }
+
+ /*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
diff --git a/lib/libc/stdlib/strtoul.c b/lib/libc/stdlib/strtoul.c
index a236365d2f9..2aa41b76e4a 100644
--- a/lib/libc/stdlib/strtoul.c
+++ b/lib/libc/stdlib/strtoul.c
@@ -1,6 +1,6 @@
-/* $OpenBSD: strtoul.c,v 1.8 2013/04/17 17:40:35 tedu Exp $ */
+/* $OpenBSD: strtoul.c,v 1.9 2014/09/13 20:10:12 schwarze Exp $ */
/*
- * Copyright (c) 1990 Regents of the University of California.
+ * Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -50,6 +50,13 @@ strtoul(const char *nptr, char **endptr, int base)
/*
* See strtol for comments as to the logic used.
*/
+ if (base < 0 || base == 1 || base > 36) {
+ if (endptr != 0)
+ *endptr = (char *)nptr;
+ errno = EINVAL;
+ return 0;
+ }
+
s = nptr;
do {
c = (unsigned char) *s++;
diff --git a/lib/libc/stdlib/strtoull.c b/lib/libc/stdlib/strtoull.c
index 28f613a0875..846417630f9 100644
--- a/lib/libc/stdlib/strtoull.c
+++ b/lib/libc/stdlib/strtoull.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: strtoull.c,v 1.6 2013/03/28 18:09:38 martynas Exp $ */
-/*-
+/* $OpenBSD: strtoull.c,v 1.7 2014/09/13 20:10:12 schwarze Exp $ */
+/*
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
@@ -50,8 +50,15 @@ strtoull(const char *nptr, char **endptr, int base)
int neg, any, cutlim;
/*
- * See strtoq for comments as to the logic used.
+ * See strtoll for comments as to the logic used.
*/
+ if (base < 0 || base == 1 || base > 36) {
+ if (endptr != 0)
+ *endptr = (char *)nptr;
+ errno = EINVAL;
+ return 0;
+ }
+
s = nptr;
do {
c = (unsigned char) *s++;
@@ -59,7 +66,7 @@ strtoull(const char *nptr, char **endptr, int base)
if (c == '-') {
neg = 1;
c = *s++;
- } else {
+ } else {
neg = 0;
if (c == '+')
c = *s++;
diff --git a/lib/libc/stdlib/strtoumax.c b/lib/libc/stdlib/strtoumax.c
index ce6e2c00f18..c73f7e507ca 100644
--- a/lib/libc/stdlib/strtoumax.c
+++ b/lib/libc/stdlib/strtoumax.c
@@ -1,6 +1,5 @@
-/* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
-
-/*-
+/* $OpenBSD: strtoumax.c,v 1.2 2014/09/13 20:10:12 schwarze Exp $ */
+/*
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
@@ -48,8 +47,15 @@ strtoumax(const char *nptr, char **endptr, int base)
int neg, any, cutlim;
/*
- * See strtoq for comments as to the logic used.
+ * See strtoimax for comments as to the logic used.
*/
+ if (base < 0 || base == 1 || base > 36) {
+ if (endptr != 0)
+ *endptr = (char *)nptr;
+ errno = EINVAL;
+ return 0;
+ }
+
s = nptr;
do {
c = (unsigned char) *s++;
@@ -57,7 +63,7 @@ strtoumax(const char *nptr, char **endptr, int base)
if (c == '-') {
neg = 1;
c = *s++;
- } else {
+ } else {
neg = 0;
if (c == '+')
c = *s++;