Skip to content

Commit 725ffdb

Browse files
committed
dgram: handle default address case when offset and length are specified
Fixes a regression introduced by: #4374. Adds a new test to avoid similar issue in the future. The test is disabled on windows, because this feature never worked there. Fixes: #5398 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent ffdc046 commit 725ffdb

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lib/dgram.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ function enqueue(self, toEnqueue) {
286286
}
287287

288288

289+
// valid combinations
290+
// send(buffer, offset, length, port, address, callback)
291+
// send(buffer, offset, length, port, address)
292+
// send(buffer, offset, length, port)
293+
// send(bufferOrList, port, address, callback)
294+
// send(bufferOrList, port, address)
295+
// send(bufferOrList, port)
289296
Socket.prototype.send = function(buffer,
290297
offset,
291298
length,
@@ -294,8 +301,7 @@ Socket.prototype.send = function(buffer,
294301
callback) {
295302
var self = this;
296303

297-
// same as arguments.length === 5 || arguments.length === 6
298-
if (address) {
304+
if (address || (port && typeof port !== 'function')) {
299305
buffer = sliceBuffer(buffer, offset, length);
300306
} else {
301307
callback = port;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const dgram = require('dgram');
6+
7+
if (common.isWindows) {
8+
// on Windows this test will fail
9+
// see https://github.com/nodejs/node/pull/5407
10+
console.log('1..0 # Skipped: This test does not apply on Windows.');
11+
return;
12+
}
13+
14+
const client = dgram.createSocket('udp4');
15+
16+
const timer = setTimeout(function() {
17+
throw new Error('Timeout');
18+
}, common.platformTimeout(2000));
19+
20+
const toSend = [new Buffer(256), new Buffer(256), new Buffer(256), 'hello'];
21+
22+
toSend[0].fill('x');
23+
toSend[1].fill('y');
24+
toSend[2].fill('z');
25+
26+
client.on('listening', function() {
27+
client.send(toSend[0], 0, toSend[0].length, common.PORT);
28+
client.send(toSend[1], common.PORT);
29+
client.send([toSend[2]], common.PORT);
30+
client.send(toSend[3], 0, toSend[3].length, common.PORT);
31+
});
32+
33+
client.on('message', function(buf, info) {
34+
const expected = toSend.shift().toString();
35+
assert.ok(buf.toString() === expected, 'message was received correctly');
36+
37+
if (toSend.length === 0) {
38+
client.close();
39+
clearTimeout(timer);
40+
}
41+
});
42+
43+
client.bind(common.PORT);

0 commit comments

Comments
 (0)