Skip to content

Commit cea7828

Browse files
authored
Fixup Buffer bounds check (#4442)
1 parent d48dea1 commit cea7828

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/node/internal/internal_buffer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,12 @@ Buffer.prototype.inspect = function inspect(
675675
ctx: InspectOptionsStylized
676676
) {
677677
let str = '';
678-
const max = INSPECT_MAX_BYTES;
678+
const max = Math.min(this.byteLength, INSPECT_MAX_BYTES);
679+
679680
str = this.toString('hex', 0, max)
680681
.replace(/(.{2})/g, '$1 ')
681682
.trim();
682-
const remaining = this.length - max;
683+
const remaining = this.byteLength - max;
683684
if (remaining > 0) {
684685
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
685686
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ uint32_t writeInto(jsg::Lock& js,
7878
uint32_t offset,
7979
uint32_t length,
8080
Encoding encoding) {
81+
KJ_ASSERT(offset <= buffer.size());
82+
KJ_ASSERT(length <= buffer.size() - offset);
8183
auto dest = buffer.slice(offset, kj::min(offset + length, buffer.size()));
8284
if (dest.size() == 0 || string.length(js) == 0) {
8385
return 0;
@@ -198,9 +200,11 @@ int BufferUtil::compare(jsg::Lock& js,
198200
// The options allow comparing subranges within the two inputs.
199201
KJ_IF_SOME(options, maybeOptions) {
200202
auto end = options.aEnd.orDefault(ptrOne.size());
203+
end = kj::min(end, ptrOne.size());
201204
auto start = kj::min(end, options.aStart.orDefault(0));
202205
ptrOne = ptrOne.slice(start, end);
203206
end = options.bEnd.orDefault(ptrTwo.size());
207+
end = kj::min(end, ptrTwo.size());
204208
start = kj::min(end, options.bStart.orDefault(0));
205209
ptrTwo = ptrTwo.slice(start, end);
206210
}
@@ -411,6 +415,7 @@ jsg::Optional<uint32_t> indexOfString(jsg::Lock& js,
411415

412416
jsg::JsString toStringImpl(
413417
jsg::Lock& js, kj::ArrayPtr<kj::byte> bytes, uint32_t start, uint32_t end, Encoding encoding) {
418+
KJ_ASSERT(end <= bytes.size());
414419
if (end < start) end = start;
415420
auto slice = bytes.slice(start, end);
416421
if (slice.size() == 0) return js.str();
@@ -508,6 +513,8 @@ void BufferUtil::swap(jsg::Lock& js, jsg::BufferSource buffer, int size) {
508513

509514
jsg::JsString BufferUtil::toString(
510515
jsg::Lock& js, jsg::BufferSource bytes, uint32_t start, uint32_t end, EncodingValue encoding) {
516+
end = kj::min(bytes.size(), end);
517+
if (end <= start) return js.str();
511518
return toStringImpl(js, bytes, start, end, static_cast<Encoding>(encoding));
512519
}
513520

@@ -517,6 +524,8 @@ uint32_t BufferUtil::write(jsg::Lock& js,
517524
uint32_t offset,
518525
uint32_t length,
519526
EncodingValue encoding) {
527+
length = kj::min(length, buffer.size() - offset);
528+
if (length == 0) return 0;
520529
return writeInto(js, buffer, string, offset, length, static_cast<Encoding>(encoding));
521530
}
522531

0 commit comments

Comments
 (0)