Skip to content

fix: avoid double free in external typed array creation - #72

Draft
GrapeBaBa wants to merge 1 commit into
mainfrom
fix/fromexternal-error-ownership
Draft

fix: avoid double free in external typed array creation#72
GrapeBaBa wants to merge 1 commit into
mainfrom
fix/fromexternal-error-ownership

Conversation

@GrapeBaBa

Copy link
Copy Markdown
Contributor

Summary

  • free the duplicated element buffer only for N-API errors returned before finalizer registration
  • leave potentially transferred buffers exclusively owned by the external ArrayBuffer finalizer
  • preserve original errors and keep existing external-memory accounting unchanged

Problem

TypedArray.fromExternal duplicates the input slice into allocator-owned native storage, then passes that allocation to napi_create_external_arraybuffer with a finalizer.

The existing catch-all frees the allocation for every external ArrayBuffer creation error. Node can return GenericFailure after it has already installed or invoked the finalizer, so the catch-all can free an allocation that the finalizer also owns.

Ownership boundary

  • NoExternalBuffersAllowed, PendingException, and CannotRunJS: Node returns before finalizer registration, so free the duplicated buffer and propagate the original error
  • other external ArrayBuffer errors: do not free because Node may already have installed or invoked the finalizer
  • later external-memory accounting or TypedArray view failures: keep cleanup with the already-created ArrayBuffer finalizer

Node implementation references:

Verification

  • zig fmt --check src/js/typed_arrays.zig
  • zig build test:zapi
  • zig build
  • pnpm test:js — 120/120
  • pnpm lint:js
  • git diff --check origin/main
  • independent Zig ownership audit — PASS, 0 MUST / 0 SHOULD / 0 NIT

Coverage limitation

The existing Node addon tests cover the production success paths, but they cannot deterministically inject a post-registration GenericFailure or count finalizer callbacks. No production mock seam or compatibility path was added for those cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant