Harden nodex-api memory safety at N-API boundaries#5
Merged
Conversation
Agent-Logs-Url: https://github.com/uuhan/nodex/sessions/1e0cd6d6-2c3b-4d0d-9903-bbca5ec71947 Co-authored-by: uuhan <3286111+uuhan@users.noreply.github.com>
Agent-Logs-Url: https://github.com/uuhan/nodex/sessions/1e0cd6d6-2c3b-4d0d-9903-bbca5ec71947 Co-authored-by: uuhan <3286111+uuhan@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
uuhan
April 1, 2026 15:48
View session
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.
This PR addresses unsafe boundary behavior in the Rust ↔ Node-API layer, with focus on memory-safety risks (UB, aliasing, and ownership misuse).
The changes reduce footguns in core handle/data APIs while keeping the surface area mostly intact.
Unsafe out-parameter handling (
napi_call!(?))napi_call!(?)now returns(status, Option<value>)and only materializesvalueon success.Reference ownership correctness (
NapiRef)ClonefromNapiRefto avoid accidental duplicate ownership of the samenapi_ref.Drop(napi_delete_reference).Mutability/aliasing API hardening
&self -> &mut Tpatterns with explicit mutable accessors:JsExternal::get(&self) -> &T+get_mut(&mut self) -> &mut TNapiValueT::unwrap(&self) -> Option<&T>+unwrap_mut(&mut self) -> Option<&mut T>NapiEnv::get_instance_data(&self) -> Option<&T>+get_instance_data_mut(&mut self) -> Option<&mut T>Async work callback type safety
NapiAsyncWorkcompletion path.completecallback storage/retrieval now uses consistentFnMut(...) -> NapiResult<()>typing.Call-site alignment