Skip to content

Commit 4b9d84d

Browse files
trevnorrisaddaleax
authored andcommitted
tty_wrap: throw when uv_tty_init() returns error
Also add checks in lib/tty.js and tests. PR-URL: #12892 Ref: #11883 Ref: #8531 Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent dd6e3f6 commit 4b9d84d

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

lib/tty.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ exports.isatty = function(fd) {
3737
function ReadStream(fd, options) {
3838
if (!(this instanceof ReadStream))
3939
return new ReadStream(fd, options);
40+
if (fd >> 0 !== fd || fd < 0)
41+
throw new RangeError('fd must be positive integer: ' + fd);
4042

4143
options = util._extend({
4244
highWaterMark: 0,
@@ -62,7 +64,11 @@ ReadStream.prototype.setRawMode = function(flag) {
6264

6365

6466
function WriteStream(fd) {
65-
if (!(this instanceof WriteStream)) return new WriteStream(fd);
67+
if (!(this instanceof WriteStream))
68+
return new WriteStream(fd);
69+
if (fd >> 0 !== fd || fd < 0)
70+
throw new RangeError('fd must be positive integer: ' + fd);
71+
6672
net.Socket.call(this, {
6773
handle: new TTY(fd, false),
6874
readable: false,

src/tty_wrap.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,25 @@ void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
150150
int fd = args[0]->Int32Value();
151151
CHECK_GE(fd, 0);
152152

153-
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue());
153+
int err = 0;
154+
TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
155+
if (err != 0)
156+
return env->ThrowUVException(err, "uv_tty_init");
157+
154158
wrap->UpdateWriteQueueSize();
155159
}
156160

157161

158-
TTYWrap::TTYWrap(Environment* env, Local<Object> object, int fd, bool readable)
162+
TTYWrap::TTYWrap(Environment* env,
163+
Local<Object> object,
164+
int fd,
165+
bool readable,
166+
int* init_err)
159167
: StreamWrap(env,
160168
object,
161169
reinterpret_cast<uv_stream_t*>(&handle_),
162170
AsyncWrap::PROVIDER_TTYWRAP) {
163-
uv_tty_init(env->event_loop(), &handle_, fd, readable);
171+
*init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
164172
}
165173

166174
} // namespace node

src/tty_wrap.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class TTYWrap : public StreamWrap {
4444
TTYWrap(Environment* env,
4545
v8::Local<v8::Object> object,
4646
int fd,
47-
bool readable);
47+
bool readable,
48+
int* init_err);
4849

4950
static void GuessHandleType(const v8::FunctionCallbackInfo<v8::Value>& args);
5051
static void IsTTY(const v8::FunctionCallbackInfo<v8::Value>& args);
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const fs = require('fs');
6+
const tty = require('tty');
7+
8+
9+
assert.throws(() => {
10+
new tty.WriteStream(-1);
11+
}, /fd must be positive integer:/);
12+
13+
const err_regex = common.isWindows ?
14+
/^Error: EBADF: bad file descriptor, uv_tty_init$/ :
15+
/^Error: EINVAL: invalid argument, uv_tty_init$/;
16+
assert.throws(() => {
17+
let fd = 2;
18+
// Get first known bad file descriptor.
19+
try {
20+
while (fs.fstatSync(++fd));
21+
} catch (e) { }
22+
new tty.WriteStream(fd);
23+
}, err_regex);
24+
25+
assert.throws(() => {
26+
new tty.ReadStream(-1);
27+
}, /fd must be positive integer:/);
28+
29+
assert.throws(() => {
30+
let fd = 2;
31+
// Get first known bad file descriptor.
32+
try {
33+
while (fs.fstatSync(++fd));
34+
} catch (e) { }
35+
new tty.ReadStream(fd);
36+
}, err_regex);

0 commit comments

Comments
 (0)