Skip to content

Comments

fix(napi): validate status before copying in TypedArray owned ToNapiValue fallback#3080

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

fix(napi): validate status before copying in TypedArray owned ToNapiValue fallback#3080
graphite-app[bot] merged 1 commit intomainfrom
bisect/typed-array-owned

Conversation

@Brooooooklyn
Copy link
Member

@Brooooooklyn Brooooooklyn commented Dec 30, 2025

Note

Strengthens TypedArray owned ToNapiValue path by tightening ArrayBuffer creation and fallback handling.

  • Refactors TypedArray::<T>::to_napi_value to explicitly handle zero-length buffers via napi_create_arraybuffer
  • Validates napi_create_external_arraybuffer status and only falls back to napi_create_arraybuffer with data copy when napi_no_external_buffers_allowed
  • Ensures check_status! occurs before copying and only copies when length > 0; improves error messages for failures

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

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced TypedArray buffer handling with explicit support for empty buffers
    • Added automatic fallback mechanism when external buffers are unsupported, ensuring data integrity
    • Improved error messages for buffer creation failures

✏️ 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:19
@coderabbitai
Copy link

coderabbitai bot commented Dec 30, 2025

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

The arraybuffer.rs file's to_napi_value implementation in the impl_typed_array macro was refactored to handle zero-length buffers explicitly and introduce a fallback mechanism when external ArrayBuffers are not permitted by the runtime.

Changes

Cohort / File(s) Summary
ArrayBuffer TypedArray Path Handling
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
Added explicit branch for zero-length buffers creating ArrayBuffer with null pointer. Refactored non-empty buffer handling to attempt external ArrayBuffer creation with status checks, and fallback to regular ArrayBuffer with data copying if external buffers are disallowed (napi_no_external_buffers_allowed). Updated error messages and control flow to ensure proper TypedArray finalization.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 ArrayBuffers hop with care today,
Empty or full, they find their way,
When external paths refuse to stay,
Regular buffers copy, come what may—
TypedArrays thrive in every way!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: validating status before copying in TypedArray's ToNapiValue fallback implementation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c541fee and 6ac1d6b.

📒 Files selected for processing (1)
  • crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi]` attribute on impl blocks to define classes exported to JavaScript/TypeScript
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi(object)]` attribute for plain objects that should be mapped to TypeScript interfaces
📚 Learning: 2025-11-25T09:31:23.877Z
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi]` attribute on impl blocks to define classes exported to JavaScript/TypeScript

Applied to files:

  • crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
📚 Learning: 2025-11-25T09:31:23.877Z
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi(js_name = "...")]` attribute to rename Rust items in JavaScript/TypeScript

Applied to files:

  • crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs
🧬 Code graph analysis (1)
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs (2)
crates/napi/src/js_values/arraybuffer.rs (2)
  • into_raw (212-214)
  • new (208-210)
crates/napi/src/js_values/value_ref.rs (1)
  • new (19-31)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (21)
  • GitHub Check: Zig-Cross-Compile-On-Linux (armv7-unknown-linux-musleabihf)
  • GitHub Check: aarch64-pc-windows-msvc - node@24 - toolchain@ stable
  • GitHub Check: x86_64-pc-windows-msvc - node@24 - toolchain@ stable
  • GitHub Check: ASAN - windows-latest
  • GitHub Check: Memory leak detect job
  • GitHub Check: Build only test - aarch64-unknown-linux-ohos
  • GitHub Check: Test node wasi target (--cfg tokio_unstable)
  • GitHub Check: Test node wasi target
  • GitHub Check: Build only test - armv7-linux-androideabi
  • GitHub Check: ASAN - ubuntu-24.04
  • GitHub Check: build-and-test-msys2 (MINGW64)
  • GitHub Check: build-and-test-msys2 (CLANG64)
  • GitHub Check: Build only test - riscv64gc-unknown-linux-gnu
  • GitHub Check: build - aarch64-unknown-linux-gnu
  • GitHub Check: stable - armv7-unknown-linux-gnueabihf - node@22
  • GitHub Check: Test freebsd target
  • GitHub Check: build - s390x-unknown-linux-gnu
  • GitHub Check: Build only test - loongarch64-unknown-linux-gnu
  • GitHub Check: build - aarch64-unknown-linux-musl
  • GitHub Check: build - x86_64-unknown-linux-musl
  • GitHub Check: build-and-test-msys2 (UCRT64)
