Skip to content

Commit b6a41d6

Browse files
addaleaxjasnell
authored andcommitted
src: fix out-of-bounds write in TwoByteValue
Plan 2 bytes instead of 1 byte for the final zero terminator for UTF-16. This is unlikely to cause real-world problems, but that ultimately depends on the `malloc` implementation. The issue can be uncovered by running e.g. `valgrind node -e "Buffer(65536).fill('a'.repeat(4096), 'utf16le')"` Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> PR-URL: #6330
1 parent aa53bf2 commit b6a41d6

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/util.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value)
4747
return;
4848

4949
// Allocate enough space to include the null terminator
50-
size_t len = StringBytes::StorageSize(isolate, string, UCS2) + 1;
50+
size_t len =
51+
StringBytes::StorageSize(isolate, string, UCS2) +
52+
sizeof(uint16_t);
5153
if (len > sizeof(str_st_)) {
5254
str_ = static_cast<uint16_t*>(malloc(len));
5355
CHECK_NE(str_, nullptr);

0 commit comments

Comments
 (0)