feat(libc): implement asctime_r #12841

Merged
heditrigui merged 7 commits from upstream-pr/12841 into main 2026-11-04 06:27:09 +00:00
heditrigui commented 2026-10-29 12:11:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by heditrigui on 2026-10-29T12:11:27Z.


What

Implement POSIX / FreeBSD \sctime_r\ (NID \488p6whx47U) in \libc, formatting broken-down time into a caller-supplied buffer with identical formatting to \sctime. Refactor \sctime\ to delegate to \sctime_r\ via a thread-local buffer, throw \std::invalid_argument\ when given a null buffer, and verify formatting and error handling in \GuestStrftime\ and \GuestTimeConversion.

Tested

  • Ran \python tools/check_conventions.py --base upstream/main\ cleanly (0 errors).
  • Validated test targets \GuestStrftime\ and \GuestTimeConversion\ on Windows 11 x64 (hosted Windows CI passed 100% green) and macOS Apple Clang arm64 harness.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5067748690 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12841). Originally posted by [heditrigui](https://github.com/heditrigui) on 2026-10-29T12:11:27Z. --- ### What Implement POSIX / FreeBSD \sctime_r\ (NID \488p6whx47U\) in \libc\, formatting broken-down time into a caller-supplied buffer with identical formatting to \sctime\. Refactor \sctime\ to delegate to \sctime_r\ via a thread-local buffer, throw \std::invalid_argument\ when given a null buffer, and verify formatting and error handling in \GuestStrftime\ and \GuestTimeConversion\. ### Tested - Ran \python tools/check_conventions.py --base upstream/main\ cleanly (0 errors). - Validated test targets \GuestStrftime\ and \GuestTimeConversion\ on Windows 11 x64 (hosted Windows CI passed 100% green) and macOS Apple Clang arm64 harness. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 13:00:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T13:00:01Z.

Review: COMMENTED


Reviewed head d690c78347 against main 157b02c4f1. I ran this on macOS arm64 with Apple clang. No console was used.

What I checked

  • The NID is right: python3 tools/nid_names.py --name asctime_r gives 488p6whx47U. main has no other asctime_r.
  • git merge-tree --write-tree upstream/main <head> merges cleanly, and git diff --check is clean.
  • check_conventions.py --base upstream/main reports 0 errors but 1 warning: docs/dev/debt/libc.md:202: [debt-length] ... 1047 characters. The line was 909 characters on main, so this PR pushes it over the 1000 limit.
  • pr_overlap.py --branch <head> (1400 PRs): there are no same-export conflicts. Three PRs (#10895, #11843, #12593) touch the neighbouring debt lines 201-203. #10409 (CONFLICTING with main) touches Time.cpp and both tests, but it doesn't change asctime.
  • I built a standalone ASan+UBSan harness from the verbatim asctime_r/asctime from Time.cpp, clampedField/tableName, and the PR's CheckAsctime rows from GuestStrftime, plus the null-tm and null-buffer rows. It passes. The fixed 26 is safe, because every field is clamped (Fri Oct 99 99:99:99 9999\n is 25 characters plus the NUL).

Mutants (single edits to the new function):

Mutant Result
Drop the null-buffer check killed (ASan)
snprintf size 25 killed
snprintf size 64 survives, but it's equivalent (output never exceeds 26)
Drop errno = EINVAL killed
Null tm returns nullptr killed
Success returns nullptr killed

Requested change: a null buffer should throw instead of returning nullptr. The debt line says "the null-buffer result is assumed". CONTRIBUTING asks for unknown behaviour to throw and be recorded, not for an assumed result. FreeBSD's asctime_r doesn't check the buffer at all, so nullptr isn't attested anywhere. Suggested change:

-    if (buffer == nullptr) return nullptr;
+    if (buffer == nullptr) throw std::invalid_argument("asctime_r: null buffer");

<stdexcept> is already included in Time.cpp. The test in GuestTimeConversion.cpp would become:

-    Require(asctime_r_nid_postfix(&roundTrip, nullptr) == nullptr, "asctime_r accepted a null buffer");
+    bool nullBufferThrew = false;
+    try {
+        asctime_r_nid_postfix(&roundTrip, nullptr);
+    } catch (const std::invalid_argument&) {
+        nullBufferThrew = true;
+    }
+    Require(nullBufferThrew, "asctime_r accepted a null buffer");

The debt sentence then shrinks below the limit (1047 → 987 characters):

`asctime_r` shares the `asctime` formatting, unmeasured; a null buffer throws

I checked this variant in the same harness: it passes, and the current head fails the new row.

Minor point: the "Tested" section lists only check_conventions. Please add which ctest targets (GuestStrftime, GuestTimeConversion) you ran and on which OS.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5698068756 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12841#pullrequestreview-5698068756). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T13:00:01Z. **Review:** COMMENTED --- Reviewed head `d690c78347` against `main` `157b02c4f1`. I ran this on macOS arm64 with Apple clang. No console was used. **What I checked** - The NID is right: `python3 tools/nid_names.py --name asctime_r` gives `488p6whx47U`. `main` has no other `asctime_r`. - `git merge-tree --write-tree upstream/main <head>` merges cleanly, and `git diff --check` is clean. - `check_conventions.py --base upstream/main` reports 0 errors but **1 warning**: `docs/dev/debt/libc.md:202: [debt-length] ... 1047 characters`. The line was 909 characters on `main`, so this PR pushes it over the 1000 limit. - `pr_overlap.py --branch <head>` (1400 PRs): there are no same-export conflicts. Three PRs (#10895, #11843, #12593) touch the neighbouring debt lines 201-203. #10409 (CONFLICTING with main) touches `Time.cpp` and both tests, but it doesn't change `asctime`. - I built a standalone ASan+UBSan harness from the verbatim `asctime_r`/`asctime` from `Time.cpp`, `clampedField`/`tableName`, and the PR's `CheckAsctime` rows from `GuestStrftime`, plus the null-tm and null-buffer rows. **It passes.** The fixed `26` is safe, because every field is clamped (`Fri Oct 99 99:99:99 9999\n` is 25 characters plus the NUL). **Mutants** (single edits to the new function): | Mutant | Result | |---|---| | Drop the null-buffer check | killed (ASan) | | `snprintf` size 25 | killed | | `snprintf` size 64 | survives, but it's equivalent (output never exceeds 26) | | Drop `errno = EINVAL` | killed | | Null tm returns nullptr | killed | | Success returns nullptr | killed | **Requested change: a null buffer should throw instead of returning `nullptr`.** The debt line says *"the null-buffer result is assumed"*. CONTRIBUTING asks for unknown behaviour to throw and be recorded, not for an assumed result. FreeBSD's `asctime_r` doesn't check the buffer at all, so `nullptr` isn't attested anywhere. Suggested change: ```diff - if (buffer == nullptr) return nullptr; + if (buffer == nullptr) throw std::invalid_argument("asctime_r: null buffer"); ``` `<stdexcept>` is already included in `Time.cpp`. The test in `GuestTimeConversion.cpp` would become: ```diff - Require(asctime_r_nid_postfix(&roundTrip, nullptr) == nullptr, "asctime_r accepted a null buffer"); + bool nullBufferThrew = false; + try { + asctime_r_nid_postfix(&roundTrip, nullptr); + } catch (const std::invalid_argument&) { + nullBufferThrew = true; + } + Require(nullBufferThrew, "asctime_r accepted a null buffer"); ``` The debt sentence then shrinks below the limit (1047 → 987 characters): ``` `asctime_r` shares the `asctime` formatting, unmeasured; a null buffer throws ``` I checked this variant in the same harness: it passes, and the current head fails the new row. **Minor point:** the "Tested" section lists only `check_conventions`. Please add which ctest targets (`GuestStrftime`, `GuestTimeConversion`) you ran and on which OS.
rooklz commented 2026-10-29 13:58:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T13:58:19Z.

Review: APPROVED


Re-reviewed head e908061e32 against main 3adb909203 on macOS arm64 with Apple clang. I didn't use a console.

The change since my last review matches what I asked for. A null buffer now throws std::invalid_argument before anything is written. There's a test row for it, and the debt sentence is short again: line 202 is 987 characters, and check_conventions passes with no warnings.

  • Standalone ASan+UBSan harness. It takes asctime_r/asctime verbatim from Time.cpp with clampedField/tableName/the name tables, plus every CheckAsctime row from GuestStrftime, the null-tm and null-buffer rows from GuestTimeConversion, and one extra widest-output row (Fri Oct 99 99:99:99 9999\n, 25 characters plus the NUL, which fits the fixed 26). It passes.
  • Mutants: 7 of 7 killed. They were: the null-buffer throw dropped, snprintf size 25, errno = EINVAL dropped, null-tm returning nullptr, the success path returning nullptr, the seconds field swapped for minutes, and the year modulus 100000.
  • git diff --check, merge-tree against 3adb909203 and check_conventions --base upstream/main are all clean. pr_overlap probed 1407 PRs and found no conflicting files and no same exports. The only ones listed are PRs that already conflict with main and share Api.hpp/libc.md.

LGTM.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5698917080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12841#pullrequestreview-5698917080). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T13:58:19Z. **Review:** APPROVED --- Re-reviewed head `e908061e32` against `main` `3adb909203` on macOS arm64 with Apple clang. I didn't use a console. The change since my last review matches what I asked for. A null buffer now throws `std::invalid_argument` before anything is written. There's a test row for it, and the debt sentence is short again: line 202 is 987 characters, and `check_conventions` passes with no warnings. - **Standalone ASan+UBSan harness.** It takes `asctime_r`/`asctime` verbatim from `Time.cpp` with `clampedField`/`tableName`/the name tables, plus every `CheckAsctime` row from `GuestStrftime`, the null-tm and null-buffer rows from `GuestTimeConversion`, and one extra widest-output row (`Fri Oct 99 99:99:99 9999\n`, 25 characters plus the NUL, which fits the fixed `26`). **It passes.** - **Mutants: 7 of 7 killed.** They were: the null-buffer throw dropped, `snprintf` size 25, `errno = EINVAL` dropped, null-tm returning `nullptr`, the success path returning `nullptr`, the seconds field swapped for minutes, and the year modulus 100000. - `git diff --check`, `merge-tree` against `3adb909203` and `check_conventions --base upstream/main` are all clean. `pr_overlap` probed 1407 PRs and found no conflicting files and no same exports. The only ones listed are PRs that already conflict with `main` and share `Api.hpp`/`libc.md`. LGTM. AI-assisted: yes
CnRJay commented 2026-11-01 19:54:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:19Z.

Review: COMMENTED


Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (Unblocks: none is allowed), and it contained no code review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729937954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12841#pullrequestreview-5729937954). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:54:19Z. **Review:** COMMENTED --- Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (`Unblocks: none` is allowed), and it contained no code review.
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!12841
No description provided.