fix(libc): clear exhausted strtok continuation state #4625

Merged
QRMOE merged 2 commits from upstream-pr/4625 into main 2026-10-13 14:59:43 +00:00
QRMOE commented 2026-10-12 17:02:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-12T17:02:37Z.


What

Clear the continuation pointer when strtok_r exhausts its input, matching FreeBSD 11.4 __strtok_r. Current main retains a pointer to the final NUL; modifying that old terminator can make a later continuation call resume an exhausted tokenization. The shared strtok wrapper receives the same correction.

The regression covers final tokens, trailing separators, empty and separator-only inputs, interleaved tokenizations, changed old terminators, and errno preservation.

Tested

Windows, MinGW GCC 15.2, Release, BUILD_TESTING=ON:

  • Full default build and explicit libs target passed.
  • guest_strings, guest_wide_strings, guest_memrchr, guest_errors, and guest_collate: 5/5 passed; each finished below one second.
  • The new regression fails on unchanged main with strtok_r retained the state after exhausting delimiters.
  • A separate harness compared production strtok_r and strtok against the original FreeBSD 11.4 routines at -O0 and -O3: 171,717 tokenization sequences and 1,953,208 calls per optimization, including changing delimiters, buffer mutations, continuation offsets, and errno. No mismatches.
  • Conventions and git diff --check passed. No game testing or console-hardware validation claimed.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832569462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4625). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-12T17:02:37Z. --- ### What Clear the continuation pointer when `strtok_r` exhausts its input, matching [FreeBSD 11.4 `__strtok_r`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/string/strtok.c). Current main retains a pointer to the final NUL; modifying that old terminator can make a later continuation call resume an exhausted tokenization. The shared `strtok` wrapper receives the same correction. The regression covers final tokens, trailing separators, empty and separator-only inputs, interleaved tokenizations, changed old terminators, and errno preservation. ### Tested Windows, MinGW GCC 15.2, Release, `BUILD_TESTING=ON`: - Full default build and explicit `libs` target passed. - `guest_strings`, `guest_wide_strings`, `guest_memrchr`, `guest_errors`, and `guest_collate`: 5/5 passed; each finished below one second. - The new regression fails on unchanged main with `strtok_r retained the state after exhausting delimiters`. - A separate harness compared production `strtok_r` and `strtok` against the original FreeBSD 11.4 routines at `-O0` and `-O3`: 171,717 tokenization sequences and 1,953,208 calls per optimization, including changing delimiters, buffer mutations, continuation offsets, and errno. No mismatches. - Conventions and `git diff --check` passed. No game testing or console-hardware validation claimed. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-13 14:50:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:50:37Z.


Checked the change against FreeBSD 11.4 __strtok_r (state cleared on exhaustion, also when the last token ends at the NUL). guest_strings passes with the PR and fails on main without the fix; scoped ctest (94 tests) green. Merging once the full build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148311738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4625#issuecomment-6148311738). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:50:37Z. --- Checked the change against FreeBSD 11.4 `__strtok_r` (state cleared on exhaustion, also when the last token ends at the NUL). `guest_strings` passes with the PR and fails on main without the fix; scoped ctest (94 tests) green. Merging once the full build is green.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4625
No description provided.