Skip to content

fix(libc): reject invalid integer conversion bases - #3953

Closed
MarcZX10 wants to merge 5 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-strto-no-conversion
Closed

MarcZX10 wants to merge 5 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-strto-no-conversion

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

This rebased carry preserves Hussein Kadhim's original implementation commit and author attribution, but narrows its behavior to invalid-base handling. A collaborator's console strtol disassembly report in the review discussion shows that no-digit conversions return zero, leave endptr at the input start, and preserve errno; this PR now keeps that behavior. The same report shows EINVAL for a base above 36. The parser validates bases against the ISO C set {0, 2..36} before calling the host CRT, so invalid values return zero, EINVAL, and the original input pointer consistently instead of reaching host-specific invalid-parameter behavior.

The shared validation also applies to the narrow and wide integer conversion wrappers. Dinkumware _Stoul and _Stoull retain their previous behavior and are not routed through the new helper. Console behavior for base 1, negative bases, and the wide conversion family is not independently verified and is recorded in technical debt.

Tested

Linux x86-64, GCC 12.2, Release, current main dacd78525:

  • Built libc and guest_atoi_tests, guest_math_tests, guest_wide_conversion_tests, and guest_wide_string_tests.
  • guest_atoi, guest_math, guest_wide_conversion, and guest_wide_strings passed (4/4).
  • tools/check_conventions.py --base upstream/main and git diff --check passed.
  • No Windows run or title run was performed on this head.

Checklist

  • Rebased on current main; no open PR found for invalid-base behavior
  • One topic: invalid integer-conversion bases
  • Preserves the carried implementation author's attribution
  • No investigation files added
  • Depends on: none
  • AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

guest_atoi fails with this merged onto main (expected errno 1234; received errno 22): since #3687 atoi goes through strtol, so the new EINVAL reaches it. Please rebase and keep atoi from setting errno.

@xXPBSXx

xXPBSXx commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Windows test of head e49e9abce merged onto current main (fa0cc752a, clean merge; built together with four other open PRs, so the run is for the combination): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors, -j2.

guest_math, guest_wide_conversion and guest_wide_strings (the tests this PR changes) pass. The seven tests of the five PRs (guest_coredump_user_data, guest_np_web_api2, guest_execvp, guest_math, guest_wide_conversion, guest_wide_strings, guest_character) all pass.

Not checked: that the test fails without the change, Linux, a title run.

AI-assisted (Claude Code).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

What shows that the console sets EINVAL when nothing converts? _Stoul and _Stoull are Dinkumware entry points, and the FreeBSD 11.4 comparison covers FreeBSD's strtol, not the console's. With a console result or a title that depends on it this can go in. guest_atoi still fails with it merged.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

I do not have console evidence that the Dinkumware conversion functions set EINVAL when no digits convert; the current evidence is only FreeBSD strtol behavior. The current diff also changes the shared parser path so atoi inherits that errno, which the existing guest_atoi regression catches. I agree this PR is not ready to merge until that regression is fixed and the console-side behavior is supported by evidence or the change is narrowed.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • guest_atoi fails with this merged onto current main (ctest -R guest_atoi; the other three PR tests pass): since #3687 atoi goes through strtol, so the new errno = EINVAL reaches it. atoi must not set errno; keep that path out of ParseGuestInteger (e.g. save/restore errno in atoi, or a variant without the errno write).
  • No evidence that the console sets EINVAL when nothing converts: the FreeBSD 11.4 strtol comparison does not cover the Dinkumware _Stoul/_Stoull entry points. Please add a console result or a title that depends on it (or narrow the change to what is shown), and say in TechnicalDebt which reference this follows.

Happy to re-review once guest_atoi passes and the evidence is added.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3965 docs/dev/debt/libc.md L30
#5596 docs/dev/debt/libc.md L30
#5599 docs/dev/debt/libc.md L30

