Skip to content

Commit 6ba3ad5

Browse files
bnoordhuisMylesBorins
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 e1f961d commit 6ba3ad5

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
@@ -185,6 +185,11 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
185185
if (tmp_i < 0)
186186
return false;
187187

188+
// Check that the result fits in a size_t.
189+
const uint64_t kSizeMax = static_cast<uint64_t>(static_cast<size_t>(-1));
190+
if (static_cast<uint64_t>(tmp_i) > kSizeMax)
191+
return false;
192+
188193
*ret = static_cast<size_t>(tmp_i);
189194
return true;
190195
}

test/parallel/test-buffer-alloc.js

+7
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,13 @@ assert.equal(Buffer.prototype.offset, undefined);
14441444
assert.equal(SlowBuffer.prototype.parent, undefined);
14451445
assert.equal(SlowBuffer.prototype.offset, undefined);
14461446

1447+
// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
1448+
assert.throws(() => {
1449+
const a = Buffer(1).fill(0);
1450+
const b = Buffer(1).fill(0);
1451+
a.copy(b, 0, 0x100000000, 0x100000001);
1452+
}), /out of range index/;
1453+
14471454
// Unpooled buffer (replaces SlowBuffer)
14481455
const ubuf = Buffer.allocUnsafeSlow(10);
14491456
assert(ubuf);

0 commit comments

Comments
 (0)