Skip to content

Commit 883c6a6

Browse files
trivikraduh95
authored andcommitted
ffi: reject arguments after callback in registerCallback
Passing the signature after the callback silently created a `void ()` callback, so native callers got no arguments forwarded and an undefined return value. Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Assisted-by: claude:opus-5.5 PR-URL: #66997 Fixes: #66996 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 1f63811 commit 883c6a6

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

‎src/node_ffi.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
10831083
}
10841084

10851085
if (args[0]->IsFunction()) {
1086+
if (args.Length() > 1) {
1087+
THROW_ERR_INVALID_ARG_TYPE(env, "Callback must be the last argument");
1088+
return;
1089+
}
10861090
fn = args[0].As<Function>();
10871091
} else {
10881092
if (!args[0]->IsObject() || args[0]->IsArray()) {

‎test/ffi/test-ffi-callbacks.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ test('ffi callback ref and unref APIs work', () => {
6363
return 0;
6464
},
6565
);
66+
assert.throws(() => lib.registerCallback(() => {}, { arguments: ['i32'], return: 'i32' }), {
67+
code: 'ERR_INVALID_ARG_TYPE',
68+
message: 'Callback must be the last argument',
69+
});
6670

6771
try {
6872
lib.unrefCallback(voidCallback);

0 commit comments

Comments
 (0)