@MarcZX10
MarcZX10 force-pushed the codex/carry-strto-no-conversion branch from e49e9ab to e3724ab Compare October 13, 2026 12:25
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Updated and rebased this PR on current main. atoi now preserves incoming errno, and the Dinkumware _Stoul/_Stoull entry points retain their prior behavior because the available reference evidence covers FreeBSD strtol, not those console functions. Added regression coverage for atoi and the unchanged Dinkumware errno behavior. The focused guest_atoi, guest_wide_conversion, and guest_wide_strings tests pass on Linux; the new _Stoul/_Stoull checks pass before guest_math reaches an abort that reproduces on unmodified main. Please re-review when convenient.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing atoi; guest_atoi and the other three tests pass here merged onto main (Linux, scoped build + ctest).

  • The premise is contradicted by the console. Disassembling strtol (NID mXlxhmLNMPg) in a title's shipped libc.prx: the wrapper passes a local error cell to the inner parser and copies it to errno only if non-zero. When no digits convert, the inner parser returns 0 with *endptr = str and leaves the cell at 0. The only places it writes the cell are ERANGE (0x22) and EINVAL (0x16) for a base above 36. So errno stays unchanged for "", "abc", whitespace-only, sign-only. FreeBSD 11.4 strtol is not what the console runs.
  • core/libs/prx/libc/include/General.hpp:32: drop if (end == str) errno = EINVAL;. The expected 22 for no-conversion cases in GuestMath.cpp:355-356,410-411, GuestWideStrings.cpp:162-172 and GuestWideConversion.cpp:73-74 should go back to unchanged errno (0 in the tests, as before).
  • The invalid-base check (General.hpp:24-28) matches the console for base > 36 (EINVAL, endptr = str). Base 1 is not confirmed by the disassembly; if you keep it, please say where it comes from. This part could stay as the PR's actual change, with the #3931 reference adjusted since the issue's claim does not hold for the console.

@simonbronk

Copy link
Copy Markdown
Contributor

Tested on Linux x86-64 (Ubuntu 24.04 in a Colima VM with Rosetta on Apple M4, GCC 13.3), this PR (e3724ab9a) merged into main 39890031e, Release build with BUILD_TESTING=ON.

  • ctest: 1062/1069 pass, the same 7 failures as main in this environment (Rosetta and VM limits: guest_math, amd64_only_converter, agc_driver_depth_compare_copy, tls_function_coverage_register_address_loads, linux_executable_exports, linux_entry_argv, linux_input_program_headers). No new failures.
  • Differential check of strtol and strtoul from the unpatched libc.prx against FreeBSD 11.4's lib/libc/stdlib/strtol.c and strtoul.c, compiled on the host: 287,381 random inputs (whitespace, signs, 0x prefixes, bases 0, 2, 8, 10, 16, 36 and invalid -1, 1, 37), comparing the value, endptr and errno. On main, 25,502 cases differ, all with an invalid base: endptr is left unset or past the start. With this PR, none differ.

AI-assisted: yes (Claude Code).

husseinkad and others added 4 commits October 13, 2026 19:32
FreeBSD strtol, strtoul, strtoll, strtoull, strtoimax, strtoumax and the
wcsto* variants set errno to EINVAL when no digits are converted. The host
functions leave errno untouched, so guests testing errno after parsing
text such as "abc" or "" saw 0.
@MarcZX10
MarcZX10 force-pushed the codex/carry-strto-no-conversion branch from e3724ab to e34a49c Compare October 13, 2026 19:33
@MarcZX10 MarcZX10 changed the title fix(libc): set EINVAL when integer parsing converts no digits fix(libc): reject invalid integer conversion bases Oct 13, 2026
@MarcZX10

Copy link
Copy Markdown
Contributor Author

I narrowed the PR to invalid-base handling and rebased all three commits onto current main dacd78525. The carried no-digit EINVAL change is removed: the reviewed console strtol behavior preserves errno when no digits convert. guest_atoi, guest_math, guest_wide_conversion, and guest_wide_strings now pass on Linux/GCC 12.2 Release (4/4); conventions and git diff --check pass. The current head has no Windows or title run. Console evidence specifically covers strtol with base >36; base 1, negative bases, and the wide family remain documented as unverified. Please re-review this narrowed scope when convenient.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Follow-up on final head e1ce61125: I added a nonzero-errno sentinel regression for all six narrow conversion wrappers. Rebuilt and reran guest_atoi, guest_math, guest_wide_conversion, and guest_wide_strings; all pass (4/4) on Linux/GCC 12.2 Release. The automatic checks for this latest commit are running.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

The latest head e1ce61125 now has all applicable CI checks complete and passing, including the Windows relinker job. The path-filtered full Linux and Windows builds are skipped for this change; locally, the four focused Linux tests (guest_atoi, guest_math, guest_wide_conversion, and guest_wide_strings) pass 4/4 with the added nonzero-errno sentinels. The narrowed scope and remaining evidence limits are unchanged from my prior update. Please re-review when convenient.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants