mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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: https://github.com/nodejs/node/issues/3766 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
1 parent
4a26f74ee3
commit
db0186e435
2 files changed
+12
-3
No files matched your search
+5
-3
@@ -730,7 +730,9 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
T val = args[1]->NumberValue();
|
||||
uint32_t offset = args[2]->Uint32Value();
|
||||
CHECK_LE(offset + sizeof(T), ts_obj_length);
|
||||
size_t memcpy_num = sizeof(T);
|
||||
if (offset + sizeof(T) > ts_obj_length)
|
||||
memcpy_num = ts_obj_length - offset;
|
||||
|
||||
union NoAlias {
|
||||
T val;
|
||||
@@ -741,8 +743,8 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
char* ptr = static_cast<char*>(ts_obj_data) + offset;
|
||||
if (endianness != GetEndianness())
|
||||
Swizzle(na.bytes, sizeof(na.bytes));
|
||||
memcpy(ptr, na.bytes, sizeof(na.bytes));
|
||||
return offset + sizeof(na.bytes);
|
||||
memcpy(ptr, na.bytes, memcpy_num);
|
||||
return offset + memcpy_num;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -44,3 +44,10 @@ assert.throws(function() {
|
||||
AB.prototype.__proto__ = ArrayBuffer.prototype;
|
||||
new Buffer(new AB());
|
||||
}, TypeError);
|
||||
|
||||
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
|
||||
var b = new Buffer(1);
|
||||
b.writeFloatLE(11.11, 0, true);
|
||||
b.writeFloatBE(11.11, 0, true);
|
||||
b.writeDoubleLE(11.11, 0, true);
|
||||
b.writeDoubleBE(11.11, 0, true);
|
||||
Reference in new issue
Block a user