From 60ff9ec6f58fc7ecf517a2faafb8ccadfaa78ff7 Mon Sep 17 00:00:00 2001 From: SCG82 Date: Sun, 16 Oct 2022 15:48:10 -0700 Subject: [PATCH 1/2] Handled malformed output from child process --- lib/Local.js | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 7486bc7..374adcf 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -45,12 +45,24 @@ function Local(){ const obj = childProcess.spawnSync(that.binaryPath, that.getBinaryArgs()); this.tunnel = {pid: obj.pid}; var data = {}; - if(obj.stdout.length > 0) - data = JSON.parse(obj.stdout); - else if(obj.stderr.length > 0) - data = JSON.parse(obj.stderr); - else + if(obj.stdout.length > 0){ + try{ + data = JSON.parse(obj.stdout); + }catch(error){ + data = obj.stdout; + } + } else if(obj.stderr.length > 0){ + try{ + data = JSON.parse(obj.stderr); + }catch(error){ + data = obj.stderr; + } + } else { return new LocalError('No output received'); + } + if(typeof data === 'string'){ + return new LocalError('Malformed output received'); + } if(data['state'] != 'connected'){ return new LocalError(data['message']['message']); } else { @@ -101,13 +113,24 @@ function Local(){ } var data = {}; - if(stdout) - data = JSON.parse(stdout); - else if(stderr) - data = JSON.parse(stderr); - else - callback(new LocalError('No output received')); - + if(stdout){ + try{ + data = JSON.parse(stdout); + }catch(error){ + data = stdout; + } + } else if(stderr){ + try{ + data = JSON.parse(stderr); + }catch(error){ + data = stderr; + } + } else { + return callback(new LocalError('No output received')); + } + if(typeof data === 'string'){ + return callback(new LocalError('Malformed output received')); + } if(data['state'] != 'connected'){ callback(new LocalError(data['message']['message'])); } else { From bdc5ce32d6cda5f05e645b999c32c4e532e41259 Mon Sep 17 00:00:00 2001 From: SCG82 Date: Sun, 16 Oct 2022 16:10:40 -0700 Subject: [PATCH 2/2] Correctly handle errors --- lib/Local.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 374adcf..98cf766 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -105,10 +105,9 @@ function Local(){ that.retriesLeft -= 1; fs.unlinkSync(that.binaryPath); delete(that.binaryPath); - that.start(options, callback); - return; + return that.start(options, callback); } else { - callback(new LocalError(error.toString())); + return callback(new LocalError(error.toString())); } } @@ -149,7 +148,7 @@ function Local(){ this.stop = function (callback) { if(!this.pid) return callback(); this.killAllProcesses(function(error){ - if(error) callback(new LocalError(error.toString())); + if(error) return callback(new LocalError(error.toString())); callback(); }); }; @@ -339,6 +338,7 @@ function Local(){ this.killAllProcesses = function(callback){ psTree(this.pid, (err, children) => { + if(err) return callback(err); var childPids = children.map(val => val.PID); var killChecker = setInterval(() => { if(childPids.length === 0) {