Skip to content

Commit 8be5c43

Browse files
committed
buffer: validate UTF8 on fast path
Fast API handles invalid UTF differently than the slow API. Fixes: #54521 PR-URL: #54525
1 parent d5dc540 commit 8be5c43

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/node_buffer.cc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,33 @@ uint32_t FastWriteString(Local<Value> receiver,
14891489

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

1492+
uint32_t FastWriteStringUTF8(Local<Value> receiver,
1493+
const v8::FastApiTypedArray<uint8_t>& dst,
1494+
const v8::FastOneByteString& src,
1495+
uint32_t offset,
1496+
uint32_t max_length,
1497+
v8::FastApiCallbackOptions& options) {
1498+
uint8_t* dst_data;
1499+
CHECK(dst.getStorageIfAligned(&dst_data));
1500+
CHECK(offset <= dst.length());
1501+
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());
1502+
1503+
const auto size = std::min(
1504+
{static_cast<uint32_t>(dst.length() - offset), max_length, src.length});
1505+
1506+
if (!simdutf::validate_utf8(src.data, size)) {
1507+
options.fallback = true;
1508+
return 0;
1509+
}
1510+
1511+
memcpy(dst_data + offset, src.data, size);
1512+
1513+
return size;
1514+
}
1515+
1516+
static v8::CFunction fast_write_string_utf8(
1517+
v8::CFunction::Make(FastWriteStringUTF8));
1518+
14921519
void Initialize(Local<Object> target,
14931520
Local<Value> unused,
14941521
Local<Context> context,
@@ -1568,7 +1595,7 @@ void Initialize(Local<Object> target,
15681595
target,
15691596
"utf8WriteStatic",
15701597
SlowWriteString<UTF8>,
1571-
&fast_write_string);
1598+
&fast_write_string_utf8);
15721599

15731600
SetMethod(context, target, "getZeroFillToggle", GetZeroFillToggle);
15741601
}
@@ -1615,6 +1642,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
16151642
registry->Register(SlowWriteString<UTF8>);
16161643
registry->Register(fast_write_string.GetTypeInfo());
16171644
registry->Register(FastWriteString);
1645+
registry->Register(fast_write_string_utf8.GetTypeInfo());
1646+
registry->Register(FastWriteStringUTF8);
16181647
registry->Register(StringWrite<ASCII>);
16191648
registry->Register(StringWrite<BASE64>);
16201649
registry->Register(StringWrite<BASE64URL>);

src/node_external_reference.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ using CFunctionWriteString =
6363
uint32_t offset,
6464
uint32_t max_length);
6565

66+
using CFunctionWriteStringFallback =
67+
uint32_t (*)(v8::Local<v8::Value> receiver,
68+
const v8::FastApiTypedArray<uint8_t>& dst,
69+
const v8::FastOneByteString& src,
70+
uint32_t offset,
71+
uint32_t max_length,
72+
v8::FastApiCallbackOptions& options);
73+
6674
using CFunctionBufferCopy =
6775
uint32_t (*)(v8::Local<v8::Value> receiver,
6876
const v8::FastApiTypedArray<uint8_t>& source,
@@ -96,6 +104,7 @@ class ExternalReferenceRegistry {
96104
V(CFunctionWithBool) \
97105
V(CFunctionBufferCopy) \
98106
V(CFunctionWriteString) \
107+
V(CFunctionWriteStringFallback) \
99108
V(const v8::CFunctionInfo*) \
100109
V(v8::FunctionCallback) \
101110
V(v8::AccessorNameGetterCallback) \

test/parallel/test-buffer-write.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,17 @@ assert.strictEqual(Buffer.alloc(4)
106106
assert.strictEqual(buf.write('ыы', 1, 'utf16le'), 4);
107107
assert.deepStrictEqual([...buf], [0, 0x4b, 0x04, 0x4b, 0x04, 0, 0, 0]);
108108
}
109+
110+
{
111+
let i = 0;
112+
113+
while (i < 1_000_000) {
114+
const buf = Buffer.from("\x80")
115+
116+
if (buf[0] !== 194 || buf[1] !== 128) {
117+
assert(false);
118+
}
119+
120+
i++;
121+
}
122+
}

0 commit comments

Comments
 (0)