fix(libc): retain owned storage borrowed by putenv #4823

Closed
QRMOE wants to merge 2 commits from upstream-pr/4823 into main
QRMOE commented 2026-10-12 23:00:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-12T23:00:50Z.


What

Keep environment-owned storage alive when putenv receives a pointer into it. On current main, setenv("A", "A=value", 1); putenv(getenv("A")); replaces the owning entry with a borrowed pointer and frees its backing string. Later environment allocations can overwrite the registered value or make it disappear.

Share ownership of the backing string with entries that borrow from it, including nested aliases. Ordinary caller-owned putenv buffers stay borrowed and remain live when the caller edits them. The allocation is released when its last owning entry is removed.

FreeBSD 11.4 putenv retains the old owned entry when registering such a pointer. The regression covers short and large strings, self aliases, cross-variable aliases after removal or a larger replacement, nested aliases, and subsequent allocation churn.

Tested

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

  • The regression fails on unchanged current main with putenv lost an aliased environment value.
  • A separate harness ran five ownership/live-buffer sequences at lengths 1, 12, 512 and 4096 against both the original FreeBSD 11.4 routine and the built production library. All expectations matched; only platform/environment adapters were added to the reference outside the repository.
  • Full default build and explicit libs build passed. guest_environment, guest_errors, guest_strings, guest_wide_strings and guest_collate all passed (five tests, 3.34 s total).
  • Conventions and git diff --check passed. No title playback was tested.

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
  • 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:4835995604 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4823). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-12T23:00:50Z. --- ### What Keep environment-owned storage alive when `putenv` receives a pointer into it. On current main, `setenv("A", "A=value", 1); putenv(getenv("A"));` replaces the owning entry with a borrowed pointer and frees its backing string. Later environment allocations can overwrite the registered value or make it disappear. Share ownership of the backing string with entries that borrow from it, including nested aliases. Ordinary caller-owned `putenv` buffers stay borrowed and remain live when the caller edits them. The allocation is released when its last owning entry is removed. [FreeBSD 11.4 `putenv`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdlib/getenv.c) retains the old owned entry when registering such a pointer. The regression covers short and large strings, self aliases, cross-variable aliases after removal or a larger replacement, nested aliases, and subsequent allocation churn. ### Tested Windows, MinGW GCC 15.2, Release, `BUILD_TESTING=ON`: - The regression fails on unchanged current main with `putenv lost an aliased environment value`. - A separate harness ran five ownership/live-buffer sequences at lengths 1, 12, 512 and 4096 against both the original FreeBSD 11.4 routine and the built production library. All expectations matched; only platform/environment adapters were added to the reference outside the repository. - Full default build and explicit libs build passed. guest_environment, guest_errors, guest_strings, guest_wide_strings and guest_collate all passed (five tests, 3.34 s total). - Conventions and `git diff --check` passed. No title playback was tested. ### 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 under Silent stubs in their library's technical debt file - [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
nicolasfalesy commented 2026-10-13 07:24:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:24:56Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4927, #4903, #4877, #4822 and #4823 (no conflicts): guest_environment passes. With this PR's source change reverted (test kept), guest_environment fails on Linux, so the check guards the change on Linux too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140513303 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4823#issuecomment-6140513303). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:24:56Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4927, #4903, #4877, #4822 and #4823 (no conflicts): `guest_environment` passes. With this PR's source change reverted (test kept), `guest_environment` fails on Linux, so the check guards the change on Linux too.
oneandonlydean commented 2026-10-14 02:37:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:37:31Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159641310 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4823#issuecomment-6159641310). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:37:31Z. --- 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.
QRMOE commented 2026-10-15 07:43:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-15T07:43:41Z.


Understood. I’ll leave this closed and revisit it only with a title ID and a current-main log showing the failure. I’ll hold off on new PRs without a title needing the change while the queue is high.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6187117022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4823#issuecomment-6187117022). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-15T07:43:41Z. --- Understood. I’ll leave this closed and revisit it only with a title ID and a current-main log showing the failure. I’ll hold off on new PRs without a title needing the change while the queue is high.

Pull request closed

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!4823
No description provided.