fix(libc): report fopen_s and freopen_s argument errors to the constraint handler #12597

Merged
JJenkx merged 3 commits from upstream-pr/12597 into main 2026-11-01 06:48:43 +00:00
JJenkx commented 2026-10-28 22:17:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T22:17:22Z.


What

fopen_s and freopen_s now report argument errors to the constraint handler, as titles' libc.prx does. The handler gets EINVAL and a null pointer, with these messages:

argument error message (both functions)
null FILE ** "fopen_s: bad FILE ** argument"
null filename (fopen_s), null mode, or null stream (freopen_s) "fopen_s: bad argument"

freopen_s really does report the fopen_s messages. On main both returned EINVAL without calling the handler. The return values and the stored stream (left alone for a null FILE **, set to null otherwise) were already right and are unchanged. Other errors, such as an invalid mode or a missing file, are not changed here.

Depends on #9935. On main the default constraint handler aborts, so until #9935 merges, a title that hits one of these violations without installing a handler would abort. Merges cleanly with #9935, #10755 and #11831.

Tested

Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, main 319621ba2e.

  • Native comparison: a scratch loader calls the titles' functions by NID next to this build's, with the same recording handler on both sides. It runs 12 cases: each null-argument case of fopen_s and freopen_s (including all null), plus gets_s with size 0, size 2^61 and a null buffer. Each case compares the return value, the stored stream, and the handler's calls, error, pointer and message.
    • main: the 9 fopen_s/freopen_s cases differ, and the 3 gets_s cases already match.
    • This PR: 0 of 12 differ.
    • PPSA07923 and PPSA05916 give the same results.
    • Paths that open a file aren't compared, because they need kernel imports the loader doesn't provide.
  • Regression: guest_fiopen and guest_fopen_errors install a recording handler around these cases and check each message. With the previous FileAndHeap.cpp both fail.
  • Test set: guest_fiopen, guest_fopen_errors, guest_safe_errors, guest_bounds_checking and guest_crt pass 3/3. Conventions pass.

Current qualification — 2026-10-30

Integrated upstream main 08a10d225306ad102b57408e2ef7566bcfb829b2 at 1768c1821b2921745f051fb97b999bb63b97f38f, retaining the actual previous head and main as parents. Manual conflict resolution changes only debt documentation; production and test files equal the automatic merge output. Both parents' added test lines and main gitlinks are preserved.

Linux CPU: 9 passes (guest_fiopen, guest_fopen_errors, guest_crt); public_headers 1 passes; no skips. MinGW source syntax and conventions pass.

No new Windows/macOS runtime, native SDK, game, GPU or performance claim. The Windows user pause remains in force.

Evidence: immutable source and qualification receipts, SHA-256 61f5500e7526ea4e107159927de78c37db7e0ac1cb7ebd8625b22ab0ad5d9bd7 (download verified).

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 under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #9935
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058569035 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12597). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T22:17:22Z. --- ### What `fopen_s` and `freopen_s` now report argument errors to the constraint handler, as titles' `libc.prx` does. The handler gets `EINVAL` and a null pointer, with these messages: | argument error | message (both functions) | |---|---| | null `FILE **` | `"fopen_s: bad FILE ** argument"` | | null filename (`fopen_s`), null mode, or null stream (`freopen_s`) | `"fopen_s: bad argument"` | `freopen_s` really does report the `fopen_s` messages. On `main` both returned `EINVAL` without calling the handler. The return values and the stored stream (left alone for a null `FILE **`, set to null otherwise) were already right and are unchanged. Other errors, such as an invalid mode or a missing file, are not changed here. **Depends on #9935.** On `main` the default constraint handler aborts, so until #9935 merges, a title that hits one of these violations without installing a handler would abort. Merges cleanly with #9935, #10755 and #11831. ### Tested Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' functions by NID next to this build's, with the same recording handler on both sides. It runs 12 cases: each null-argument case of `fopen_s` and `freopen_s` (including all null), plus `gets_s` with size 0, size `2^61` and a null buffer. Each case compares the return value, the stored stream, and the handler's calls, error, pointer and message. - `main`: the 9 `fopen_s`/`freopen_s` cases differ, and the 3 `gets_s` cases already match. - This PR: **0 of 12** differ. - PPSA07923 and PPSA05916 give the same results. - Paths that open a file aren't compared, because they need kernel imports the loader doesn't provide. - **Regression:** `guest_fiopen` and `guest_fopen_errors` install a recording handler around these cases and check each message. With the previous `FileAndHeap.cpp` both fail. - **Test set:** `guest_fiopen`, `guest_fopen_errors`, `guest_safe_errors`, `guest_bounds_checking` and `guest_crt` pass 3/3. Conventions pass. <!-- codex-maintenance-20261030-repeat-12597 --> **Current qualification — 2026-10-30** Integrated upstream main `08a10d225306ad102b57408e2ef7566bcfb829b2` at `1768c1821b2921745f051fb97b999bb63b97f38f`, retaining the actual previous head and main as parents. Manual conflict resolution changes only debt documentation; production and test files equal the automatic merge output. Both parents' added test lines and main gitlinks are preserved. Linux CPU: 9 passes (guest_fiopen, guest_fopen_errors, guest_crt); public_headers 1 passes; no skips. MinGW source syntax and conventions pass. No new Windows/macOS runtime, native SDK, game, GPU or performance claim. The Windows user pause remains in force. Evidence: [immutable source and qualification receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu-seven-current-main-linux-20261030.tar.gz), SHA-256 `61f5500e7526ea4e107159927de78c37db7e0ac1cb7ebd8625b22ab0ad5d9bd7` (download verified). ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #9935 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
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!12597
No description provided.