ucrt: read no further than the count in strncpy, strncat and their _s forms #40
No reviewers
Labels
No labels
accessibility
bug
claimed
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
tester report
urgent
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/LakshanSivakumar--Native-Victory-macOS!40
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/40/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Repla09 on GitHub · 2026-11-26 11:07 UTC · original
Repla09:fix/ucrt-counted-copy-overread→main· +149 −22 · 2 files · 2 commits · merged 2026-11-26 11:43 UTCg_ncpy,g_ncatandg_cpy_stook the whole source length before limiting it to the count, sostrncpy,strncat,strncpy_sandstrncat_s(and thewcsforms) scan the source for a NUL past the count. A counted source needn't have one: copying a fixed-size field with its size is the usual case. When such a field ends where a mapping ends, the native host faults instead of copying.Now they read at most the count. For
_TRUNCATEthat's one unit more than fits, which is how a truncation is detected.Testing
counted_copies_read_no_further_than_their_countplaces an unterminated source right before a no-access page and covers all eight functions, plus_TRUNCATE. Without the fix the test binary dies with SIGBUS; with it, it passes.cargo test --target x86_64-apple-darwin -p victory-hlepasses (220 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter on this machine).x86_64-apple-darwinshows no warnings instring.rs.rustfmt --checkandcargo xtask policypass.@LakshanSivakumar on GitHub · 2026-11-26 11:22 UTC · original · requested changes
Thanks for the fix and the guard-page test. The plain
strncpy/strncatpaths match the C contract now. A few things before merging:_sscans. They should stop at the remaining destination capacity (min(n, size), or the remaining space for concatenation). Today a 4-element destination with a long source still scans all the way ton, though four non-NUL elements already proveERANGE(ucrt/string.rs:~316).dst[0]on append overflow. OnERANGEthe append path leaves the old string visible. MSVC clearsdst[0]before reporting (~347).strncpyleaves the next byte alone.docs/native-host.md(AGENTS.md asks for one).@Repla09 on GitHub · 2026-11-26 11:30 UTC · original
Thanks for the review. All four are in
d2084ba: the_sscans stop at the room left (a count past the room with a 4-unit source before the guard page now reads 4, not 100), an error empties the whole destination, the test uses a 0xEE sentinel and nonempty destinations, and native-host.md has a note under the C runtime. Rebased onto main.@LakshanSivakumar on GitHub · 2026-11-26 11:42 UTC · original · approved these changes
Thanks for addressing all four points. The guard-page and sentinel checks now cover the scan limit, the destination clearing and termination, and the UCRT notes are updated. Rosetta tests, Windows clippy and policy pass merged with main. Merging.