Skip to content

Fix async query result owner handoff#15

Merged
adsharma merged 1 commit intomainfrom
fix-macos-query-result-uaf
Apr 29, 2026
Merged

Fix async query result owner handoff#15
adsharma merged 1 commit intomainfrom
fix-macos-query-result-uaf

Conversation

@adsharma
Copy link
Copy Markdown
Contributor

@adsharma adsharma commented Apr 29, 2026

The bug: In the old code, currQueryResult->Unref() was called unconditionally at the start. Unref() decrements
the NAPI reference count on the JS wrapper object. If this was the last reference, the JS object could be garbage
collected, which would destroy the C++ NodeQueryResult object (since it's an ObjectWrapper). After that, accessing
currQueryResult->connection and currQueryResult->database would be a use-after-free.

The fix:
- When nextOwnedResult == nullptr, we just return undefined. We release/unref first, then callback. That's
fine.
- When nextOwnedResult != nullptr, we FIRST copy out connection and database (both are std::shared_ptr, so
copying increments their ref counts and is safe). Then we create the new instance. Only THEN do we
ReleaseAsyncUse() and Unref() the current result. This ensures we don't access currQueryResult after it could be
freed.

@adsharma adsharma force-pushed the fix-macos-query-result-uaf branch from 9f95be0 to 6aeb463 Compare April 29, 2026 21:01
@adsharma adsharma merged commit 42ef11c into main Apr 29, 2026
4 checks passed
@adsharma adsharma deleted the fix-macos-query-result-uaf branch April 29, 2026 21:21
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