Skip to content

Commit

Permalink
src: fix reading empty string views in Blob[De]serializer
Browse files Browse the repository at this point in the history
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: nodejs#52000
Fixes: nodejs#50740
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
joyeecheung authored Mar 10, 2024
1 parent 575ced8 commit 1f19316
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
9 changes: 9 additions & 0 deletions src/blob_serializer_deserializer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 1f19316

Please sign in to comment.