docs(napi): clarify unsafe function invariants#3267
Conversation
📝 WalkthroughWalkthroughSafety documentation across the N-API crate was made more explicit: unsafe functions now list concrete caller preconditions for environment validity, napi_value/pointer validity, data lifetime and size, and finalize callback responsibilities. No executable logic or public signatures were changed. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs`:
- Around line 1037-1041: The doc comment incorrectly states `data_len` as a byte
count; update the safety docs in arraybuffer.rs to clarify that `data_len` is an
element count and that the backing storage must be at least `data_len *
size_of::<$rust_type>()` bytes (for typed-array slices like i16/i32/f32/f64),
and keep the existing note about the finalize callback; target the doc block
around the `data_len` and `finalize` description in this file to replace "at
least `len` bytes" with the element-count/size_of formulation and a short note
that `data_len` represents number of elements.
In `@crates/napi/src/js_values/string/utf16.rs`:
- Around line 114-119: Update the safety doc for the UTF-16 string to clarify
that `len` counts UTF-16 code units rather than bytes: change the phrase "at
least `len` bytes" to "at least `len` UTF-16 code units (i.e. at least `len *
size_of::<u16>()` bytes)". Ensure the same clarification is applied to the
bullet mentioning the finalize callback and any nearby references to `len` so
callers understand the storage requirement for the backing data and the meaning
of `len`.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 59a95c4c-e62b-423e-a3e1-26cf64e1f4f5
📒 Files selected for processing (7)
crates/napi/src/bindgen_runtime/js_values.rscrates/napi/src/bindgen_runtime/js_values/arraybuffer.rscrates/napi/src/bindgen_runtime/js_values/buffer.rscrates/napi/src/env.rscrates/napi/src/js_values/string/latin1.rscrates/napi/src/js_values/string/utf16.rscrates/napi/src/js_values/unknown.rs
There was a problem hiding this comment.
♻️ Duplicate comments (1)
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs (1)
1040-1040:⚠️ Potential issue | 🟡 MinorDoc references the wrong parameter name.
The function signature uses
data_len(line 1054), but the safety bullet still says “at leastlenelements”. Please rename to match the actual parameter so readers can map the requirement to the API.📝 Proposed fix
- #[doc = "- The data pointer is valid for the lifetime of the buffer and points to a memory region of at least `len` elements"] + #[doc = "- The data pointer is valid for the lifetime of the buffer and points to a memory region of at least `data_len` elements"]🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs` at line 1040, Update the doc comment that currently says "at least `len` elements" to use the actual parameter name `data_len` so it matches the function signature; locate the safety bullet in the ArrayBuffer-related docs in arraybuffer.rs (the doc for the function that accepts the parameter `data_len`) and replace the reference `len` with `data_len`, and scan nearby documentation for any other mismatched occurrences to keep parameter names consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs`:
- Line 1040: Update the doc comment that currently says "at least `len`
elements" to use the actual parameter name `data_len` so it matches the function
signature; locate the safety bullet in the ArrayBuffer-related docs in
arraybuffer.rs (the doc for the function that accepts the parameter `data_len`)
and replace the reference `len` with `data_len`, and scan nearby documentation
for any other mismatched occurrences to keep parameter names consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d69d1e09-8aa8-43b7-896c-c4bc3405914d
📒 Files selected for processing (2)
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rscrates/napi/src/js_values/string/utf16.rs
✅ Files skipped from review due to trivial changes (1)
- crates/napi/src/js_values/string/utf16.rs
Clarified unsafe function invariants in the safety comments.
Summary by CodeRabbit