From 4dfed556baf03117859143aee6ea23525313e82b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 17 Sep 2024 11:48:56 +0200 Subject: [PATCH] module: throw when invalid argument is passed to enableCompileCache() PR-URL: https://github.com/nodejs/node/pull/54971 Fixes: https://github.com/nodejs/node/issues/54770 Fixes: https://github.com/nodejs/node/issues/54465 Reviewed-By: Yagiz Nizipli Reviewed-By: Matteo Collina --- src/node_modules.cc | 5 ++++- test/parallel/test-compile-cache-api-error.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-compile-cache-api-error.js diff --git a/src/node_modules.cc b/src/node_modules.cc index 3bedd2dfecb..721b5e1e4b4 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -436,10 +436,13 @@ void BindingData::GetPackageScopeConfig( } void EnableCompileCache(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsString()); Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); Environment* env = Environment::GetCurrent(context); + if (!args[0]->IsString()) { + THROW_ERR_INVALID_ARG_TYPE(env, "cacheDir should be a string"); + return; + } Utf8Value value(isolate, args[0]); CompileCacheEnableResult result = env->EnableCompileCache(*value); std::vector> values = { diff --git a/test/parallel/test-compile-cache-api-error.js b/test/parallel/test-compile-cache-api-error.js new file mode 100644 index 00000000000..580c8f756a0 --- /dev/null +++ b/test/parallel/test-compile-cache-api-error.js @@ -0,0 +1,11 @@ +'use strict'; + +// This tests module.enableCompileCache() throws when an invalid argument is passed. + +require('../common'); +const { enableCompileCache } = require('module'); +const assert = require('assert'); + +for (const invalid of [0, null, false, () => {}, {}, []]) { + assert.throws(() => enableCompileCache(invalid), { code: 'ERR_INVALID_ARG_TYPE' }); +}