Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7be4e49

Browse filesBrowse files
jasnellrvagg
authored andcommitted
child_process: check execFile and fork args
Port of joyent/node commits: * nodejs/node-v0.x-archive@e17c5a7 * nodejs/node-v0.x-archive@70dafa7 Pull over test-child-process-spawn-typeerror.js from v0.12, replacing the existing test in master. The new test includes a broader set of tests on the various arg choices and throws. Reviewed-By: trevnorris - Trevor Norris <trevnorris@nodejs.org> Reviewed-By: cjihrig - Colin Ihrig <cjihrig@gmail.com> Reviewed-By: thefourtheye - Sakthipriyan Vairamani PR-URL: #2667 Fixes: #2515
1 parent b2c5479 commit 7be4e49
Copy full SHA for 7be4e49

File tree

Expand file treeCollapse file tree

2 files changed

+114
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+114
-19
lines changed
Open diff view settings
Collapse file

‎lib/child_process.js‎

Copy file name to clipboardExpand all lines: lib/child_process.js
+20-10Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ exports.fork = function(modulePath /*, args, options*/) {
2323
if (Array.isArray(arguments[1])) {
2424
args = arguments[1];
2525
options = util._extend({}, arguments[2]);
26+
} else if (arguments[1] && typeof arguments[1] !== 'object') {
27+
throw new TypeError('Incorrect value of args option');
2628
} else {
2729
args = [];
2830
options = util._extend({}, arguments[1]);
@@ -108,7 +110,7 @@ exports.exec = function(command /*, options, callback*/) {
108110

109111

110112
exports.execFile = function(file /*, args, options, callback*/) {
111-
var args, callback;
113+
var args = [], callback;
112114
var options = {
113115
encoding: 'utf8',
114116
timeout: 0,
@@ -118,18 +120,26 @@ exports.execFile = function(file /*, args, options, callback*/) {
118120
env: null
119121
};
120122

121-
// Parse the parameters.
123+
// Parse the optional positional parameters.
124+
var pos = 1;
125+
if (pos < arguments.length && Array.isArray(arguments[pos])) {
126+
args = arguments[pos++];
127+
} else if (pos < arguments.length && arguments[pos] == null) {
128+
pos++;
129+
}
122130

123-
if (typeof arguments[arguments.length - 1] === 'function') {
124-
callback = arguments[arguments.length - 1];
131+
if (pos < arguments.length && typeof arguments[pos] === 'object') {
132+
options = util._extend(options, arguments[pos++]);
133+
} else if (pos < arguments.length && arguments[pos] == null) {
134+
pos++;
125135
}
126136

127-
if (Array.isArray(arguments[1])) {
128-
args = arguments[1];
129-
options = util._extend(options, arguments[2]);
130-
} else {
131-
args = [];
132-
options = util._extend(options, arguments[1]);
137+
if (pos < arguments.length && typeof arguments[pos] === 'function') {
138+
callback = arguments[pos++];
139+
}
140+
141+
if (pos === 1 && arguments.length > 1) {
142+
throw new TypeError('Incorrect value of args option');
133143
}
134144

135145
var child = spawn(file, args, {
Collapse file

‎test/parallel/test-child-process-spawn-typeerror.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-child-process-spawn-typeerror.js
+94-9Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
'use strict';
2-
var assert = require('assert');
3-
var child_process = require('child_process');
4-
var spawn = child_process.spawn;
5-
var cmd = require('../common').isWindows ? 'rundll32' : 'ls';
6-
var invalidArgsMsg = /Incorrect value of args option/;
7-
var invalidOptionsMsg = /options argument must be an object/;
8-
9-
// verify that args argument must be an array
2+
const assert = require('assert');
3+
const child_process = require('child_process');
4+
const spawn = child_process.spawn;
5+
const fork = child_process.fork;
6+
const execFile = child_process.execFile;
7+
const common = require('../common');
8+
const cmd = common.isWindows ? 'rundll32' : 'ls';
9+
const invalidcmd = 'hopefully_you_dont_have_this_on_your_machine';
10+
const invalidArgsMsg = /Incorrect value of args option/;
11+
const invalidOptionsMsg = /options argument must be an object/;
12+
const empty = common.fixturesDir + '/empty.js';
13+
1014
assert.throws(function() {
11-
spawn(cmd, 'this is not an array');
15+
var child = spawn(invalidcmd, 'this is not an array');
16+
child.on('error', assert.fail);
1217
}, TypeError);
1318

1419
// verify that valid argument combinations do not throw
@@ -49,3 +54,83 @@ assert.throws(function() {
4954
spawn(cmd, [], 1);
5055
}, invalidOptionsMsg);
5156

57+
// Argument types for combinatorics
58+
const a = [];
59+
const o = {};
60+
const c = function callback() {};
61+
const s = 'string';
62+
const u = undefined;
63+
const n = null;
64+
65+
// function spawn(file=f [,args=a] [, options=o]) has valid combinations:
66+
// (f)
67+
// (f, a)
68+
// (f, a, o)
69+
// (f, o)
70+
assert.doesNotThrow(function() { spawn(cmd); });
71+
assert.doesNotThrow(function() { spawn(cmd, a); });
72+
assert.doesNotThrow(function() { spawn(cmd, a, o); });
73+
assert.doesNotThrow(function() { spawn(cmd, o); });
74+
75+
// Variants of undefined as explicit 'no argument' at a position
76+
assert.doesNotThrow(function() { spawn(cmd, u, o); });
77+
assert.doesNotThrow(function() { spawn(cmd, a, u); });
78+
79+
assert.throws(function() { spawn(cmd, n, o); }, TypeError);
80+
assert.throws(function() { spawn(cmd, a, n); }, TypeError);
81+
82+
assert.throws(function() { spawn(cmd, s); }, TypeError);
83+
assert.throws(function() { spawn(cmd, a, s); }, TypeError);
84+
85+
86+
// verify that execFile has same argument parsing behaviour as spawn
87+
//
88+
// function execFile(file=f [,args=a] [, options=o] [, callback=c]) has valid
89+
// combinations:
90+
// (f)
91+
// (f, a)
92+
// (f, a, o)
93+
// (f, a, o, c)
94+
// (f, a, c)
95+
// (f, o)
96+
// (f, o, c)
97+
// (f, c)
98+
assert.doesNotThrow(function() { execFile(cmd); });
99+
assert.doesNotThrow(function() { execFile(cmd, a); });
100+
assert.doesNotThrow(function() { execFile(cmd, a, o); });
101+
assert.doesNotThrow(function() { execFile(cmd, a, o, c); });
102+
assert.doesNotThrow(function() { execFile(cmd, a, c); });
103+
assert.doesNotThrow(function() { execFile(cmd, o); });
104+
assert.doesNotThrow(function() { execFile(cmd, o, c); });
105+
assert.doesNotThrow(function() { execFile(cmd, c); });
106+
107+
// Variants of undefined as explicit 'no argument' at a position
108+
assert.doesNotThrow(function() { execFile(cmd, u, o, c); });
109+
assert.doesNotThrow(function() { execFile(cmd, a, u, c); });
110+
assert.doesNotThrow(function() { execFile(cmd, a, o, u); });
111+
assert.doesNotThrow(function() { execFile(cmd, n, o, c); });
112+
assert.doesNotThrow(function() { execFile(cmd, a, n, c); });
113+
assert.doesNotThrow(function() { execFile(cmd, a, o, n); });
114+
115+
// string is invalid in arg position (this may seem strange, but is
116+
// consistent across node API, cf. `net.createServer('not options', 'not
117+
// callback')`
118+
assert.throws(function() { execFile(cmd, s, o, c); }, TypeError);
119+
assert.doesNotThrow(function() { execFile(cmd, a, s, c); });
120+
assert.doesNotThrow(function() { execFile(cmd, a, o, s); });
121+
122+
123+
// verify that fork has same argument parsing behaviour as spawn
124+
//
125+
// function fork(file=f [,args=a] [, options=o]) has valid combinations:
126+
// (f)
127+
// (f, a)
128+
// (f, a, o)
129+
// (f, o)
130+
assert.doesNotThrow(function() { fork(empty); });
131+
assert.doesNotThrow(function() { fork(empty, a); });
132+
assert.doesNotThrow(function() { fork(empty, a, o); });
133+
assert.doesNotThrow(function() { fork(empty, o); });
134+
135+
assert.throws(function() { fork(empty, s); }, TypeError);
136+
assert.doesNotThrow(function() { fork(empty, a, s); }, TypeError);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.