Skip to content

Commit 16ebaf6

Browse files
pmq20rvagg
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 1889593 commit 16ebaf6

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
@@ -733,7 +733,9 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
733733

734734
T val = args[1]->NumberValue();
735735
uint32_t offset = args[2]->Uint32Value();
736-
CHECK_LE(offset + sizeof(T), ts_obj_length);
736+
size_t memcpy_num = sizeof(T);
737+
if (offset + sizeof(T) > ts_obj_length)
738+
memcpy_num = ts_obj_length - offset;
737739

738740
union NoAlias {
739741
T val;
@@ -744,8 +746,8 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
744746
char* ptr = static_cast<char*>(ts_obj_data) + offset;
745747
if (endianness != GetEndianness())
746748
Swizzle(na.bytes, sizeof(na.bytes));
747-
memcpy(ptr, na.bytes, sizeof(na.bytes));
748-
return offset + sizeof(na.bytes);
749+
memcpy(ptr, na.bytes, memcpy_num);
750+
return offset + memcpy_num;
749751
}
750752

751753

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)