libc/stdlib: Check if there is next char before assuming octal

When *pptr/ptr is "0" and base is 0, lib_checkbase() returns the error base 8(should be 10).
e.g. `strtoul("0", &endptr, 0);`

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
This commit is contained in:
wangjianyu3 2023-12-26 17:06:38 +08:00 committed by Xiang Xiao
parent 1bb0f19fd5
commit 9773fb9867

View File

@ -69,18 +69,21 @@ int lib_checkbase(int base, FAR const char **pptr)
if (*ptr == '0')
{
/* Assume octal */
base = 8;
ptr++;
/* Check for hexadecimal */
if ((*ptr == 'X' || *ptr == 'x') &&
lib_isbasedigit(ptr[1], 16, NULL))
if (ptr[1] != '\0')
{
base = 16;
/* Assume octal */
base = 8;
ptr++;
/* Check for hexadecimal */
if ((*ptr == 'X' || *ptr == 'x') &&
lib_isbasedigit(ptr[1], 16, NULL))
{
base = 16;
ptr++;
}
}
}
}