Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/node_union_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class UnionBytes {
UnionBytes& operator=(UnionBytes&&) = default;

bool is_one_byte() const { return one_byte_resource_ != nullptr; }
size_t length() const {
return is_one_byte() ? one_byte_resource_->length()
: two_byte_resource_->length();
}

v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const;

Expand Down
6 changes: 6 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,12 @@ void SetConstructorFunction(Isolate* isolate,
}

Local<String> UnionBytes::ToStringChecked(Isolate* isolate) const {
if (length() == 0) [[unlikely]] {
// V8 requires non-null data pointers for empty external strings,
// but we don't guarantee that. Solve this by not creating an
// external string at all in that case.
return String::Empty(isolate);
}
if (is_one_byte()) {
return String::NewExternalOneByte(isolate, one_byte_resource_)
.ToLocalChecked();
Expand Down
14 changes: 14 additions & 0 deletions test/cctest/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "node_options-inl.h"
#include "node_test_fixture.h"
#include "node_union_bytes.h"
#include "simdutf.h"
#include "util-inl.h"
#include "v8-function-callback.h"
Expand Down Expand Up @@ -312,6 +313,19 @@ TEST_F(UtilTest, DumpJavaScriptStackWithNoIsolate) {
node::DumpJavaScriptBacktrace(stderr);
}

TEST_F(UtilTest, EmptyUnionBytesToString) {
const v8::HandleScope handle_scope(isolate_);
node::StaticExternalOneByteResource one_byte_resource(nullptr, 0, nullptr);
node::StaticExternalTwoByteResource two_byte_resource(nullptr, 0, nullptr);

EXPECT_EQ(
0,
node::UnionBytes(&one_byte_resource).ToStringChecked(isolate_)->Length());
EXPECT_EQ(
0,
node::UnionBytes(&two_byte_resource).ToStringChecked(isolate_)->Length());
}

TEST_F(UtilTest, DetermineSpecificErrorType) {
const v8::HandleScope handle_scope(isolate_);
Argv argv;
Expand Down
Loading