[BUG] libc/stdlib: lib_checkbase() strips "0x" prefix in base 16 even when no hex digit follows
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 82/100
Línea de trabajo
Empieza por libs/libc/stdlib/lib_checkbase.c y compara la rama base == 16 con la rama base == 0 adyacente. Comprueba cómo se usa lib_isbasedigit() para validar el carácter que sigue al prefijo y, después, verifica el comportamiento del puntero final de strtoul() y strtoull() con entradas como 0xg y 0x. El trabajo estará terminado cuando el prefijo solo se omita si va seguido de un dígito hexadecimal, de acuerdo con el comportamiento de análisis esperado de C99/POSIX.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description / Steps to reproduce the issue
Summary
In libs/libc/stdlib/lib_checkbase.c, the base == 16 branch skips a leading "0x" or "0X" without checking whether a hexadecimal digit follows:
else if (base == 16)
{
if (ptr[0] == '0' && (ptr[1] == 'X' || ptr[1] == 'x'))
{
ptr += 2;
}
}
Root Cause
Per C99 7.20.1.4 / POSIX, the "0x" / "0X" prefix in base 16 is only part of the subject sequence when followed by at least one hexadecimal digit. Otherwise, the longest valid initial subsequence is "0", so the converted value is 0 and *endptr must point to 'x' / 'X'.
The base == 0 branch immediately above (lines 84-89) already checks lib_isbasedigit(ptr[2], 16, NULL), whereas base == 16 does not. As a result, on "0x", "0xg", "-0xg", or " 0xz" with base == 16, strtoul() / strtol() set *endptr back to nptr and strtoull() sets *endptr past 'x'.
Steps to Reproduce
Compile lib_checkbase.c, lib_strtoul.c, and lib_strtoull.c (Ubuntu 24.04, x86_64, gcc 13.3.0):
input="0xg": glibc=(0, "xg") nuttx_strtoul=(0, "0xg") nuttx_strtoull=(0, "g")
input="0x": glibc=(0, "x") nuttx_strtoul=(0, "0x") nuttx_strtoull=(0, "")
input="0X": glibc=(0, "X") nuttx_strtoul=(0, "0X") nuttx_strtoull=(0, "")
input="-0xg": glibc=(0, "xg") nuttx_strtoul=(0, "-0xg") nuttx_strtoull=(0, "g")
input=" 0xz": glibc=(0, "xz") nuttx_strtoul=(0, " 0xz") nuttx_strtoull=(0, "z")
Expected Behavior
Only skip "0x" / "0X" in base == 16 when lib_isbasedigit(ptr[2], 16, NULL) is true, so "0xg" parses "0" and sets *endptr to "xg", matching C99/POSIX.
On which OS does this issue occur?
[OS: Linux]
What is the version of your OS?
Ubuntu 24.04
NuttX Version
master (cf03942d)
Issue Architecture
[Arch: all]
Issue Area
[Area: Posix]
Host information
Architecture-independent C bug in libs/libc/stdlib/lib_checkbase.c. Reproduced on Ubuntu 24.04 x86_64 with gcc 13.3.0.
Verification
- I have verified before submitting the report.
- Lenguaje dominante
- C
- Estrellas
- 4.1k
- Forks
- 1.8k
- Merge medio
- 1 d 5 h
- PR fusionados (30 d)
- 878
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de apache/nuttx
-
[BUG] libc/time: strptime() numeric_range upper bound is off by one and accepts invalid dates/timesAbiertoArch: all Area: Posix OS: Linux
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 1 día
-
mtd: RAM consolidation reads past its block buffer on a malformed entryPosiblemente ocupada @Ahmed55559 la tomó hace 6 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
Please rerun `pre-commit run -a`AbiertoArch: all Arch: arm Area: Build system OS: Linux Type: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
apache/nuttx#19435 · 9 comentarios ·
Los mantenedores suelen responder en 1 día
-
Arch: all OS: Linux Type: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
apache/nuttx#16249 · 7 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de apache/nuttx
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
fastfetch-cli/fastfetch#2780 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
elliotchance/vsql#2079 ·
Los mantenedores suelen responder en 1 día
-
docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 61/100
vim/vim#22107 · 1 comentario ·
Los mantenedores suelen responder en 13 días
-
threadpool: a failed `pthread_cond_wait` makes Mlucas hang instead of stoppingPosiblemente ocupada @leinardi la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
primesearch/Mlucas#398 ·