Skip to content

Commit e9b6fbb

Browse files
committedSep 8, 2016
dgram: prefer strict equality, type validation
- Enforces strict comparisons in dgram - bindState should always be strictly equal to one of the defined constant states, and newHandle type is a string. - Check that the argument `type` in createSocket is not null when it is of type 'object', before using its `type` property. - Adds a test to check dgram.createSocket is properly validating its `type` argument. PR-URL: #8011 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yorkie Liu <[email protected]> Reviewed-By: Jackson Tian <[email protected]>
1 parent a634554 commit e9b6fbb

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed
 

‎lib/dgram.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ function lookup6(address, callback) {
3939

4040

4141
function newHandle(type) {
42-
if (type == 'udp4') {
42+
if (type === 'udp4') {
4343
const handle = new UDP();
4444
handle.lookup = lookup4;
4545
return handle;
4646
}
4747

48-
if (type == 'udp6') {
48+
if (type === 'udp6') {
4949
const handle = new UDP();
5050
handle.lookup = lookup6;
5151
handle.bind = handle.bind6;
@@ -78,7 +78,7 @@ exports._createSocketHandle = function(address, port, addressType, fd, flags) {
7878
function Socket(type, listener) {
7979
EventEmitter.call(this);
8080

81-
if (typeof type === 'object') {
81+
if (type !== null && typeof type === 'object') {
8282
var options = type;
8383
type = options.type;
8484
}
@@ -137,7 +137,7 @@ Socket.prototype.bind = function(port_ /*, address, callback*/) {
137137

138138
self._healthCheck();
139139

140-
if (this._bindState != BIND_STATE_UNBOUND)
140+
if (this._bindState !== BIND_STATE_UNBOUND)
141141
throw new Error('Socket is already bound');
142142

143143
this._bindState = BIND_STATE_BINDING;
@@ -346,15 +346,15 @@ Socket.prototype.send = function(buffer,
346346

347347
self._healthCheck();
348348

349-
if (self._bindState == BIND_STATE_UNBOUND)
349+
if (self._bindState === BIND_STATE_UNBOUND)
350350
self.bind({port: 0, exclusive: true}, null);
351351

352352
if (list.length === 0)
353353
list.push(Buffer.allocUnsafe(0));
354354

355355
// If the socket hasn't been bound yet, push the outbound packet onto the
356356
// send queue and send after binding is complete.
357-
if (self._bindState != BIND_STATE_BOUND) {
357+
if (self._bindState !== BIND_STATE_BOUND) {
358358
enqueue(self, self.send.bind(self, list, port, address, callback));
359359
return;
360360
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
5+
const invalidTypes = [
6+
'test',
7+
['udp4'],
8+
new String('udp4'),
9+
1,
10+
{},
11+
true,
12+
false,
13+
null,
14+
undefined
15+
];
16+
const validTypes = [
17+
'udp4',
18+
'udp6',
19+
{ type: 'udp4' },
20+
{ type: 'udp6' }
21+
];
22+
23+
// Error must be thrown with invalid types
24+
invalidTypes.forEach((invalidType) => {
25+
assert.throws(() => {
26+
dgram.createSocket(invalidType);
27+
}, /Bad socket type specified/);
28+
});
29+
30+
// Error must not be thrown with valid types
31+
validTypes.forEach((validType) => {
32+
assert.doesNotThrow(() => {
33+
const socket = dgram.createSocket(validType);
34+
socket.close();
35+
});
36+
});

0 commit comments

Comments
 (0)
Please sign in to comment.