child_process: use stdio.fd even if it is 0
authorEvan Lucas <evanlucas@me.com>
Mon, 7 Sep 2015 17:25:25 +0000 (12:25 -0500)
committerRod Vagg <rod@vagg.org>
Tue, 8 Sep 2015 00:35:14 +0000 (10:35 +1000)
Previously, in _validateStdio we were using stdio.fd || stdio. If
stdio.fd was falsy (or 0 in the case of stdin), then the entire stdio
object would be passed which could cause a crash.

Fixes: https://github.com/nodejs/node/issues/2721
PR-URL: https://github.com/nodejs/node/pull/2727
Reviewed-By: silverwind - Roman Reiss <me@silverwind.io>
Reviewed-By: cjihrig - Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: indutny - Fedor Indutny <fedor.indutny@gmail.com>
lib/internal/child_process.js
test/parallel/test-child-process-validate-stdio.js

index 6d1c22d03b48eeee73e7d5b36e502fa9b42ab236..328a67d1ef09628753597a49d31741e5959d642f 100644 (file)
@@ -766,7 +766,7 @@ function _validateStdio(stdio, sync) {
     } else if (typeof stdio === 'number' || typeof stdio.fd === 'number') {
       acc.push({
         type: 'fd',
-        fd: stdio.fd || stdio
+        fd: typeof stdio === 'number' ? stdio : stdio.fd
       });
     } else if (getHandleWrapType(stdio) || getHandleWrapType(stdio.handle) ||
                getHandleWrapType(stdio._handle)) {
index aba43551e82b448885a7b8b5ab6414a135e57804..289323002da4bd4bfd18bb0913e63f62f53c07f1 100644 (file)
@@ -28,3 +28,15 @@ var stdio2 = ['ipc', 'ipc', 'ipc'];
 assert.throws(function() {
   _validateStdio(stdio2, true);
 }, /You cannot use IPC with synchronous forks/);
+
+const stdio3 = [process.stdin, process.stdout, process.stderr];
+var result = _validateStdio(stdio3, false);
+assert.deepStrictEqual(result, {
+  stdio: [
+    { type: 'fd', fd: 0 },
+    { type: 'fd', fd: 1 },
+    { type: 'fd', fd: 2 }
+  ],
+  ipc: undefined,
+  ipcFd: undefined
+});