🔇 Additional comments (1)
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs (1)

747-787: Overall approach is sound but has issues that need addressing.

The refactoring correctly:

  • Handles zero-length buffers separately to avoid Rust's 0x1 dangling pointer issue with V8
  • Validates status before copying in the fallback path (line 780 before line 782)
  • Reclaims hint_ptr in the napi_no_external_buffers_allowed fallback (line 770)

However, the issues flagged in previous comments (misleading error messages and memory leak on unexpected errors) should be addressed before merging.


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

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 the error handling logic in the ToNapiValue implementation for typed arrays. The changes restructure the control flow for creating array buffers, separating the status checks into explicit branches rather than nesting them within a single check_status! macro call.

  • Extracts the zero-length buffer case into its own block with immediate error checking
  • Explicitly stores and checks the status from napi_create_external_arraybuffer using a mutable variable
  • Separates error handling for the fallback path when external buffers are not allowed

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Brooooooklyn Brooooooklyn force-pushed the bisect/arraybuffer-struct branch from 13f1fe1 to 512153f Compare December 30, 2025 03:36
@Brooooooklyn Brooooooklyn force-pushed the bisect/typed-array-owned branch from 9eb1cc0 to 4cc80d3 Compare December 30, 2025 03:36
@Brooooooklyn Brooooooklyn force-pushed the bisect/arraybuffer-struct branch from 512153f to 72668eb Compare December 30, 2025 03:40
@Brooooooklyn Brooooooklyn force-pushed the bisect/typed-array-owned branch from 4cc80d3 to 55a51d1 Compare December 30, 2025 03:40
@Brooooooklyn Brooooooklyn changed the title Bisect/typed array owned fix(napi): validate status before copying in TypedArray owned ToNapiValue fallback Dec 30, 2025
@Brooooooklyn Brooooooklyn force-pushed the bisect/arraybuffer-struct branch from 72668eb to 9945379 Compare December 30, 2025 04:17
@Brooooooklyn Brooooooklyn force-pushed the bisect/typed-array-owned branch 2 times, most recently from e48088b to 3d92833 Compare December 30, 2025 04:58
@Brooooooklyn Brooooooklyn force-pushed the bisect/arraybuffer-struct branch from 9945379 to 6752c3e Compare December 30, 2025 04:59
@graphite-app graphite-app bot changed the base branch from bisect/arraybuffer-struct to graphite-base/3080 December 30, 2025 05:24
@graphite-app
Copy link

graphite-app bot commented Dec 30, 2025

Merge activity

@graphite-app graphite-app bot changed the base branch from graphite-base/3080 to main December 30, 2025 05:38
…alue fallback (#3080)

<!-- CURSOR_SUMMARY -->
> [!NOTE]
> Strengthens `TypedArray` owned `ToNapiValue` path by tightening ArrayBuffer creation and fallback handling.
>
> - Refactors `TypedArray::<T>::to_napi_value` to explicitly handle zero-length buffers via `napi_create_arraybuffer`
> - Validates `napi_create_external_arraybuffer` status and only falls back to `napi_create_arraybuffer` with data copy when `napi_no_external_buffers_allowed`
> - Ensures `check_status!` occurs before copying and only copies when `length > 0`; improves error messages for failures
>
> <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 3d92833. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
@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 merged commit 6ac1d6b into main Dec 30, 2025
75 checks passed
@graphite-app graphite-app bot deleted the bisect/typed-array-owned branch December 30, 2025 05:58
@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