[BUG] libc/stdlib: lib_checkbase() strips "0x" prefix in base 16 even when no hex digit follows

Open Beginner friendly
#22,757 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

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

Arch: all Area: Posix OS: Linux
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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/nuttx

All issues in apache/nuttx

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.