Skip to content

Commit ad6769f

Browse files
committed
dgram: don't assert on send('string')
Raise a TypeError when the argument to send() or sendto() is anything but a Buffer. Fixes the following assertion: $ node -e 'require("dgram").createSocket("udp4").send("BAM")' node: ../../src/udp_wrap.cc:220: static v8::Handle<v8::Value> node::UDPWrap::DoSend(const v8::Arguments&, int): Assertion `Buffer::HasInstance(args[0])' failed. Aborted (core dumped) Fixes #4496.
1 parent 872cb0d commit ad6769f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/dgram.js

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ Socket.prototype.send = function(buffer,
165165
callback) {
166166
var self = this;
167167

168+
if (!Buffer.isBuffer(buffer))
169+
throw new TypeError('First argument must be a buffer object.');
170+
168171
if (offset >= buffer.length)
169172
throw new Error('Offset into buffer too large');
170173

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
// Remove this test once we support sending strings.
23+
24+
var common = require('../common');
25+
var assert = require('assert');
26+
var dgram = require('dgram');
27+
28+
// Should throw but not crash.
29+
var socket = dgram.createSocket('udp4');
30+
assert.throws(function() { socket.send('BAM', 0, 1, 1, 'host') }, TypeError);
31+
assert.throws(function() { socket.sendto('BAM', 0, 1, 1, 'host') }, TypeError);
32+
socket.close();

0 commit comments

Comments
 (0)