src: fix reading empty string views in Blob[De]serializer

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 <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Joyee Cheung authored and marco-ippolito committed 2024-05-02 11:19:19 +02:00
1 parent e216192390
commit 07322b490f
2 files changed
+12 -1

No files matched your search

+3 -1
View File
@@ -553,7 +553,9 @@ MaybeLocal<Value> LoadEnvironment(Environment* env,
MaybeLocal<Value> 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<Value> {
+9
View File
@@ -139,6 +139,11 @@ std::string_view BlobDeserializer<Impl>::ReadStringView(StringLogMode mode) {
size_t length = ReadArithmetic<size_t>();
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<Impl>::WriteStringView(std::string_view data,
size_t written_total = WriteArithmetic<size_t>(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;