Skip to content

Commit 1c12105

Browse files
committed
Fix minor UBSan violation in buffer-nodejs-test
This allows us to expand downstream UBSan coverage.
1 parent 9475856 commit 1c12105

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/workerd/api/node/buffer.c++

+9
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,18 @@ uint32_t writeInto(jsg::Lock& js,
9898
return result.written;
9999
}
100100
case Encoding::UTF16LE: {
101+
#if __has_feature(undefined_behavior_sanitizer)
102+
// UBSan warns about unaligned writes, but this can be hard to avoid if dest is unaligned.
103+
// Use temp variable to perform aligned write instead.
104+
kj::Array<uint16_t> tmpBuf = kj::heapArray<uint16_t>(dest.size() / sizeof(uint16_t));
105+
auto result = string.writeInto(js, tmpBuf, flags);
106+
kj::ArrayPtr<uint16_t> buf(reinterpret_cast<uint16_t*>(dest.begin()), result.written);
107+
buf.copyFrom(tmp);
108+
#else
101109
kj::ArrayPtr<uint16_t> buf(
102110
reinterpret_cast<uint16_t*>(dest.begin()), dest.size() / sizeof(uint16_t));
103111
auto result = string.writeInto(js, buf, flags);
112+
#endif
104113
return result.written * sizeof(uint16_t);
105114
}
106115
case Encoding::BASE64:

0 commit comments

Comments
 (0)