[BUG] libc/stdlib: lib_checkbase() strips "0x" prefix in base 16 even when no hex digit follows
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
まず libs/libc/stdlib/lib_checkbase.c を確認し、base == 16 の分岐と隣接する base == 0 の分岐を比較します。lib_isbasedigit() がプレフィックスの後の文字を検証するためにどう使われているかを確認し、続いて 0xg や 0x のような入力に対する strtoul() と strtoull() の終了ポインターの動作を検証します。16進数の数字が続く場合に限ってプレフィックスが読み飛ばされ、期待される C99/POSIX の解析動作に一致すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C
- スター
- 4.1k
- フォーク
- 1.8k
- 平均マージ
- 1日 5時間
- マージ済み PR(30日)
- 878
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/nuttx のほかの issue
-
[BUG] libc/time: strptime() numeric_range upper bound is off by one and accepts invalid dates/timesオープンArch: all Area: Posix OS: Linux
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
mtd: RAM consolidation reads past its block buffer on a malformed entry対応中かも @Ahmed55559 が 6 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
Arch: all Arch: arm Area: Build system OS: Linux Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
apache/nuttx#19435 · コメント 9 件 ·
メンテナーはふだん 1 日以内に返信
-
Arch: all OS: Linux Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
apache/nuttx#16249 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
fastfetch-cli/fastfetch#2780 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elliotchance/vsql#2079 ·
メンテナーはふだん 1 日以内に返信
-
docs
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 61/100
メンテナーはふだん 13 日以内に返信
-
threadpool: a failed `pthread_cond_wait` makes Mlucas hang instead of stopping対応中かも @leinardi が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
primesearch/Mlucas#398 ·