Skip to content

Commit

Permalink
buffer: validate UTF8 on fast path
Browse files Browse the repository at this point in the history
Fast API handles invalid UTF differently than the slow API.

Fixes: #54521
PR-URL: #54525
  • Loading branch information
ronag committed Aug 23, 2024
1 parent d5dc540 commit 0fda693
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,32 @@ uint32_t FastWriteString(Local<Value> receiver,

static v8::CFunction fast_write_string(v8::CFunction::Make(FastWriteString));

uint32_t FastWriteStringUTF8(Local<Value> receiver,
const v8::FastApiTypedArray<uint8_t>& dst,
const v8::FastOneByteString& src,
uint32_t offset,
uint32_t max_length,
v8::FastApiCallbackOptions& options) {
uint8_t* dst_data;
CHECK(dst.getStorageIfAligned(&dst_data));
CHECK(offset <= dst.length());
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());

const auto size = std::min(
{static_cast<uint32_t>(dst.length() - offset), max_length, src.length});

if (!simdutf::validate_utf8(src.data, size)) {
options.fallback = true;
return 0;
}

memcpy(dst_data + offset, src.data, size);

return size;
}

static v8::CFunction fast_write_string_utf8(v8::CFunction::Make(FastWriteStringUTF8));

void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -1568,7 +1594,7 @@ void Initialize(Local<Object> target,
target,
"utf8WriteStatic",
SlowWriteString<UTF8>,
&fast_write_string);
&fast_write_string_utf8);

SetMethod(context, target, "getZeroFillToggle", GetZeroFillToggle);
}
Expand Down Expand Up @@ -1615,6 +1641,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SlowWriteString<UTF8>);
registry->Register(fast_write_string.GetTypeInfo());
registry->Register(FastWriteString);
registry->Register(fast_write_string_utf8.GetTypeInfo());
registry->Register(FastWriteStringUTF8);
registry->Register(StringWrite<ASCII>);
registry->Register(StringWrite<BASE64>);
registry->Register(StringWrite<BASE64URL>);
Expand Down
9 changes: 9 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ using CFunctionWriteString =
uint32_t offset,
uint32_t max_length);

using CFunctionWriteStringFallback =
uint32_t (*)(v8::Local<v8::Value> receiver,
const v8::FastApiTypedArray<uint8_t>& dst,
const v8::FastOneByteString& src,
uint32_t offset,
uint32_t max_length,
v8::FastApiCallbackOptions& options);

using CFunctionBufferCopy =
uint32_t (*)(v8::Local<v8::Value> receiver,
const v8::FastApiTypedArray<uint8_t>& source,
Expand Down Expand Up @@ -96,6 +104,7 @@ class ExternalReferenceRegistry {
V(CFunctionWithBool) \
V(CFunctionBufferCopy) \
V(CFunctionWriteString) \
V(CFunctionWriteStringFallback) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorNameGetterCallback) \
Expand Down

0 comments on commit 0fda693

Please sign in to comment.