Skip to content

Fix UB from error out-params not written on success paths - #104

Open
harmony7 wants to merge 1 commit into
mainfrom
kats/fix-uninit-err-outparam
Open

Fix UB from error out-params not written on success paths#104
harmony7 wants to merge 1 commit into
mainfrom
kats/fix-uninit-err-outparam

Conversation

@harmony7

Copy link
Copy Markdown
Member

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.

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>
@harmony7
harmony7 requested review from TartanLlama and zkat August 24, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant