From 1f193165b990190074faab34f503683148816d39 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 10 Mar 2024 11:40:24 +0100 Subject: [PATCH] src: fix reading empty string views in Blob[De]serializer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The string writing/reading was intended for debugging info in snapshot, which had a CHECK_GT(length, 0) check, it then got repurposed for SEA resource writing/reading and turned into a helper for string views, but was not updated to handle empty views, causing occasional crash in the CI when the read is protected. This patch fixes it. PR-URL: https://github.com/nodejs/node/pull/52000 Fixes: https://github.com/nodejs/node/issues/50740 Reviewed-By: Michaƫl Zasso Reviewed-By: Anna Henningsen Reviewed-By: Luigi Pinca --- src/api/environment.cc | 4 +++- src/blob_serializer_deserializer-inl.h | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index 95e76a8adafec9..cdc2f7aaa8efd8 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -553,7 +553,9 @@ MaybeLocal LoadEnvironment(Environment* env, MaybeLocal LoadEnvironment(Environment* env, std::string_view main_script_source_utf8, EmbedderPreloadCallback preload) { - CHECK_NOT_NULL(main_script_source_utf8.data()); + // It could be empty when it's used by SEA to load an empty script. + CHECK_IMPLIES(main_script_source_utf8.size() > 0, + main_script_source_utf8.data()); return LoadEnvironment( env, [&](const StartExecutionCallbackInfo& info) -> MaybeLocal { diff --git a/src/blob_serializer_deserializer-inl.h b/src/blob_serializer_deserializer-inl.h index 1d1204a96e6101..f47a1e0cdf8a44 100644 --- a/src/blob_serializer_deserializer-inl.h +++ b/src/blob_serializer_deserializer-inl.h @@ -139,6 +139,11 @@ std::string_view BlobDeserializer::ReadStringView(StringLogMode mode) { size_t length = ReadArithmetic(); Debug("ReadStringView(), length=%zu: ", length); + if (length == 0) { + Debug("ReadStringView() read an empty view\n"); + return std::string_view(); + } + std::string_view result(sink.data() + read_total, length); Debug("%p, read %zu bytes", result.data(), result.size()); if (mode == StringLogMode::kAddressAndContent) { @@ -269,6 +274,10 @@ size_t BlobSerializer::WriteStringView(std::string_view data, size_t written_total = WriteArithmetic(data.size()); size_t length = data.size(); + if (length == 0) { + Debug("WriteStringView() wrote an empty view\n"); + return written_total; + } sink.insert(sink.end(), data.data(), data.data() + length); written_total += length;