diff mbox

[2/3] Don't recognise a double hex prefix (0x0xNN) as valid.

Message ID 1424400307-13664-3-git-send-email-dja@axtens.net
State Accepted
Headers show

Commit Message

Daniel Axtens Feb. 20, 2015, 2:45 a.m. UTC
When autodetecting the base, the code would strip hex prefixes twice.
Now the string is not modified in the detection stage.

Signed-off-by: Daniel Axtens <dja@axtens.net>
---
 libc/stdlib/strtol.c   | 2 --
 libc/stdlib/strtoul.c  | 2 --
 libc/test/run-stdlib.c | 7 +++----
 3 files changed, 3 insertions(+), 8 deletions(-)

Comments

Cyril Bur Feb. 20, 2015, 3:07 a.m. UTC | #1
On Fri, 2015-02-20 at 13:45 +1100, Daniel Axtens wrote:
> When autodetecting the base, the code would strip hex prefixes twice.
> Now the string is not modified in the detection stage.
> 
> Signed-off-by: Daniel Axtens <dja@axtens.net>

Reviewed-by: Cyril Bur <cyrilbur@gmail.com>
> ---
>  libc/stdlib/strtol.c   | 2 --
>  libc/stdlib/strtoul.c  | 2 --
>  libc/test/run-stdlib.c | 7 +++----
>  3 files changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/libc/stdlib/strtol.c b/libc/stdlib/strtol.c
> index 474597a..e6d4da3 100644
> --- a/libc/stdlib/strtol.c
> +++ b/libc/stdlib/strtol.c
> @@ -53,8 +53,6 @@ long int strtol(const char *S, char **PTR,int BASE)
>  		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
>  		{
>  			BASE = 16;
> -			(*PTR)++;
> -			(*PTR)++;
>  		}
>  		else
>  		{
> diff --git a/libc/stdlib/strtoul.c b/libc/stdlib/strtoul.c
> index 754e7db..8472668 100644
> --- a/libc/stdlib/strtoul.c
> +++ b/libc/stdlib/strtoul.c
> @@ -46,8 +46,6 @@ unsigned long int strtoul(const char *S, char **PTR,int BASE)
>  		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
>  		{
>  			BASE = 16;
> -			(*PTR)++;
> -			(*PTR)++;
>  		}
>  		else
>  		{
> diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c
> index bb64790..98c79b7 100644
> --- a/libc/test/run-stdlib.c
> +++ b/libc/test/run-stdlib.c
> @@ -41,8 +41,8 @@ int main(void)
>  
>  	/* our atoi recognises hex!  */
>  	assert(atoi("0x800") == 0x800);
> -	/* Really weird hex! */
> -	assert(atoi("0x0x800") == 0x800);
> +	/* But not with a duplicate prefix */
> +	assert(atoi("0x0x800") == 0);
>  
>  	/* atol - ensure it recognises longs */
>  	assert(atol("2147483648") == 2147483648);
> @@ -73,8 +73,7 @@ int main(void)
>  	/* strtoul - autodetection of base */
>  	assert(strtoul(" 123456", NULL, 0) == 123456);
>  	assert(strtoul("0x800", NULL, 0) == 0x800);
> -	/* Again, really weird hex */
> -	assert(strtoul("0x0x800", NULL, 0) == 0x800);
> +	assert(strtoul("0x0x800", NULL, 0) == 0);
>  
>  	/* strtoul - weird/invalid bases */
>  	assert(strtoul("z", NULL, -1) == 0);
diff mbox

Patch

diff --git a/libc/stdlib/strtol.c b/libc/stdlib/strtol.c
index 474597a..e6d4da3 100644
--- a/libc/stdlib/strtol.c
+++ b/libc/stdlib/strtol.c
@@ -53,8 +53,6 @@  long int strtol(const char *S, char **PTR,int BASE)
 		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
 		{
 			BASE = 16;
-			(*PTR)++;
-			(*PTR)++;
 		}
 		else
 		{
diff --git a/libc/stdlib/strtoul.c b/libc/stdlib/strtoul.c
index 754e7db..8472668 100644
--- a/libc/stdlib/strtoul.c
+++ b/libc/stdlib/strtoul.c
@@ -46,8 +46,6 @@  unsigned long int strtoul(const char *S, char **PTR,int BASE)
 		if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
 		{
 			BASE = 16;
-			(*PTR)++;
-			(*PTR)++;
 		}
 		else
 		{
diff --git a/libc/test/run-stdlib.c b/libc/test/run-stdlib.c
index bb64790..98c79b7 100644
--- a/libc/test/run-stdlib.c
+++ b/libc/test/run-stdlib.c
@@ -41,8 +41,8 @@  int main(void)
 
 	/* our atoi recognises hex!  */
 	assert(atoi("0x800") == 0x800);
-	/* Really weird hex! */
-	assert(atoi("0x0x800") == 0x800);
+	/* But not with a duplicate prefix */
+	assert(atoi("0x0x800") == 0);
 
 	/* atol - ensure it recognises longs */
 	assert(atol("2147483648") == 2147483648);
@@ -73,8 +73,7 @@  int main(void)
 	/* strtoul - autodetection of base */
 	assert(strtoul(" 123456", NULL, 0) == 123456);
 	assert(strtoul("0x800", NULL, 0) == 0x800);
-	/* Again, really weird hex */
-	assert(strtoul("0x0x800", NULL, 0) == 0x800);
+	assert(strtoul("0x0x800", NULL, 0) == 0);
 
 	/* strtoul - weird/invalid bases */
 	assert(strtoul("z", NULL, -1) == 0);