Skip to content

Commit 84bb0ec

Browse files
committed
child_process: fix sending utf-8 to child process
In process#send() and child_process.ChildProcess#send(), use 'utf8' as the encoding and correctly handle partial character sequences by introducing a StringDecoder. Before this commit, it used 'ascii' and partial sequences were dropped or corrupted. This is a back-port of commit 44843a6 from the v0.10 branch. Fixes #4999 and #5011.
1 parent 2c41a80 commit 84bb0ec

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/child_process.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
var StringDecoder = require('string_decoder').StringDecoder;
2223
var EventEmitter = require('events').EventEmitter;
2324
var net = require('net');
2425
var Process = process.binding('process_wrap').Process;
@@ -277,11 +278,12 @@ function handleMessage(target, message, handle) {
277278
function setupChannel(target, channel) {
278279
target._channel = channel;
279280

281+
var decoder = new StringDecoder('utf8');
280282
var jsonBuffer = '';
281283
channel.buffering = false;
282284
channel.onread = function(pool, offset, length, recvHandle) {
283285
if (pool) {
284-
jsonBuffer += pool.toString('ascii', offset, offset + length);
286+
jsonBuffer += decoder.write(pool.slice(offset, offset + length));
285287

286288
var i, start = 0;
287289

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var fork = require('child_process').fork;
25+
26+
var expected = Array(1e5).join('ßßßß');
27+
if (process.argv[2] === 'child') {
28+
process.send(expected);
29+
} else {
30+
var child = fork(process.argv[1], ['child']);
31+
child.on('message', common.mustCall(function(actual) {
32+
assert.equal(actual, expected);
33+
child.kill();
34+
}));
35+
}

0 commit comments

Comments
 (0)