ucrt: follow the CRT's buffer rules for legacy and ISO vsprintf #70

Manually merged
github merged 2 commits from github-bot/gh-70 into main 2026-11-26 15:46:31 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 13:41 UTC · original
Olima34:fix/ucrt-vsprintf-buffer-rules → main · +59 −22 · 2 files · 2 commits · merged 2026-11-26 15:41 UTC

What this changes

__stdio_common_vs(w)printf had its two non-standard buffer modes swapped, and the sizing idiom failed outside the standard one. Per Microsoft's _vsnprintf reference and the CRT headers' choice of options (_vsnwprintf_l passes LEGACY_VSPRINTF_NULL_TERMINATION, _vswprintf_c_l, behind ISO vswprintf, passes none):

Case _vsnprintf (legacy) ISO vswprintf (no option)
text fills the buffer exactly whole text, no NUL, returns len (was: NUL, -1) cut, NUL, -1 (was: no NUL, len)
text longer than the buffer count chars, no NUL, -1 (was: NUL) cut, NUL, -1 (was: no NUL)
buffer == NULL, count == 0 the length (was -1) the length (was -1)

So swprintf(buf, 4, L"%d", 1234) no longer leaves buf unterminated, and _vsnprintf(NULL, 0, fmt, ...) returns the size to allocate. A null buffer with room is EINVAL. The standard (snprintf) mode is unchanged.

The existing test encoded the swapped behaviour; it is replaced by legacy_and_iso_printf_truncate_as_the_ucrt_does, which covers each mode and case, narrow and wide.

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/70) `Olima34:fix/ucrt-vsprintf-buffer-rules` → `main` · +59 −22 · 2 files · 2 commits · merged 2026-11-26 15:41 UTC ## What this changes `__stdio_common_vs(w)printf` had its two non-standard buffer modes swapped, and the sizing idiom failed outside the standard one. Per [Microsoft's `_vsnprintf` reference](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/vsnprintf-vsnprintf-vsnprintf-l-vsnwprintf-vsnwprintf-l) and the CRT headers' choice of options (`_vsnwprintf_l` passes `LEGACY_VSPRINTF_NULL_TERMINATION`, `_vswprintf_c_l`, behind ISO `vswprintf`, passes none): | Case | `_vsnprintf` (legacy) | ISO `vswprintf` (no option) | |---|---|---| | text fills the buffer exactly | whole text, no NUL, returns `len` (was: NUL, -1) | cut, NUL, -1 (was: no NUL, `len`) | | text longer than the buffer | `count` chars, no NUL, -1 (was: NUL) | cut, NUL, -1 (was: no NUL) | | `buffer == NULL`, `count == 0` | the length (was -1) | the length (was -1) | So `swprintf(buf, 4, L"%d", 1234)` no longer leaves `buf` unterminated, and `_vsnprintf(NULL, 0, fmt, ...)` returns the size to allocate. A null buffer with room is `EINVAL`. The standard (`snprintf`) mode is unchanged. The existing test encoded the swapped behaviour; it is replaced by `legacy_and_iso_printf_truncate_as_the_ucrt_does`, which covers each mode and case, narrow and wide. ## 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)
__stdio_common_vs(w)printf had the two non-standard modes swapped. With
LEGACY_VSPRINTF_NULL_TERMINATION (_vsnprintf, _vsnwprintf) a text that
fills the buffer exactly is now returned whole without a NUL, and a
longer one is cut without a NUL (-1). With no option (ISO vswprintf,
_vswprintf_c_l in the CRT headers) a cut text now always ends in a NUL
(-1): swprintf(buf, 4, L"%d", 1234) used to leave the buffer unterminated.

No buffer with a count of 0, the sizing idiom, now returns the length in
every mode instead of -1 outside the standard one; no buffer with room is
EINVAL, as Microsoft's behavior summary for these functions says.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:45:08 +00:00
Author
Owner

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

The legacy and ISO vsprintf buffer modes now match UCRT, including EINVAL for a NULL buffer with count > 0. Thanks.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 14:55 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/70#pullrequestreview-6076267009) · **approved these changes** The legacy and ISO vsprintf buffer modes now match UCRT, including EINVAL for a NULL buffer with count > 0. Thanks.
github manually merged commit 4f2c6d6d0e into main 2026-11-26 15:46:31 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.