Skip to content

fix(napi): cleanup memory issues - #2949

Merged
Brooooooklyn merged 17 commits into
mainfrom
10-04-fix_napi_cleanup_memory_issues
Oct 5, 2025
Merged

Brooooooklyn merged 17 commits into
mainfrom
10-04-fix_napi_cleanup_memory_issues

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Oct 4, 2025 •

Copy link
Copy Markdown
Member

Note

Fixes buffer ownership/capacity and error-string retrieval to prevent leaks and undefined behavior.

  • ArrayBuffer/TypedArray (js_values/arraybuffer.rs):
    • Uint8Array::from_string now restores String with original capacity (not len) when finalizing.
  • Env (env.rs):
    • create_buffer_with_data creates the external buffer hint only for non-empty buffers (avoids unnecessary alloc on empty case).
  • Error handling (error.rs):
    • Use CStr::from_ptr(...).to_str()?.to_owned() instead of CString::from_raw to read napi_extended_error_info.error_message without taking ownership.

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

Copy link
Copy Markdown
Member Author

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.

@Brooooooklyn
Brooooooklyn requested a review from Copilot October 4, 2025 02:51
@Brooooooklyn

Copy link
Copy Markdown
Member Author

cursor review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes memory management issues in the NAPI bindings by correcting improper memory handling patterns that could lead to memory leaks or undefined behavior.

  • Replaces CString::from_raw() with CStr::from_ptr() to avoid taking ownership of C strings
  • Moves heap allocation of hint pointer inside the conditional block where it's actually used
  • Fixes String reconstruction to use the original capacity instead of length

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
crates/napi/src/error.rs Updates error message handling to avoid taking ownership of C strings
crates/napi/src/env.rs Moves hint pointer allocation to prevent memory leak when not used
crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs Fixes String reconstruction to use correct capacity parameter

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread crates/napi/src/error.rs Resolved

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch 7 times, most recently from 29d05a7 to 432b4de Compare October 4, 2025 07:48
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch from 432b4de to 7681cb9 Compare October 4, 2025 08:01
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch from e9fb176 to 0b68580 Compare October 4, 2025 09:51
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch 2 times, most recently from 8b535dd to a009c94 Compare October 4, 2025 11:00
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch 3 times, most recently from e715656 to a3c42e3 Compare October 4, 2025 13:21
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch from a3c42e3 to da66640 Compare October 4, 2025 14:00
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch from 2306ef5 to 668c3b0 Compare October 5, 2025 02:47
@Brooooooklyn
Brooooooklyn force-pushed the 10-04-fix_napi_cleanup_memory_issues branch from 4a1bfbf to 8cf17c5 Compare October 5, 2025 04:06
@Brooooooklyn
Brooooooklyn merged commit 8f40ebd into main Oct 5, 2025
@Brooooooklyn
Brooooooklyn deleted the 10-04-fix_napi_cleanup_memory_issues branch October 5, 2025 11:35
@github-actions github-actions Bot mentioned this pull request Oct 3, 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.

2 participants