diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2014-09-13 20:10:13 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2014-09-13 20:10:13 +0000 |
commit | 2dcb748989a77eda5919f6460bbcee0365c5538d (patch) | |
tree | 287dc35292ac2a824f536a0c130424ea6018fa0e /lib/libc/stdlib/strtoimax.c | |
parent | 5de60ac521905d61b6fc62afc6ed7be616694051 (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/stdlib/strtoimax.c')
-rw-r--r-- | lib/libc/stdlib/strtoimax.c | 16 |
1 files changed, 13 insertions, 3 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. |