Skip to content

Commit 72fb796

Browse files
Matt LoringFishrock123
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 341b3d0 commit 72fb796

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
@@ -804,14 +804,14 @@ void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
804804
size_t offset = args[2]->IntegerValue(env->context()).FromMaybe(0);
805805

806806
size_t memcpy_num = sizeof(T);
807-
if (offset + sizeof(T) > ts_obj_length)
808-
memcpy_num = ts_obj_length - offset;
809807

810808
if (should_assert) {
811809
CHECK_NOT_OOB(offset + memcpy_num >= memcpy_num);
812810
CHECK_NOT_OOB(offset + memcpy_num <= ts_obj_length);
813811
}
814-
CHECK_LE(offset + memcpy_num, ts_obj_length);
812+
813+
if (offset + memcpy_num > ts_obj_length)
814+
memcpy_num = ts_obj_length - offset;
815815

816816
union NoAlias {
817817
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
new 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)