diff options
author | Daniel Axtens <dja@axtens.net> | 2015-02-20 13:45:07 +1100 |
---|---|---|
committer | Stewart Smith <stewart@linux.vnet.ibm.com> | 2015-02-23 13:35:50 +1100 |
commit | 2d9cfc99ce6d1bd77e6dd722858147b78dd3962d (patch) | |
tree | b487c8285efe1dfd53fc94377312ce962b062e3f /libc | |
parent | e4f5a453bb4e8b9ee3820c0f495beff417a867de (diff) | |
download | skiboot-2d9cfc99ce6d1bd77e6dd722858147b78dd3962d.zip skiboot-2d9cfc99ce6d1bd77e6dd722858147b78dd3962d.tar.gz skiboot-2d9cfc99ce6d1bd77e6dd722858147b78dd3962d.tar.bz2 |
atoi/atol should assume base 10, not autodetect base.
The behaviour of atoi/atol on glibc (and according to the spec) is
to assume base 10, not to autodetect the base.
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Cyril Bur <cyrilbur@gmail.com>
Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
Diffstat (limited to 'libc')
-rw-r--r-- | libc/stdlib/atoi.c | 2 | ||||
-rw-r--r-- | libc/stdlib/atol.c | 2 | ||||
-rw-r--r-- | libc/test/run-stdlib.c | 11 |
3 files changed, 9 insertions, 6 deletions
diff --git a/libc/stdlib/atoi.c b/libc/stdlib/atoi.c index d2fb33b..444c05a 100644 --- a/libc/stdlib/atoi.c +++ b/libc/stdlib/atoi.c @@ -14,5 +14,5 @@ int atoi(const char *str) { - return strtol(str, NULL, 0); + return strtol(str, NULL, 10); } diff --git a/libc/stdlib/atol.c b/libc/stdlib/atol.c index a6aa47b..e73c7d4 100644 --- a/libc/stdlib/atol.c +++ b/libc/stdlib/atol.c @@ -14,5 +14,5 @@ long atol(const char *str) { - return strtol(str, NULL, 0); + return strtol(str, NULL, 10); } diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c index 98c79b7..1f3a2e1 100644 --- a/libc/test/run-stdlib.c +++ b/libc/test/run-stdlib.c @@ -39,15 +39,18 @@ int main(void) assert(atoi(buf) == 42); assert(atoi("42isthemagicnumber") == 42); - /* our atoi recognises hex! */ - assert(atoi("0x800") == 0x800); - /* But not with a duplicate prefix */ - assert(atoi("0x0x800") == 0); + /* atoi is base 10 only */ + assert(atoi("0x800") == 0); /* atol - ensure it recognises longs */ assert(atol("2147483648") == 2147483648); assert(atol("-2147483649") == -2147483649); + /* strtol detects hex */ + assert(strtol("0x800", NULL, 0) == 0x800); + /* But not with a duplicate prefix */ + assert(strtol("0x0x800", NULL, 0) == 0); + /* strtol - invalid/weird bases */ assert(strtol("z", NULL, -1) == 0); assert(strtol("11111", NULL, 1) == 0); |