Skip to content

Comments

fix(napi): validate status before copying in remaining TypedArray fallback paths#3076

Merged
graphite-app[bot] merged 1 commit intomainfrom
bisect/typed-array-mut
Dec 30, 2025
Merged

fix(napi): validate status before copying in remaining TypedArray fallback paths#3076
graphite-app[bot] merged 1 commit intomainfrom
bisect/typed-array-mut

Conversation

@Brooooooklyn
Copy link
Member

@Brooooooklyn Brooooooklyn commented Dec 30, 2025

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

Written by Cursor Bugbot for commit f2e1e07. This will update automatically on new commits. Configure here.

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.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings December 30, 2025 03:18
@coderabbitai
Copy link

coderabbitai bot commented Dec 30, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit 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.

📝 Walkthrough

Walkthrough

Refactors 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

Cohort / File(s) Summary
ArrayBuffer Error Handling
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
Adds guards for zero-length buffer operations to prevent null dereferences, introduces explicit status checks for external buffer creation paths, handles napi_no_external_buffers_allowed fallback by switching to regular arraybuffer creation with data copy, and extends conditional compilation to exclude wasm targets in debug assertion blocks.
Buffer Compilation Guards
crates/napi/src/bindgen_runtime/js_values/buffer.rs
Narrows compilation scope of thread_local BUFFER_DATA static by adding non-wasm target gate alongside existing debug_assertions and windows conditions.
Environment Buffer Creation
crates/napi/src/env.rs
Separates zero-length and positive-length buffer creation paths with explicit status checks, implements fallback behavior for napi_no_external_buffers_allowed by dropping hints and creating backing arraybuffers, ensures data copying precedes finalization, and adds consistent error messages for all buffer creation failure modes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 Buffers now guarded from null-pointer's bite,
Zero-length edges handled just right,
Finalize before copy, errors in flight—
External buffers fall back with grace,
Safe bunny hops through memory space! 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly and accurately summarizes the main change: validating status before copying in TypedArray fallback paths, which aligns with the core improvements across all three modified files.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Brooooooklyn Brooooooklyn changed the base branch from main to graphite-base/3076 December 30, 2025 03:30
@Brooooooklyn Brooooooklyn changed the base branch from graphite-base/3076 to bisect/typed-array-owned December 30, 2025 03:30
Copy link
Member Author

Brooooooklyn commented Dec 30, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • ready-to-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@graphite-app
Copy link

graphite-app bot commented Dec 30, 2025

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 -->
@graphite-app graphite-app bot force-pushed the bisect/typed-array-owned branch from 3d92833 to 6ac1d6b Compare December 30, 2025 05:38
@graphite-app graphite-app bot force-pushed the bisect/typed-array-mut branch from f2e1e07 to 51e65eb Compare December 30, 2025 05:39
Base automatically changed from bisect/typed-array-owned to main December 30, 2025 05:58
@graphite-app graphite-app bot merged commit 51e65eb into main Dec 30, 2025
75 checks passed
@graphite-app graphite-app bot deleted the bisect/typed-array-mut branch December 30, 2025 05:59
@github-actions github-actions bot mentioned this pull request Dec 30, 2025
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