From 71ba7c0a6c57c71ffe42adf4e37952e8e9348556 Mon Sep 17 00:00:00 2001 From: VirajMishra1 Date: Thu, 3 Sep 2026 09:58:18 -0400 Subject: [PATCH] inspector: fix ctype UB and stoi overflow in session id parsing - cast the isdigit argument to unsigned char, since passing a negative char is undefined behavior - replace stoi with from_chars, since stoi throws on overflow and the build uses -fno-exceptions - cast to unsigned int before the left shift, since shifting a large signed value is undefined behavior Signed-off-by: VirajMishra1 --- src/inspector_io.cc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/inspector_io.cc b/src/inspector_io.cc index e0b4b6c37461..8bf33814a62a 100644 --- a/src/inspector_io.cc +++ b/src/inspector_io.cc @@ -17,8 +17,9 @@ #include "v8-inspector.h" #include "zlib.h" -#include +#include #include +#include #include namespace node { @@ -378,12 +379,16 @@ void InspectorIoDelegate::MessageReceived(int session_id, if (target_session_id_str) { bool is_number = std::all_of(target_session_id_str->begin(), target_session_id_str->end(), - ::isdigit); + [](unsigned char c) { return ::isdigit(c); }); if (is_number) { - int target_session_id = std::stoi(*target_session_id_str); + int target_session_id = 0; + const auto& s = *target_session_id_str; + auto [ptr, ec] = + std::from_chars(s.data(), s.data() + s.size(), target_session_id); + if (ec != std::errc{}) return; worker = TargetManager::WorkerForSession(target_session_id); if (worker) { - merged_session_id += target_session_id << 16; + merged_session_id += static_cast(target_session_id) << 16; } } }