Fix wrong base parsing in strtol(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-10-21 19:46:38 +02:00
parent db185ece7d
commit 8018a85a30
1 changed files with 4 additions and 3 deletions

View File

@ -49,7 +49,7 @@
#include <limits.h>
// Convert a character into a digit.
static int debase( STRTOL_CHAR c)
static int debase(STRTOL_CHAR c)
{
if ( STRTOL_L('0') <= c && c <= STRTOL_L('9') )
return c - STRTOL_L('0');
@ -150,9 +150,10 @@ STRTOL_INT STRTOL(const STRTOL_CHAR* restrict str,
// Autodetect base 8 or base 16.
if ( !base && str[0] == STRTOL_L('0') )
{
if ( str[1] == STRTOL_L('x') || str[1] == STRTOL_L('X') )
if ( (str[1] == STRTOL_L('x') || str[1] == STRTOL_L('X')) &&
(str[2] && debase(str[2]) < 16) )
str += 2, base = 16;
else if ( 0 <= debase(str[1]) && debase(str[1]) < 8 )
else if ( 0 <= debase(str[1]) )
str++, base = 8;
}