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

child_process: fix exec set stdout.setEncoding #18976

Closed
wants to merge 3 commits into from

Conversation

killagu
Copy link
Contributor

@killagu killagu commented Feb 24, 2018

cp.exec decide to use _stdout(_stdout is string) or
Buffer.concat(_stdout)(_stdout is buffer array) by options.encoding.
but std(out|err) encoding can be changed. If encoding is changed to
not null, _stdout will become string, and Buffer.concat(_stdout)
will throw TypeError. This patch will fix it, use
options.encoding and std(out|err)._readableState.encoding.

Fixes: #18969

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

child_process

@nodejs-github-bot nodejs-github-bot added the child_process Issues and PRs related to the child_process subsystem. label Feb 24, 2018

if (process.argv[2] === 'child') {
// The following console calls are part of the test.
console.log(stdoutData);
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about using process.{stdout,stderr}.write()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

process.std.write is ok. But in other test-child-process tests use the console, like encoding,env.

@hstarorg
Copy link

hstarorg commented Feb 25, 2018

The stdoutLen will be affect when encoding changed.
https://github.com/killagu/node/blob/c40252d80610765e5f6efefc02ea6ba1b95cee17/lib/child_process.js#L336-L345
The stdoutLen still used old length calculate function when encoding is changed. The case is that string is Chinese.

@killagu
Copy link
Contributor Author

killagu commented Feb 25, 2018

@hstarorg Thanks for the advice, you are right.I have a better solution.

@@ -311,7 +311,8 @@ Readable.prototype.setEncoding = function(enc) {
if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
this._readableState.decoder = new StringDecoder(enc);
this._readableState.encoding = enc;
// if setEncoding(null), decoder.encoding = 'utf8'
this._readableState.encoding = this._readableState.decoder.encoding;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

const readStream = new ReadableStream();
readStream.setEncoding(null);
// readStream._readableState.decoder.encoding equals 'utf8'
// readStream._readableState.encoding equals null;

_readableState.encoding should be same as this._readableState.decoder.encoding

@BridgeAR
Copy link
Member

BridgeAR commented Apr 9, 2018

@nodejs/streams PTAL

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you please add a test for Readable  alone?

LGTM with that test included.

@BridgeAR
Copy link
Member

Ping @killagu

@BridgeAR
Copy link
Member

@killagu sorry that it took so long for to look at this again, it just slipped through. Would you be so kind and add a test as requested by @mcollina?

@killagu
Copy link
Contributor Author

killagu commented May 2, 2018

@BridgeAR I'm sorry it's taken me so long to get back to you. I'll add the test ASAP.

cp.exec decide to use `_stdout`(_stdout is string) or
`Buffer.concat(_stdout)`(_stdout is buffer array) by options.encoding.
but std(out|err) encoding can be changed. If encoding is changed to
not null, `_stdout` will become string, and `Buffer.concat(_stdout)`
will throw TypeError. This patch will fix it, use
options.encoding and `std(out|err)._readableState.encoding`.

Fixes: nodejs#18969
fix max-buffer
fix _stream_readable.setEncoding
@killagu
Copy link
Contributor Author

killagu commented May 2, 2018

The test has beed added.
@mcollina @BridgeAR

@BridgeAR
Copy link
Member

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 15, 2018
@BridgeAR
Copy link
Member

@nodejs/child_process PTAL

BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 18, 2018
cp.exec decide to use `_stdout`(_stdout is string) or
`Buffer.concat(_stdout)`(_stdout is buffer array) by options.encoding.
but std(out|err) encoding can be changed. If encoding is changed to
not null, `_stdout` will become string, and `Buffer.concat(_stdout)`
will throw TypeError. This patch will fix it, use
options.encoding and `std(out|err)._readableState.encoding`.

PR-URL: nodejs#18976
Fixes: nodejs#18969
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@BridgeAR
Copy link
Member

Landed in 7d81f5d 🎉

@BridgeAR BridgeAR closed this May 18, 2018
MylesBorins pushed a commit that referenced this pull request May 22, 2018
cp.exec decide to use `_stdout`(_stdout is string) or
`Buffer.concat(_stdout)`(_stdout is buffer array) by options.encoding.
but std(out|err) encoding can be changed. If encoding is changed to
not null, `_stdout` will become string, and `Buffer.concat(_stdout)`
will throw TypeError. This patch will fix it, use
options.encoding and `std(out|err)._readableState.encoding`.

PR-URL: #18976
Fixes: #18969
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@addaleax addaleax mentioned this pull request May 22, 2018
@killagu killagu deleted the fix-cp-exec-set-encoding branch July 18, 2018 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. child_process Issues and PRs related to the child_process subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

child_process instance set encoding will trigger exception
6 participants