Skip to content

Commit d3c0d1b

Browse files
Matt Loringjasnell
Matt Loring
authored andcommitted
buffer: throw range error before truncating write
The check to determine whether `noAssert` was set to true and thus whether RangeErrors should be thrown was happening after the write was truncated to the available size of the buffer. These checks now occur in the correct order. Fixes: #5587 PR-URL: #5605 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent cde81b6 commit d3c0d1b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/node_buffer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,14 @@ void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
814814
size_t offset = args[2]->IntegerValue(env->context()).FromMaybe(0);
815815

816816
size_t memcpy_num = sizeof(T);
817-
if (offset + sizeof(T) > ts_obj_length)
818-
memcpy_num = ts_obj_length - offset;
819817

820818
if (should_assert) {
821819
CHECK_NOT_OOB(offset + memcpy_num >= memcpy_num);
822820
CHECK_NOT_OOB(offset + memcpy_num <= ts_obj_length);
823821
}
824-
CHECK_LE(offset + memcpy_num, ts_obj_length);
822+
823+
if (offset + memcpy_num > ts_obj_length)
824+
memcpy_num = ts_obj_length - offset;
825825

826826
union NoAlias {
827827
T val;

test/parallel/test-buffer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,16 @@ assert.throws(function() {
10381038
Buffer(0xFFFFFFFFF);
10391039
}, RangeError);
10401040

1041+
// issue GH-5587
1042+
assert.throws(function() {
1043+
var buf = new Buffer(8);
1044+
buf.writeFloatLE(0, 5);
1045+
}, RangeError);
1046+
assert.throws(function() {
1047+
var buf = new Buffer(16);
1048+
buf.writeDoubleLE(0, 9);
1049+
}, RangeError);
1050+
10411051

10421052
// attempt to overflow buffers, similar to previous bug in array buffers
10431053
assert.throws(function() {

0 commit comments

Comments
 (0)