Skip to content

Commit b3fb4f3

Browse files
HarshithaKPtargos
authored andcommitted
src: handle NULL env scenario
Convert hard assertion into a throw with a useful error message in src/module_wrap.cc. PR-URL: nodejs#31899 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 507d1cd commit b3fb4f3

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,13 @@ provided.
879879
Encoding provided to `TextDecoder()` API was not one of the
880880
[WHATWG Supported Encodings][].
881881

882+
<a id="ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE"></a>
883+
### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE`
884+
885+
The JS execution context is not associated with a Node.js environment.
886+
This may occur when Node.js is used as an embedded library and some hooks
887+
for the JS engine are not set up properly.
888+
882889
<a id="ERR_FALSY_VALUE_REJECTION"></a>
883890
### `ERR_FALSY_VALUE_REJECTION`
884891

src/module_wrap.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,12 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
453453
Local<String> specifier,
454454
Local<Module> referrer) {
455455
Environment* env = Environment::GetCurrent(context);
456-
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here.
456+
if (env == nullptr) {
457+
Isolate* isolate = context->GetIsolate();
458+
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(isolate);
459+
return MaybeLocal<Module>();
460+
}
461+
457462
Isolate* isolate = env->isolate();
458463

459464
ModuleWrap* dependent = GetFromModule(env, referrer);
@@ -1445,7 +1450,11 @@ static MaybeLocal<Promise> ImportModuleDynamically(
14451450
Local<String> specifier) {
14461451
Isolate* iso = context->GetIsolate();
14471452
Environment* env = Environment::GetCurrent(context);
1448-
CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here.
1453+
if (env == nullptr) {
1454+
THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(iso);
1455+
return MaybeLocal<Promise>();
1456+
}
1457+
14491458
EscapableHandleScope handle_scope(iso);
14501459

14511460
Local<Function> import_callback =

src/node_errors.h

+25-22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void OnFatalError(const char* location, const char* message);
3939
V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \
4040
V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \
4141
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \
42+
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \
4243
V(ERR_INVALID_ARG_VALUE, TypeError) \
4344
V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \
4445
V(ERR_INVALID_ARG_TYPE, TypeError) \
@@ -86,28 +87,30 @@ void OnFatalError(const char* location, const char* message);
8687

8788
// Errors with predefined static messages
8889

89-
#define PREDEFINED_ERROR_MESSAGES(V) \
90-
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
91-
"Buffer is not available for the current Context") \
92-
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
93-
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
94-
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
95-
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
96-
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
97-
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
98-
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
99-
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \
100-
"MessagePort was found in message but not listed in transferList") \
101-
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
102-
"The V8 platform used by this instance of Node does not support " \
103-
"creating Workers") \
104-
V(ERR_NON_CONTEXT_AWARE_DISABLED, \
105-
"Loading non context-aware native modules has been disabled") \
106-
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
107-
"Script execution was interrupted by `SIGINT`") \
108-
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \
109-
"Cannot serialize externalized SharedArrayBuffer") \
110-
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \
90+
#define PREDEFINED_ERROR_MESSAGES(V) \
91+
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
92+
"Buffer is not available for the current Context") \
93+
V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \
94+
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
95+
V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \
96+
V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \
97+
V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \
98+
"Context not associated with Node.js environment") \
99+
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
100+
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
101+
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
102+
V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \
103+
"MessagePort was found in message but not listed in transferList") \
104+
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
105+
"The V8 platform used by this instance of Node does not support " \
106+
"creating Workers") \
107+
V(ERR_NON_CONTEXT_AWARE_DISABLED, \
108+
"Loading non context-aware native modules has been disabled") \
109+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
110+
"Script execution was interrupted by `SIGINT`") \
111+
V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \
112+
"Cannot serialize externalized SharedArrayBuffer") \
113+
V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint")
111114

112115
#define V(code, message) \
113116
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \

0 commit comments

Comments
 (0)