diff --git a/src/ffi/data.cc b/src/ffi/data.cc index 308bde60cae..5478ea5a281 100644 --- a/src/ffi/data.cc +++ b/src/ffi/data.cc @@ -163,8 +163,7 @@ Maybe ValidateStringLength(Environment* env, size_t len) { Maybe> GetValidatedPointerAndOffset( Environment* env, const FunctionCallbackInfo& args) { uintptr_t raw_ptr; - if (args.Length() < 1 || - !GetValidatedPointerAddress(env, args[0], "pointer").To(&raw_ptr)) { + if (!GetValidatedPointerAddress(env, args[0], "pointer").To(&raw_ptr)) { return {}; } @@ -204,8 +203,7 @@ Maybe GetValidatedPointerOffsetAndValue( size_t offset; Local value; uintptr_t raw_ptr; - if (args.Length() < 1 || - !GetValidatedPointerAddress(env, args[0], "pointer").To(&raw_ptr)) { + if (!GetValidatedPointerAddress(env, args[0], "pointer").To(&raw_ptr)) { return {}; } @@ -556,7 +554,7 @@ void ToBuffer(const FunctionCallbackInfo& args) { } size_t len; - if (args.Length() < 2 || !GetValidatedSize(env, args[1], "length").To(&len)) { + if (!GetValidatedSize(env, args[1], "length").To(&len)) { return; } @@ -618,7 +616,7 @@ void ToArrayBuffer(const FunctionCallbackInfo& args) { } size_t len; - if (args.Length() < 2 || !GetValidatedSize(env, args[1], "length").To(&len)) { + if (!GetValidatedSize(env, args[1], "length").To(&len)) { return; } @@ -694,13 +692,12 @@ void ExportBytes(const FunctionCallbackInfo& args) { } uintptr_t ptr; - if (args.Length() < 2 || - !GetValidatedPointerAddress(env, args[1], "pointer").To(&ptr)) { + if (!GetValidatedPointerAddress(env, args[1], "pointer").To(&ptr)) { return; } size_t len; - if (args.Length() < 3 || !GetValidatedSize(env, args[2], "length").To(&len)) { + if (!GetValidatedSize(env, args[2], "length").To(&len)) { return; } diff --git a/test/ffi/test-ffi-memory.js b/test/ffi/test-ffi-memory.js index 72d37efacd5..951ef71986b 100644 --- a/test/ffi/test-ffi-memory.js +++ b/test/ffi/test-ffi-memory.js @@ -319,3 +319,23 @@ test('ffi validates memory access arguments', () => { } })); }); + +test('ffi memory helpers reject missing required arguments', () => { + const widths = ['Int8', 'Uint8', 'Int16', 'Uint16', 'Int32', 'Uint32', + 'Int64', 'Uint64', 'Float32', 'Float64']; + + // Calling a helper with no arguments must report the missing pointer the + // same way an explicitly passed `undefined` does, instead of returning + // `undefined` as if the read or the write had succeeded. + for (const width of widths) { + for (const name of [`get${width}`, `set${width}`]) { + assert.throws(() => ffi[name](), /The pointer must be a bigint/); + assert.throws(() => ffi[name](undefined), /The pointer must be a bigint/); + } + } + + assert.throws(() => ffi.toBuffer(1n), /The length must be a number/); + assert.throws(() => ffi.toBuffer(1n, undefined), /The length must be a number/); + assert.throws(() => ffi.toArrayBuffer(1n), /The length must be a number/); + assert.throws(() => ffi.toArrayBuffer(1n, undefined), /The length must be a number/); +});