Fix UB from error out-params not written on success paths - #104
Open
harmony7 wants to merge 1 commit into
Open
Conversation
The bridge error convention declares the out-param uninitialized on the C++
side and relies on the Rust side to write it. `try_fe!` (and the equivalent
`try_kv!` / `try_is!` macros) do set it to null on success, so the pattern is
sound for any function that routes every path through a macro.
Two KV store functions bypass those macros and wrote `err` only on their error
path, leaving the success path reading an indeterminate pointer:
- `ListResponse::next()` (src/kv_store.rs) - the `Ok(page)` arm set `out` only.
- `m_static_kv_store_kv_store_open()` - the `Ok(..)` arms set `out` only.
When the stack slot happened to be non-zero, the C++ wrapper saw `err !=
nullptr`, reported a bogus error, and called `rust::Box::from_raw()` on garbage,
which would later drop a garbage box. Cross-language LTO happened to leave
those slots zeroed, so the test suite passed; building with LTO off surfaced it
as a failure of `REQUIRE(page.has_value())` at test/kv_store.cpp:92. Both
outcomes were luck rather than correctness.
Both functions now null `err` on entry, which also covers the paths that return
false without an error at all: iteration finishing in `next()`, and `Ok(None)`
(store does not exist) in `open()`, which the C++ side must be able to tell
apart from a real failure.
Also gives all 67 error out-param declarations across include/ and src/cpp/ an
explicit `{nullptr}` initializer. That is redundant where a `try_*!` macro
already guarantees the write, but it makes the convention robust by default so
a future bridge function that forgets cannot reintroduce this class of bug.
Verified by building with LTO disabled, where kv_store previously failed and now
passes (7/7); the normal LTO build and lint gates are unaffected.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bridge error convention declares the out-param uninitialized on the C++ side and relies on the Rust side to write it.
try_fe!(and the equivalenttry_kv!/try_is!macros) do set it to null on success, so the pattern is sound for any function that routes every path through a macro.Two KV store functions bypass those macros and wrote
erronly on their error path, leaving the success path reading an indeterminate pointer:ListResponse::next()(src/kv_store.rs) - theOk(page)arm setoutonly.m_static_kv_store_kv_store_open()- theOk(..)arms setoutonly.When the stack slot happened to be non-zero, the C++ wrapper saw
err != nullptr, reported a bogus error, and calledrust::Box::from_raw()on garbage, which would later drop a garbage box. Cross-language LTO happened to leave those slots zeroed, so the test suite passed; building with LTO off surfaced it as a failure ofREQUIRE(page.has_value())at test/kv_store.cpp:92. Both outcomes were luck rather than correctness.Both functions now null
erron entry, which also covers the paths that return false without an error at all: iteration finishing innext(), andOk(None)(store does not exist) inopen(), which the C++ side must be able to tell apart from a real failure.Also gives all 67 error out-param declarations across include/ and src/cpp/ an explicit
{nullptr}initializer. That is redundant where atry_*!macro already guarantees the write, but it makes the convention robust by default so a future bridge function that forgets cannot reintroduce this class of bug.Verified by building with LTO disabled, where kv_store previously failed and now passes (7/7); the normal LTO build and lint gates are unaffected.