Skip to content

Commit 41ec6d0

Browse files
committed
dgram: fix regression in string argument handling
v0.10 allows strings for the offset, length and port arguments to dgram.send() and dgram.sendto() but master before this commit would abort with the following assert: node: ../../src/udp_wrap.cc:227: static void node::UDPWrap::DoSend(const v8::FunctionCallbackInfo<v8::Value>&, int): Assertion `args[2]->IsUint32()' failed. Go beyond what v0.10 does and also add range checks: offset and length should be >= 0, port should be between 1 and 65535. That particular change needs to be back-ported to v0.10 because passing a negative offset or length number aborts with the following assertions: node: ../../src/udp_wrap.cc:264: static v8::Handle<v8::Value> node::UDPWrap::DoSend(const v8::Arguments&, int): Assertion `offset < Buffer::Length(buffer_obj)' failed. Or: node: ../../src/udp_wrap.cc:265: static v8::Handle<v8::Value> node::UDPWrap::DoSend(const v8::Arguments&, int): Assertion `length <= Buffer::Length(buffer_obj) - offset' failed. Interestingly enough, a negative port number is accepted in v0.10 but is silently ignored. This commit exposed a bug in the simple/test-dgram-close test which has also been fixed.
1 parent f674b09 commit 41ec6d0

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

lib/dgram.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,25 @@ Socket.prototype.send = function(buffer,
252252
if (!util.isBuffer(buffer))
253253
throw new TypeError('First argument must be a buffer object.');
254254

255+
offset = offset | 0;
256+
if (offset < 0)
257+
throw new RangeError('Offset should be >= 0');
258+
255259
if (offset >= buffer.length)
256-
throw new Error('Offset into buffer too large');
260+
throw new RangeError('Offset into buffer too large');
261+
262+
// Sending a zero-length datagram is kind of pointless but it _is_
263+
// allowed, hence check that length >= 0 rather than > 0.
264+
length = length | 0;
265+
if (length < 0)
266+
throw new RangeError('Length should be >= 0');
257267

258268
if (offset + length > buffer.length)
259-
throw new Error('Offset + length beyond buffer length');
269+
throw new RangeError('Offset + length beyond buffer length');
270+
271+
port = port | 0;
272+
if (port <= 0 || port > 65535)
273+
throw new RangeError('Port should be > 0 and < 65536');
260274

261275
callback = callback || noop;
262276

test/simple/test-dgram-close.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ var buf = new Buffer(1024);
3030
buf.fill(42);
3131

3232
var socket = dgram.createSocket('udp4');
33-
34-
socket.send(buf, 0, buf.length, common.port, 'localhost');
33+
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
3534
socket.close();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
var common = require('../common');
23+
var assert = require('assert');
24+
var dgram = require('dgram');
25+
26+
var buf = Buffer('test');
27+
var host = '127.0.0.1';
28+
var sock = dgram.createSocket('udp4');
29+
30+
assert.throws(function() {
31+
sock.send();
32+
}, TypeError); // First argument should be a buffer.
33+
34+
assert.throws(function() { sock.send(buf, -1, 1, 1, host); }, RangeError);
35+
assert.throws(function() { sock.send(buf, 1, -1, 1, host); }, RangeError);
36+
assert.throws(function() { sock.send(buf, 1, 1, -1, host); }, RangeError);
37+
assert.throws(function() { sock.send(buf, 5, 1, 1, host); }, RangeError);
38+
assert.throws(function() { sock.send(buf, 1, 5, 1, host); }, RangeError);
39+
assert.throws(function() { sock.send(buf, 1, 1, 0, host); }, RangeError);
40+
assert.throws(function() { sock.send(buf, 1, 1, 65536, host); }, RangeError);

0 commit comments

Comments
 (0)