Skip to content
Permalink
Browse files

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 <trev.norris@gmail.com>
  • Loading branch information...
pmq20 authored and trevnorris committed Nov 16, 2015
1 parent df268f9 commit 0ed3a7c11b4031bdb56908b16f2aa5aea24c1813
Showing with 12 additions and 3 deletions.
  1. +5 −3 src/node_buffer.cc
  2. +7 −0 test/parallel/test-buffer-arraybuffer.js
@@ -735,7 +735,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;
@@ -746,8 +748,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);

6 comments on commit 0ed3a7c

@mscdex

This comment has been minimized.

Copy link
Contributor

replied Nov 17, 2015

This is missing the PR-URL which should be: #3767

@trevnorris

This comment has been minimized.

Copy link
Contributor

replied Nov 17, 2015

FFFFUUUU!!!!

Thanks @mscdex for placing the reference here.

@jasnell

This comment has been minimized.

Copy link
Member

replied Nov 17, 2015

I'm just happy I'm not the only one ;-)

@evanlucas

This comment has been minimized.

Copy link
Member

replied Nov 17, 2015

/cc @nodejs/punished :]

@trevnorris

This comment has been minimized.

Copy link
Contributor

replied Nov 17, 2015

@evanlucas yeah, i'm the only one on it. :-(

@jasnell

This comment has been minimized.

Copy link
Member

replied Nov 17, 2015

(heh.. they just haven't noticed any of mine yet ... ;-) ... oh, wait, did I just say that out loud)

Please sign in to comment.
You can’t perform that action at this time.