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

オープン 初心者向け
#22,757 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

まず libs/libc/stdlib/lib_checkbase.c を確認し、base == 16 の分岐と隣接する base == 0 の分岐を比較します。lib_isbasedigit() がプレフィックスの後の文字を検証するためにどう使われているかを確認し、続いて 0xg や 0x のような入力に対する strtoul() と strtoull() の終了ポインターの動作を検証します。16進数の数字が続く場合に限ってプレフィックスが読み飛ばされ、期待される C99/POSIX の解析動作に一致すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
C
スター
4.1k
フォーク
1.8k
平均マージ
1日 5時間
マージ済み PR(30日)
878

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/nuttx のほかの issue

apache/nuttx の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。