Skip to content

Commit f3e5b39

Browse files
bnoordhuiscjihrig
authored andcommitted
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 c730a5d commit f3e5b39

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/node_buffer.cc

Lines changed: 5 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,13 @@ assert.throws(function() {
14541454
Buffer.from(new ArrayBuffer(0), -1 >>> 0);
14551455
}, /RangeError: 'offset' is out of bounds/);
14561456

1457+
// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
1458+
assert.throws(() => {
1459+
const a = Buffer(1).fill(0);
1460+
const b = Buffer(1).fill(0);
1461+
a.copy(b, 0, 0x100000000, 0x100000001);
1462+
}), /out of range index/;
1463+
14571464
// Unpooled buffer (replaces SlowBuffer)
14581465
const ubuf = Buffer.allocUnsafeSlow(10);
14591466
assert(ubuf);

0 commit comments

Comments
 (0)