Skip to content

Commit eef59fd

Browse files
natcldceejay
authored andcommitted
Add additional safety checks to avoid acting on non-existent objects (node-red#1057)
1 parent 361ff31 commit eef59fd

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

nodes/core/io/31-tcpin.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ module.exports = function(RED) {
444444
//node.log(RED._("tcpin.errors.client-connected"));
445445
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
446446
node.connected = true;
447-
if (clients[connection_id].client) {
447+
if (clients[connection_id] && clients[connection_id].client) {
448448
clients[connection_id].client.write(clients[connection_id].msg.payload);
449449
}
450450
});
@@ -455,8 +455,10 @@ module.exports = function(RED) {
455455

456456
clients[connection_id].client.on('data', function(data) {
457457
if (node.out == "sit") { // if we are staying connected just send the buffer
458-
clients[connection_id].msg.payload = data;
459-
node.send(clients[connection_id].msg);
458+
if (clients[connection_id]) {
459+
clients[connection_id].msg.payload = data;
460+
node.send(clients[connection_id].msg);
461+
}
460462
}
461463
else if (node.splitc === 0) {
462464
clients[connection_id].msg.payload = data;
@@ -523,7 +525,9 @@ module.exports = function(RED) {
523525
//console.log("END");
524526
node.connected = false;
525527
node.status({fill:"grey",shape:"ring",text:"common.status.disconnected"});
526-
clients[connection_id].client = null;
528+
if (clients[connection_id] && clients[connection_id].client) {
529+
clients[connection_id].client = null;
530+
}
527531
});
528532

529533
clients[connection_id].client.on('close', function() {
@@ -537,7 +541,7 @@ module.exports = function(RED) {
537541
node.connected = false;
538542
node.status({fill:"red",shape:"ring",text:"common.status.error"});
539543
node.error(RED._("tcpin.errors.connect-fail"),msg);
540-
if (clients[connection_id].client) {
544+
if (clients[connection_id] && clients[connection_id].client) {
541545
clients[connection_id].client.destroy();
542546
delete clients[connection_id];
543547
}
@@ -547,7 +551,7 @@ module.exports = function(RED) {
547551
node.connected = false;
548552
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
549553
//node.warn(RED._("tcpin.errors.connect-timeout"));
550-
if (clients[connection_id].client) {
554+
if (clients[connection_id] && clients[connection_id].client) {
551555
clients[connection_id].client.connect(port, host, function() {
552556
node.connected = true;
553557
node.status({fill:"green",shape:"dot",text:"common.status.connected"});

0 commit comments

Comments
 (0)