Skip to content

ffi reuses mutated temporary string buffer without restoring its contents #65050

Description

@trivikr

Version

main

Platform

macOS 26.6.0

Subsystem

ffi

What steps will reproduce the bug?

repro.c

#include <stddef.h>
#include <string.h>

char* overwrite(char* string, int byte, size_t count) {
  return memset(string, byte, count);
}

repro.js

import { DynamicLibrary, suffix, toString } from 'node:ffi';

const lib = new DynamicLibrary(`./repro.${suffix}`);
const overwrite = lib.getFunction('overwrite', {
  arguments: ['string', 'i32', 'u64'],
  return: 'pointer',
});

console.log(toString(overwrite('hello', 0x79, 1n))); // "yello"
console.log(toString(overwrite('hello', 0, 0n))); // "yello"; expected "hello"

lib.close();

Run commands

$ cc -dynamiclib -o repro.dylib repro.c
$ node --experimental-ffi repro.js

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

yello
hello

each call should populate temporary storage from the supplied JavaScript string, so reusing "hello" should produce "hello".

What do you see instead?

yello
yello

the second call receives "yello" because the cached temporary buffer retains the native mutation from the first call.

Additional information

No response

Metadata

Metadata

Assignees

Labels

ffiIssues and PRs related to experimental Foreign Function Interface support.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions