Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Cannot read properties of null for setRawMode #41335

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ObjectSetPrototypeOf(ReadStream, net.Socket);

ReadStream.prototype.setRawMode = function(flag) {
flag = !!flag;
const err = this._handle.setRawMode(flag);
const err = this._handle && this._handle.setRawMode(flag);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be covered by a test. Also, is it correct that this function does not fail if this._handle == null?

Copy link
Author

@asaid-0 asaid-0 Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review
I think it's safe as long as this._handle would be null if this function is called when using the terminal.
also, I found this check in readline interface:

if (typeof this.input.setRawMode === 'function') {
this.input.setRawMode(mode);

I have covered this issue by a test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tniessen What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const err = this._handle && this._handle.setRawMode(flag);
const err = this._handle?.setRawMode(flag);

if (err) {
this.emit('error', errors.errnoException(err, 'setRawMode'));
return this;
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-repl-stdin-push-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
require('../common');

// This test ensures that the repl does not
// throw when using process.stdin stream to push null
// Refs: https://github.com/nodejs/node/issues/41330

const r = require('repl').start();

// Should not throw.
r.write('process.stdin.push(null)\n');
r.write('.exit\n');
Comment on lines +8 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test already passes with current version of Node.js, that means it's failing to replicate the issue at hand.

All fixes must have a test case which demonstrates the defect. The test should
fail before the change, and pass after the change.