[Skiboot] [PATCH 3/3] atoi/atol should assume base 10, not autodetect base.
Cyril Bur
cyrilbur at gmail.com
Fri Feb 20 14:05:59 AEDT 2015
On Fri, 2015-02-20 at 13:45 +1100, Daniel Axtens wrote:
> 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 at axtens.net>
Reviewed-by: Cyril Bur <cyrilbur at gmail.com>
> ---
> libc/stdlib/atoi.c | 2 +-
> libc/stdlib/atol.c | 2 +-
> 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);
More information about the Skiboot
mailing list