diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 1adbb7dda373f3..0459593162b942 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -1683,8 +1683,16 @@ static uint32_t FastCRC32(v8::Local receiver, uint32_t value, // NOLINTNEXTLINE(runtime/references) v8::FastApiCallbackOptions& options) { - TRACK_V8_FAST_API_CALL("zlib.crc32"); v8::HandleScope handle_scope(options.isolate); + if (!data->IsArrayBufferView() && !data->IsString()) { + TRACK_V8_FAST_API_CALL("zlib.crc32.error"); + THROW_ERR_INVALID_ARG_TYPE( + options.isolate, + "The \"data\" argument must be of type string or an instance of " + "Buffer, TypedArray, or DataView."); + return 0; + } + TRACK_V8_FAST_API_CALL("zlib.crc32"); return CRC32Impl(options.isolate, data, value); } diff --git a/test/sequential/test-zlib-crc32-fast-api.js b/test/sequential/test-zlib-crc32-fast-api.js index 37d5682a7eadfd..9b31a589f0bc49 100644 --- a/test/sequential/test-zlib-crc32-fast-api.js +++ b/test/sequential/test-zlib-crc32-fast-api.js @@ -18,9 +18,21 @@ const zlib = require('zlib'); testFastPath(); testFastPath(); + // Test that invalid input types throw after optimization + assert.throws(() => zlib.crc32(123), { + code: 'ERR_INVALID_ARG_TYPE', + }); + assert.throws(() => zlib.crc32(null), { + code: 'ERR_INVALID_ARG_TYPE', + }); + assert.throws(() => zlib.crc32({}), { + code: 'ERR_INVALID_ARG_TYPE', + }); + if (common.isDebug) { const { internalBinding } = require('internal/test/binding'); const { getV8FastApiCallCount } = internalBinding('debug'); assert.strictEqual(getV8FastApiCallCount('zlib.crc32'), 2); + assert.strictEqual(getV8FastApiCallCount('zlib.crc32.error'), 3); } }