ffi: refresh cached string buffers on every call - #65051
Conversation
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65051 +/- ##
==========================================
- Coverage 90.33% 90.33% -0.01%
==========================================
Files 760 760
Lines 248522 248517 -5
Branches 46904 46889 -15
==========================================
- Hits 224513 224486 -27
- Misses 15444 15453 +9
- Partials 8565 8578 +13
🚀 New features to boost your workflow:
|
MILLERMARRU
left a comment
There was a problem hiding this comment.
Makes sense once you see what overwrite_string in the test does. The caching keyed on entry.string === value, which is a valid optimization only if the native buffer's bytes can't change out from under you between calls with the same JS string, but that's exactly what FFI exists to let native code do, write into a buffer you handed it. Once a native call mutates the backing memory, a second call with the identical JS string would have returned the pointer to the now-stale bytes instead of re-encoding.
Confirmed the removal doesn't leave entry.string referenced anywhere else in the file, it was only ever read by the check that's gone now, so no dead field left behind. The buffer itself (entry.buffer/entry.pointer) still gets reused across calls, which is fine, that part's genuinely safe to cache, it's just the "skip rewriting" shortcut that wasn't.
Native code can mutate temporary string storage during an FFI call. Rewrite cached buffers on every conversion so a later call with the same JavaScript string receives a fresh copy of its UTF-8 bytes. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol
a88fe4c to
8f76513
Compare
Fixes: #65050
Native code can mutate temporary string storage during an FFI call. Rewrite cached buffers on every conversion so a later call with the same JavaScript string receives a fresh copy of its UTF-8 bytes.
Assisted-by: codex:gpt-5.6-sol