Skip to content

Commit 630096b

Browse files
committed
src: guard against overflow in ParseArrayIndex()
ParseArrayIndex() would wrap around large (>=2^32) index values on platforms where sizeof(int64_t) > sizeof(size_t). Ensure that the return value fits in a size_t. PR-URL: #7497 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6ae2043 commit 630096b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/node_buffer.cc

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
207207
if (tmp_i < 0)
208208
return false;
209209

210+
// Check that the result fits in a size_t.
211+
const uint64_t kSizeMax = static_cast<uint64_t>(static_cast<size_t>(-1));
212+
if (static_cast<uint64_t>(tmp_i) > kSizeMax)
213+
return false;
214+
210215
*ret = static_cast<size_t>(tmp_i);
211216
return true;
212217
}

test/parallel/test-buffer-alloc.js

+7
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,13 @@ assert.throws(function() {
14621462
Buffer.from(new ArrayBuffer(0), -1 >>> 0);
14631463
}, /RangeError: 'offset' is out of bounds/);
14641464

1465+
// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
1466+
assert.throws(() => {
1467+
const a = Buffer(1).fill(0);
1468+
const b = Buffer(1).fill(0);
1469+
a.copy(b, 0, 0x100000000, 0x100000001);
1470+
}), /out of range index/;
1471+
14651472
// Unpooled buffer (replaces SlowBuffer)
14661473
const ubuf = Buffer.allocUnsafeSlow(10);
14671474
assert(ubuf);

0 commit comments

Comments
 (0)