Skip to content

Commit 0716944

Browse files
cjihrigBridgeAR
authored andcommitted
dgram: fix abort on bad args
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 <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e1fc9b9 commit 0716944

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/dgram.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,16 +561,19 @@ Socket.prototype.send = function(buffer,
561561
port = offset;
562562
address = length;
563563
}
564-
} else if (typeof length === 'number') {
565-
buffer = sliceBuffer(buffer, offset, length);
566-
if (typeof port === 'function') {
567-
callback = port;
568-
port = null;
569-
} else if (port || address) {
570-
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
571-
}
572564
} else {
573-
callback = offset;
565+
if (typeof length === 'number') {
566+
buffer = sliceBuffer(buffer, offset, length);
567+
if (typeof port === 'function') {
568+
callback = port;
569+
port = null;
570+
}
571+
} else {
572+
callback = offset;
573+
}
574+
575+
if (port || address)
576+
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
574577
}
575578

576579
if (!Array.isArray(buffer)) {

test/parallel/test-dgram-send-bad-arguments.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ function checkArgs(connected) {
6868
message: 'Already connected'
6969
}
7070
);
71+
72+
common.expectsError(
73+
() => { sock.send(buf, 1234, '127.0.0.1', common.mustNotCall()); },
74+
{
75+
code: 'ERR_SOCKET_DGRAM_IS_CONNECTED',
76+
type: Error,
77+
message: 'Already connected'
78+
}
79+
);
7180
} else {
7281
assert.throws(() => { sock.send(buf, 1, 1, -1, host); }, RangeError);
7382
assert.throws(() => { sock.send(buf, 1, 1, 0, host); }, RangeError);

0 commit comments

Comments
 (0)