ucrt: malloc the result of _getcwd and _fullpath given no buffer #74

Manually merged
github merged 5 commits from github-bot/gh-74 into main 2026-11-26 17:09:33 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 13:41 UTC · original
Olima34:fix/ucrt-getcwd-fullpath-allocate → main · +54 −13 · 1 files · 5 commits · merged 2026-11-26 17:05 UTC

What this changes

Given a null buffer, _getcwd and _fullpath made a leaked host string, then checked it against max, so _getcwd(NULL, 0) (Microsoft's own example, followed by free) failed with ERANGE. Had it succeeded, the title's free() would have been handed a block malloc never made. Both now return a malloc block, at least max bytes for _getcwd, ignoring max for _fullpath (reference), as _wgetcwd already did. _getcwd with a buffer and max <= 0 is now EINVAL, as for _wgetcwd, instead of writing a NUL into an empty buffer.

New test getcwd_and_fullpath_malloc_when_given_no_buffer.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory (all pass, native probe tests included)
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings, also with --all-features; clippy for x86_64-apple-darwin reports nothing in the files touched (host_posix is not built for Windows)
  • arm64 cargo test --workspace and cargo xtask policy
  • The new test fails on main and passes with the change
  • Ran the game: not done, no copy of it here

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
**@⁠Olima34** on GitHub · 2026-11-26 13:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/74) `Olima34:fix/ucrt-getcwd-fullpath-allocate` → `main` · +54 −13 · 1 files · 5 commits · merged 2026-11-26 17:05 UTC ## What this changes Given a null buffer, `_getcwd` and `_fullpath` made a leaked host string, then checked it against `max`, so `_getcwd(NULL, 0)` ([Microsoft's own example](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd), followed by `free`) failed with `ERANGE`. Had it succeeded, the title's `free()` would have been handed a block `malloc` never made. Both now return a `malloc` block, at least `max` bytes for `_getcwd`, ignoring `max` for `_fullpath` ([reference](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fullpath-wfullpath)), as `_wgetcwd` already did. `_getcwd` with a buffer and `max <= 0` is now `EINVAL`, as for `_wgetcwd`, instead of writing a NUL into an empty buffer. New test `getcwd_and_fullpath_malloc_when_given_no_buffer`. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` (all pass, native probe tests included) - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`, also with `--all-features`; clippy for `x86_64-apple-darwin` reports nothing in the files touched (`host_posix` is not built for Windows) - [x] arm64 `cargo test --workspace` and `cargo xtask policy` - [x] The new test fails on `main` and passes with the change - [ ] Ran the game: not done, no copy of it here ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
Given a null buffer, _getcwd and _fullpath made a leaked host string and then checked it against max, so _getcwd(NULL, 0), Microsoft's own example, failed with ERANGE; and had it succeeded, the title's free() would have been handed a block malloc never made. Both now return a malloc block (at least max bytes for _getcwd, ignoring max for _fullpath), as _wgetcwd already did. _getcwd with a buffer and max <= 0 is EINVAL, as for _wgetcwd, instead of writing a NUL into an empty buffer.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:45:17 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 14:55 UTC · original · review (dismissed)

_getcwd and _fullpath with a NULL buffer now return a block the title can free. Thanks. (Minor follow-up: set errno when malloc fails.)

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 14:55 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/74#pullrequestreview-6076266670) · **review (dismissed)** _getcwd and _fullpath with a NULL buffer now return a block the title can free. Thanks. (Minor follow-up: set errno when malloc fails.)
Review follow-up on the malloc path: heap::malloc already sets ENOMEM when it fails (its checked helper), which a comment now says. The other failure, the host's current_dir, returned null without errno; it now sets it as _wgetcwd does.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-26 15:13 UTC · original

On the malloc path errno was already set: heap::malloc goes through checked, which sets ENOMEM on a null result, and a comment at the call now says so. While checking, I found that _getcwd's other failure (the host's current_dir) returned null without errno. It now sets it as _wgetcwd does.

**@⁠Olima34** on GitHub · 2026-11-26 15:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/74#issuecomment-7195954696) On the malloc path errno was already set: `heap::malloc` goes through `checked`, which sets `ENOMEM` on a null result, and a comment at the call now says so. While checking, I found that `_getcwd`'s other failure (the host's `current_dir`) returned null without errno. It now sets it as `_wgetcwd` does.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 15:55 UTC · original · approved these changes

Both follow-ups are in (errno on the malloc path is covered by checked, and _getcwd sets errno on a current_dir failure). CI is green. Approving.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 15:55 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/74#pullrequestreview-6077112889) · **approved these changes** Both follow-ups are in (errno on the malloc path is covered by checked, and _getcwd sets errno on a current_dir failure). CI is green. Approving.
github manually merged commit d5447914dc into main 2026-11-26 17:09:33 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.