fix(napi): validate status before copying in remaining TypedArray fallback paths#3076
fix(napi): validate status before copying in remaining TypedArray fallback paths#3076graphite-app[bot] merged 1 commit intomainfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughRefactors buffer and arraybuffer creation logic across three files to add explicit error handling for edge cases, including guards against null pointer dereferences for zero-length buffers, status checks for external buffer fallback scenarios (napi_no_external_buffers_allowed), and conditional compilation adjustments excluding wasm targets. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
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.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
This PR refactors typed array and arraybuffer creation logic to improve error handling, fix memory safety issues, and add WebAssembly support. The changes focus on making the code more robust when external buffers are not allowed by the JavaScript runtime.
Key changes:
- Refactored error handling to check status codes immediately after each operation rather than at the end of complex expressions
- Added safety checks to ensure data is only copied when
length > 0, preventing potential issues with zero-length buffers - Fixed finalize callback ordering to ensure data is copied before finalizers run (since finalizers may free the source data) and that finalizers always run even on error
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/napi/src/env.rs | Refactored arraybuffer creation with improved error handling and added length checks before copy operations |
| crates/napi/src/bindgen_runtime/js_values/buffer.rs | Extended cfg attribute to exclude wasm targets from BUFFER_DATA thread local |
| crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs | Major refactoring of ArrayBuffer and TypedArray creation logic with improved error handling, safety checks, and finalize callback ordering; also updated cfg attributes for wasm support |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
9eb1cc0 to
4cc80d3
Compare
490be2e to
d213203
Compare
4cc80d3 to
55a51d1
Compare
d213203 to
7ebc052
Compare
55a51d1 to
e48088b
Compare
7ebc052 to
07b46c7
Compare
e48088b to
3d92833
Compare
07b46c7 to
f2e1e07
Compare
Merge activity
|
…lback paths (#3076) <!-- CURSOR_SUMMARY --> > [!NOTE] > Strengthens error handling and memory safety across ArrayBuffer/TypedArray creation and fallback paths. > > - In fallback when external arraybuffers aren’t allowed, now check `status` before copying, only copy when `length > 0`, and always call `finalize`; propagate errors consistently > - In `&mut TypedArray::to_napi_value`, reclaim leaked hint safely and immediately clear `val` fields before early returns to avoid dangling pointers; copy into newly allocated ArrayBuffer when needed > - Add missing `check_status!` validations for buffer creation across typed array slices and `Uint8ClampedSlice`, including zero-length guards > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit f2e1e07. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Improved buffer creation logic with enhanced error handling and validation. * Added safeguards for zero-length buffer operations to prevent edge-case failures. * Strengthened fallback mechanisms and error propagation in buffer creation paths. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
3d92833 to
6ac1d6b
Compare
f2e1e07 to
51e65eb
Compare

Note
Strengthens error handling and memory safety across ArrayBuffer/TypedArray creation and fallback paths.
statusbefore copying, only copy whenlength > 0, and always callfinalize; propagate errors consistently&mut TypedArray::to_napi_value, reclaim leaked hint safely and immediately clearvalfields before early returns to avoid dangling pointers; copy into newly allocated ArrayBuffer when neededcheck_status!validations for buffer creation across typed array slices andUint8ClampedSlice, including zero-length guardsWritten by Cursor Bugbot for commit f2e1e07. This will update automatically on new commits. Configure here.
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.