Skip to content

Commit

Permalink
tty,doc: add type-check to isatty
Browse files Browse the repository at this point in the history
Previously, various inputs other than non-negative integers would
produce incorrect results.

Added type-checking on input, returning false for anything other than
non-negative integers.

Also clarified in docs.

PR-URL: #15567
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
bengl authored and MylesBorins committed Nov 28, 2017
1 parent 5095b99 commit ae5930b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/api/tty.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ added: v0.5.8
* `fd` {number} A numeric file descriptor

The `tty.isatty()` method returns `true` if the given `fd` is associated with
a TTY and `false` if is not.
a TTY and `false` if it is not, including whenever `fd` is not a non-negative
integer.
2 changes: 1 addition & 1 deletion lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const readline = require('readline');


exports.isatty = function(fd) {
return isTTY(fd);
return Number.isInteger(fd) && fd >= 0 && isTTY(fd);
};


Expand Down
17 changes: 17 additions & 0 deletions test/pseudo-tty/test-tty-isatty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../common');
const { strictEqual } = require('assert');
const { isatty } = require('tty');

strictEqual(isatty(0), true, 'stdin reported to not be a tty, but it is');
strictEqual(isatty(1), true, 'stdout reported to not be a tty, but it is');
strictEqual(isatty(2), true, 'stderr reported to not be a tty, but it is');

strictEqual(isatty(-1), false, '-1 reported to be a tty, but it is not');
strictEqual(isatty(55555), false, '55555 reported to be a tty, but it is not');
strictEqual(isatty(1.1), false, '1.1 reported to be a tty, but it is not');
strictEqual(isatty('1'), false, '\'1\' reported to be a tty, but it is not');
strictEqual(isatty({}), false, '{} reported to be a tty, but it is not');
strictEqual(isatty(() => {}), false,
'() => {} reported to be a tty, but it is not');
Empty file.

0 comments on commit ae5930b

Please sign in to comment.