forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit fix a possible crash situation in dgram send(). A crash is possible if an array is passed, and then altered after the send call, as the call to libuv is wrapped in process.nextTick(). It also avoid sending an empty array to libuv by allocating an empty buffer. It also does some cleanup inside send() to increase readability. It removes test flakyness by use common.mustCall and common.platformTimeout. Fixes situations were some events were not asserted to be emitted. Fixes: nodejs#6616 PR-URL: nodejs#6804 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
- Loading branch information
1 parent
4f1c108
commit e995e0d
Showing
7 changed files
with
105 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
'use strict'; | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
var dgram = require('dgram'); | ||
var client, timer, buf, len, offset; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const dgram = require('dgram'); | ||
const client = dgram.createSocket('udp4'); | ||
|
||
client = dgram.createSocket('udp4'); | ||
|
||
buf = Buffer.allocUnsafe(256); | ||
offset = 20; | ||
|
||
len = buf.length - offset; | ||
const timer = setTimeout(function() { | ||
throw new Error('Timeout'); | ||
}, common.platformTimeout(200)); | ||
|
||
const buf = Buffer.allocUnsafe(256); | ||
const offset = 20; | ||
const len = buf.length - offset; | ||
|
||
client.send(buf, offset, len, common.PORT, '127.0.0.1', function(err, bytes) { | ||
const messageSent = common.mustCall(function messageSent(err, bytes) { | ||
assert.notEqual(bytes, buf.length); | ||
assert.equal(bytes, buf.length - offset); | ||
clearTimeout(timer); | ||
client.close(); | ||
}); | ||
|
||
timer = setTimeout(function() { | ||
throw new Error('Timeout'); | ||
}, 200); | ||
client.send(buf, offset, len, common.PORT, '127.0.0.1', messageSent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
if (process.platform === 'darwin') { | ||
common.skip('because of 17894467 Apple bug'); | ||
return; | ||
} | ||
|
||
const client = dgram.createSocket('udp4'); | ||
|
||
const timer = setTimeout(function() { | ||
throw new Error('Timeout'); | ||
}, common.platformTimeout(200)); | ||
|
||
client.on('message', common.mustCall(function onMessage(buf, info) { | ||
const expected = Buffer.alloc(0); | ||
assert.ok(buf.equals(expected), 'message was received correctly'); | ||
clearTimeout(timer); | ||
client.close(); | ||
})); | ||
|
||
client.on('listening', function() { | ||
client.send([], common.PORT, common.localhostIPv4); | ||
}); | ||
|
||
client.bind(common.PORT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
|
||
const client = dgram.createSocket('udp4'); | ||
|
||
const timer = setTimeout(function() { | ||
throw new Error('Timeout'); | ||
}, common.platformTimeout(200)); | ||
|
||
const onMessage = common.mustCall(function(err, bytes) { | ||
assert.equal(bytes, buf1.length + buf2.length); | ||
clearTimeout(timer); | ||
}); | ||
|
||
const buf1 = Buffer.alloc(256, 'x'); | ||
const buf2 = Buffer.alloc(256, 'y'); | ||
|
||
client.on('listening', function() { | ||
const toSend = [buf1, buf2]; | ||
client.send(toSend, common.PORT, common.localhostIPv4, onMessage); | ||
toSend.splice(0, 2); | ||
}); | ||
|
||
client.on('message', common.mustCall(function onMessage(buf, info) { | ||
const expected = Buffer.concat([buf1, buf2]); | ||
assert.ok(buf.equals(expected), 'message was received correctly'); | ||
client.close(); | ||
})); | ||
|
||
client.bind(common.PORT); |