From 98f509714787e77a1400290a889d4561190b9a8f Mon Sep 17 00:00:00 2001 From: Jan Olaf Krems Date: Mon, 3 Jul 2023 04:44:50 -0700 Subject: [PATCH] src: fix uninitialized field access in AsyncHooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joyee Cheung PR-URL: https://github.com/nodejs/node/pull/48566 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno Reviewed-By: Luigi Pinca Reviewed-By: Joyee Cheung Reviewed-By: Minwoo Jung Reviewed-By: Gerhard Stöbich --- src/env.cc | 12 +++++------- src/env.h | 7 ++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/env.cc b/src/env.cc index a62b8ef48b9d85..56f4344d9e1b5d 100644 --- a/src/env.cc +++ b/src/env.cc @@ -170,15 +170,13 @@ bool AsyncHooks::pop_async_context(double async_id) { } void AsyncHooks::clear_async_id_stack() { - if (env()->can_call_into_js()) { + if (!js_execution_async_resources_.IsEmpty() && env()->can_call_into_js()) { Isolate* isolate = env()->isolate(); HandleScope handle_scope(isolate); - if (!js_execution_async_resources_.IsEmpty()) { - USE(PersistentToLocal::Strong(js_execution_async_resources_) - ->Set(env()->context(), - env()->length_string(), - Integer::NewFromUnsigned(isolate, 0))); - } + USE(PersistentToLocal::Strong(js_execution_async_resources_) + ->Set(env()->context(), + env()->length_string(), + Integer::NewFromUnsigned(isolate, 0))); } native_execution_async_resources_.clear(); diff --git a/src/env.h b/src/env.h index dc9ba3baeb2b4b..231ca64db38e90 100644 --- a/src/env.h +++ b/src/env.h @@ -1015,6 +1015,10 @@ class Environment : public MemoryRetainer { uv_async_t task_queues_async_; int64_t task_queues_async_refs_ = 0; + // These may be read by ctors and should be listed before complex fields. + std::atomic_bool is_stopping_{false}; + std::atomic_bool can_call_into_js_{true}; + AsyncHooks async_hooks_; ImmediateInfo immediate_info_; AliasedInt32Array timeout_info_; @@ -1092,7 +1096,6 @@ class Environment : public MemoryRetainer { bool has_serialized_options_ = false; - std::atomic_bool can_call_into_js_ { true }; uint64_t flags_; uint64_t thread_id_; std::unordered_set sub_worker_contexts_; @@ -1150,8 +1153,6 @@ class Environment : public MemoryRetainer { CleanupQueue cleanup_queue_; bool started_cleanup_ = false; - std::atomic_bool is_stopping_ { false }; - std::unordered_set unmanaged_fds_; std::function process_exit_handler_{