Skip to content

Commit 0ed3a7c

Browse files
pmq20trevnorris
authored andcommitted
buffer: let WriteFloatGeneric silently drop values
Documentation currently states that setting noAssert and passing a value larger than can fit in the Buffer will cause data to be silently dropped. Change implementation to match documented behavior. Fixes: #3766 Reviewed-By: Trevor Norris <[email protected]>
1 parent df268f9 commit 0ed3a7c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/node_buffer.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,9 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
735735

736736
T val = args[1]->NumberValue();
737737
uint32_t offset = args[2]->Uint32Value();
738-
CHECK_LE(offset + sizeof(T), ts_obj_length);
738+
size_t memcpy_num = sizeof(T);
739+
if (offset + sizeof(T) > ts_obj_length)
740+
memcpy_num = ts_obj_length - offset;
739741

740742
union NoAlias {
741743
T val;
@@ -746,8 +748,8 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
746748
char* ptr = static_cast<char*>(ts_obj_data) + offset;
747749
if (endianness != GetEndianness())
748750
Swizzle(na.bytes, sizeof(na.bytes));
749-
memcpy(ptr, na.bytes, sizeof(na.bytes));
750-
return offset + sizeof(na.bytes);
751+
memcpy(ptr, na.bytes, memcpy_num);
752+
return offset + memcpy_num;
751753
}
752754

753755

test/parallel/test-buffer-arraybuffer.js

+7
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ assert.throws(function() {
4444
AB.prototype.__proto__ = ArrayBuffer.prototype;
4545
new Buffer(new AB());
4646
}, TypeError);
47+
48+
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
49+
var b = new Buffer(1);
50+
b.writeFloatLE(11.11, 0, true);
51+
b.writeFloatBE(11.11, 0, true);
52+
b.writeDoubleLE(11.11, 0, true);
53+
b.writeDoubleBE(11.11, 0, true);

0 commit comments

Comments
 (0)