Skip to content

Commit

Permalink
dgram: fix abort on bad args
Browse files Browse the repository at this point in the history
This commit fixes a C++ abort for connected dgram sockets
by improving input validation in the JS layer.

Fixes: #28126
PR-URL: #28135
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and BridgeAR committed Jun 17, 2019
1 parent e1fc9b9 commit 0716944
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,19 @@ Socket.prototype.send = function(buffer,
port = offset;
address = length;
}
} else if (typeof length === 'number') {
buffer = sliceBuffer(buffer, offset, length);
if (typeof port === 'function') {
callback = port;
port = null;
} else if (port || address) {
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
}
} else {
callback = offset;
if (typeof length === 'number') {
buffer = sliceBuffer(buffer, offset, length);
if (typeof port === 'function') {
callback = port;
port = null;
}
} else {
callback = offset;
}

if (port || address)
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
}

if (!Array.isArray(buffer)) {
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-dgram-send-bad-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ function checkArgs(connected) {
message: 'Already connected'
}
);

common.expectsError(
() => { sock.send(buf, 1234, '127.0.0.1', common.mustNotCall()); },
{
code: 'ERR_SOCKET_DGRAM_IS_CONNECTED',
type: Error,
message: 'Already connected'
}
);
} else {
assert.throws(() => { sock.send(buf, 1, 1, -1, host); }, RangeError);
assert.throws(() => { sock.send(buf, 1, 1, 0, host); }, RangeError);
Expand Down

0 comments on commit 0716944

Please sign in to comment.