Skip to content

Commit

Permalink
src: check receiver when writing floating point
Browse files Browse the repository at this point in the history
Verify that the receiver is a buffer object before attempting to write
out a floating point value.

Fixes: nodejs#8724
  • Loading branch information
bnoordhuis committed Aug 9, 2017
1 parent 1f8c353 commit 8d8f4dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,10 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
template <typename T, enum Endianness endianness>
void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);

bool should_assert = args.Length() < 4;

if (should_assert) {
THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
}

Local<ArrayBufferView> ts_obj = args[0].As<ArrayBufferView>();
ArrayBuffer::Contents ts_obj_c = ts_obj->Buffer()->GetContents();
const size_t ts_obj_offset = ts_obj->ByteOffset();
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-buffer-write-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,26 @@ writePartial('writeFloatBE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 63, 128, 0]));
writePartial('writeFloatLE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 128]));

// https://github.com/nodejs/node/issues/8724 - specific to methods dealing
// with floating point types because they call out to C++ code.
for (const noAssert of [true, false]) {
const re = /^TypeError: argument should be a Buffer$/;
const proto = Buffer.prototype;
const { writeFloatBE, writeFloatLE, writeDoubleBE, writeDoubleLE } = proto;
// Non-method call.
assert.throws(() => writeFloatBE(0, 0, noAssert), re);
assert.throws(() => writeFloatLE(0, 0, noAssert), re);
assert.throws(() => writeDoubleBE(0, 0, noAssert), re);
assert.throws(() => writeDoubleLE(0, 0, noAssert), re);
// Wrong receiver.
assert.throws(() => proto.writeFloatBE(0, 0, noAssert), re);
assert.throws(() => proto.writeFloatLE(0, 0, noAssert), re);
assert.throws(() => proto.writeDoubleBE(0, 0, noAssert), re);
assert.throws(() => proto.writeDoubleLE(0, 0, noAssert), re);
// Wrong receiver.
assert.throws(() => proto.writeFloatBE.call({}, 0, 0, noAssert), re);
assert.throws(() => proto.writeFloatLE.call({}, 0, 0, noAssert), re);
assert.throws(() => proto.writeDoubleBE.call({}, 0, 0, noAssert), re);
assert.throws(() => proto.writeDoubleLE.call({}, 0, 0, noAssert), re);
}

0 comments on commit 8d8f4dd

Please sign in to comment.