Skip to content

Commit 8d8f4dd

Browse files
committed
src: check receiver when writing floating point
Verify that the receiver is a buffer object before attempting to write out a floating point value. Fixes: nodejs#8724
1 parent 1f8c353 commit 8d8f4dd

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/node_buffer.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,10 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
762762
template <typename T, enum Endianness endianness>
763763
void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
764764
Environment* env = Environment::GetCurrent(args);
765+
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
765766

766767
bool should_assert = args.Length() < 4;
767768

768-
if (should_assert) {
769-
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
770-
}
771-
772769
Local<ArrayBufferView> ts_obj = args[0].As<ArrayBufferView>();
773770
ArrayBuffer::Contents ts_obj_c = ts_obj->Buffer()->GetContents();
774771
const size_t ts_obj_offset = ts_obj->ByteOffset();

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

+23
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,26 @@ writePartial('writeFloatBE', [1, 6], 10,
100100
Buffer.from([0, 0, 0, 0, 0, 0, 63, 128, 0]));
101101
writePartial('writeFloatLE', [1, 6], 10,
102102
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 128]));
103+
104+
// https://github.com/nodejs/node/issues/8724 - specific to methods dealing
105+
// with floating point types because they call out to C++ code.
106+
for (const noAssert of [true, false]) {
107+
const re = /^TypeError: argument should be a Buffer$/;
108+
const proto = Buffer.prototype;
109+
const { writeFloatBE, writeFloatLE, writeDoubleBE, writeDoubleLE } = proto;
110+
// Non-method call.
111+
assert.throws(() => writeFloatBE(0, 0, noAssert), re);
112+
assert.throws(() => writeFloatLE(0, 0, noAssert), re);
113+
assert.throws(() => writeDoubleBE(0, 0, noAssert), re);
114+
assert.throws(() => writeDoubleLE(0, 0, noAssert), re);
115+
// Wrong receiver.
116+
assert.throws(() => proto.writeFloatBE(0, 0, noAssert), re);
117+
assert.throws(() => proto.writeFloatLE(0, 0, noAssert), re);
118+
assert.throws(() => proto.writeDoubleBE(0, 0, noAssert), re);
119+
assert.throws(() => proto.writeDoubleLE(0, 0, noAssert), re);
120+
// Wrong receiver.
121+
assert.throws(() => proto.writeFloatBE.call({}, 0, 0, noAssert), re);
122+
assert.throws(() => proto.writeFloatLE.call({}, 0, 0, noAssert), re);
123+
assert.throws(() => proto.writeDoubleBE.call({}, 0, 0, noAssert), re);
124+
assert.throws(() => proto.writeDoubleLE.call({}, 0, 0, noAssert), re);
125+
}

0 commit comments

Comments
 (0)