[BUG] libc/stdlib: lib_checkbase() strips "0x" prefix in base 16 even when no hex digit follows
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
Research direction
Start with libs/libc/stdlib/lib_checkbase.c, comparing the base == 16 branch with the adjacent base == 0 branch. Check how lib_isbasedigit() is used to validate the character after the prefix, then verify strtoul() and strtoull() end-pointer behavior for inputs such as 0xg and 0x. Done means the prefix is skipped only when a hexadecimal digit follows, matching the expected C99/POSIX parsing behavior.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- C
- Stars
- 4.1k
- Forks
- 1.8k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 881
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/nuttx
-
[BUG] libc/time: strptime() numeric_range upper bound is off by one and accepts invalid dates/timesOpenArch: all Area: Posix OS: Linux
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
Arch: all Arch: arm Area: Build system OS: Linux Type: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/nuttx#19435 · 9 comments ·
Maintainers usually reply within 1 day
-
Arch: all OS: Linux Type: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
apache/nuttx#16249 · 7 comments ·
Maintainers usually reply within 1 day
-
[Bug]: libc/net: getifaddrs() drops and leaks secondary IPv6 addresses and leaves dangling ifa_name and uninitialized netmask/dstaddr pointersPossibly taken @yashgoyal0110 claimed this today. Open
Maintainers usually reply within 1 day
Similar issues
-
CF_CFDP_S_SendFileData computes the file checksum from the SB buffer after CFE_SB_TransmitBufferOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
389ds/389-ds-base#8251 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cpp#645 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
ROCm/hrx-system#3628 ·
Maintainers usually reply within 1 day