Skip to content

Commit 59f71f5

Browse files
TimothyGuMylesBorins
authored andcommitted
src, buffer: do not segfault on out-of-range index
Also add test cases for partial writes and invalid indices. PR-URL: #11927 Fixes: #8724 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 55a1126 commit 59f71f5

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

src/node_buffer.cc

+20-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ void CallbackInfo::WeakCallback(Isolate* isolate) {
148148
// Parse index for external array data.
149149
inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
150150
size_t def,
151-
size_t* ret) {
151+
size_t* ret,
152+
size_t needed = 0) {
152153
if (arg->IsUndefined()) {
153154
*ret = def;
154155
return true;
@@ -162,7 +163,7 @@ inline MUST_USE_RESULT bool ParseArrayIndex(Local<Value> arg,
162163
// Check that the result fits in a size_t.
163164
const uint64_t kSizeMax = static_cast<uint64_t>(static_cast<size_t>(-1));
164165
// coverity[pointless_expression]
165-
if (static_cast<uint64_t>(tmp_i) > kSizeMax)
166+
if (static_cast<uint64_t>(tmp_i) > kSizeMax - needed)
166167
return false;
167168

168169
*ret = static_cast<size_t>(tmp_i);
@@ -793,17 +794,28 @@ void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
793794
CHECK_NE(ts_obj_data, nullptr);
794795

795796
T val = args[1]->NumberValue(env->context()).FromMaybe(0);
796-
size_t offset = args[2]->IntegerValue(env->context()).FromMaybe(0);
797797

798798
size_t memcpy_num = sizeof(T);
799+
size_t offset;
799800

800-
if (should_assert) {
801-
THROW_AND_RETURN_IF_OOB(offset + memcpy_num >= memcpy_num);
802-
THROW_AND_RETURN_IF_OOB(offset + memcpy_num <= ts_obj_length);
801+
// If the offset is negative or larger than the size of the ArrayBuffer,
802+
// throw an error (if needed) and return directly.
803+
if (!ParseArrayIndex(args[2], 0, &offset, memcpy_num) ||
804+
offset >= ts_obj_length) {
805+
if (should_assert)
806+
THROW_AND_RETURN_IF_OOB(false);
807+
return;
803808
}
804809

805-
if (offset + memcpy_num > ts_obj_length)
806-
memcpy_num = ts_obj_length - offset;
810+
// If the offset is too large for the entire value, but small enough to fit
811+
// part of the value, throw an error and return only if should_assert is
812+
// true. Otherwise, write the part of the value that fits.
813+
if (offset + memcpy_num > ts_obj_length) {
814+
if (should_assert)
815+
THROW_AND_RETURN_IF_OOB(false);
816+
else
817+
memcpy_num = ts_obj_length - offset;
818+
}
807819

808820
union NoAlias {
809821
T val;

test/parallel/test-buffer-write-noassert.js

+55-8
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ const assert = require('assert');
44

55
// testing buffer write functions
66

7+
const outOfRange = /^RangeError: (?:Index )?out of range(?: index)?$/;
8+
79
function write(funx, args, result, res) {
810
{
911
const buf = Buffer.alloc(9);
1012
assert.strictEqual(buf[funx](...args), result);
1113
assert.deepStrictEqual(buf, res);
1214
}
1315

14-
{
15-
const invalidArgs = Array.from(args);
16-
invalidArgs[1] = -1;
17-
assert.throws(
18-
() => Buffer.alloc(9)[funx](...invalidArgs),
19-
/^RangeError: (?:Index )?out of range(?: index)?$/
20-
);
21-
}
16+
writeInvalidOffset(-1);
17+
writeInvalidOffset(9);
2218

2319
{
2420
const error = /Int/.test(funx) ?
@@ -37,6 +33,15 @@ function write(funx, args, result, res) {
3733
assert.deepStrictEqual(buf2, res);
3834
}
3935

36+
function writeInvalidOffset(offset) {
37+
const newArgs = Array.from(args);
38+
newArgs[1] = offset;
39+
assert.throws(() => Buffer.alloc(9)[funx](...newArgs), outOfRange);
40+
41+
const buf = Buffer.alloc(9);
42+
buf[funx](...newArgs, true);
43+
assert.deepStrictEqual(buf, Buffer.alloc(9));
44+
}
4045
}
4146

4247
write('writeInt8', [1, 0], 1, Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 0]));
@@ -57,3 +62,45 @@ write('writeDoubleBE', [1, 1], 9, Buffer.from([0, 63, 240, 0, 0, 0, 0, 0, 0]));
5762
write('writeDoubleLE', [1, 1], 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 240, 63]));
5863
write('writeFloatBE', [1, 1], 5, Buffer.from([0, 63, 128, 0, 0, 0, 0, 0, 0]));
5964
write('writeFloatLE', [1, 1], 5, Buffer.from([0, 0, 0, 128, 63, 0, 0, 0, 0]));
65+
66+
function writePartial(funx, args, result, res) {
67+
assert.throws(() => Buffer.alloc(9)[funx](...args), outOfRange);
68+
const buf = Buffer.alloc(9);
69+
assert.strictEqual(buf[funx](...args, true), result);
70+
assert.deepStrictEqual(buf, res);
71+
}
72+
73+
// Test partial writes (cases where the buffer isn't large enough to hold the
74+
// entire value, but is large enough to hold parts of it).
75+
writePartial('writeIntBE', [0x0eadbeef, 6, 4], 10,
76+
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
77+
writePartial('writeIntLE', [0x0eadbeef, 6, 4], 10,
78+
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
79+
writePartial('writeInt16BE', [0x1234, 8], 10,
80+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
81+
writePartial('writeInt16LE', [0x1234, 8], 10,
82+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
83+
writePartial('writeInt32BE', [0x0eadbeef, 6], 10,
84+
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
85+
writePartial('writeInt32LE', [0x0eadbeef, 6], 10,
86+
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
87+
writePartial('writeUIntBE', [0xdeadbeef, 6, 4], 10,
88+
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
89+
writePartial('writeUIntLE', [0xdeadbeef, 6, 4], 10,
90+
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
91+
writePartial('writeUInt16BE', [0x1234, 8], 10,
92+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
93+
writePartial('writeUInt16LE', [0x1234, 8], 10,
94+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
95+
writePartial('writeUInt32BE', [0xdeadbeef, 6], 10,
96+
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
97+
writePartial('writeUInt32LE', [0xdeadbeef, 6], 10,
98+
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
99+
writePartial('writeDoubleBE', [1, 2], 10,
100+
Buffer.from([0, 0, 63, 240, 0, 0, 0, 0, 0]));
101+
writePartial('writeDoubleLE', [1, 2], 10,
102+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 240]));
103+
writePartial('writeFloatBE', [1, 6], 10,
104+
Buffer.from([0, 0, 0, 0, 0, 0, 63, 128, 0]));
105+
writePartial('writeFloatLE', [1, 6], 10,
106+
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 128]));

0 commit comments

Comments
 (0)