net: make .write() throw on bad input
Passing a non-buffer or non-string argument to Socket.prototype.write triggered an assert: Assertion failed: (Buffer::HasInstance(args[0])), function Write, file ../src/stream_wrap.cc, line 289. Fixes #2532.
This commit is contained in:
parent
766f609838
commit
f0c1376e07
@ -424,8 +424,10 @@ Socket.prototype.write = function(data, arg1, arg2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Change strings to buffers. SLOW
|
// Change strings to buffers. SLOW
|
||||||
if (typeof data == 'string') {
|
if (typeof data === 'string') {
|
||||||
data = new Buffer(data, encoding);
|
data = new Buffer(data, encoding);
|
||||||
|
} else if (!Buffer.isBuffer(data)) {
|
||||||
|
throw new TypeError("First argument must be a buffer or a string.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.bytesWritten += data.length;
|
this.bytesWritten += data.length;
|
||||||
|
@ -63,6 +63,25 @@ tcp.listen(common.PORT, function() {
|
|||||||
|
|
||||||
assert.equal('opening', socket.readyState);
|
assert.equal('opening', socket.readyState);
|
||||||
|
|
||||||
|
// Make sure that anything besides a buffer or a string throws.
|
||||||
|
[ null,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
undefined,
|
||||||
|
1,
|
||||||
|
1.0,
|
||||||
|
1 / 0,
|
||||||
|
+Infinity
|
||||||
|
-Infinity,
|
||||||
|
[],
|
||||||
|
{}
|
||||||
|
].forEach(function(v) {
|
||||||
|
function f() {
|
||||||
|
socket.write(v);
|
||||||
|
}
|
||||||
|
assert.throws(f, TypeError);
|
||||||
|
});
|
||||||
|
|
||||||
// Write a string that contains a multi-byte character sequence to test that
|
// Write a string that contains a multi-byte character sequence to test that
|
||||||
// `bytesWritten` is incremented with the # of bytes, not # of characters.
|
// `bytesWritten` is incremented with the # of bytes, not # of characters.
|
||||||
var a = "L'État, c'est ";
|
var a = "L'État, c'est ";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user