Skip to content

Commit 44843a6

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 instead of 'ascii' because 'ascii' mutilates non-ASCII input. Correctly handle partial character sequences by introducing a StringDecoder. Sending over UTF-8 no longer works in v0.10 because the high bit of each byte is now cleared when converting a Buffer to ASCII. See commit 96a314b for details. Fixes #4999 and #5011.
1 parent 05bd6b7 commit 44843a6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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 dgram = require('dgram');
@@ -321,11 +322,12 @@ function handleMessage(target, message, handle) {
321322
function setupChannel(target, channel) {
322323
target._channel = channel;
323324

325+
var decoder = new StringDecoder('utf8');
324326
var jsonBuffer = '';
325327
channel.buffering = false;
326328
channel.onread = function(pool, offset, length, recvHandle) {
327329
if (pool) {
328-
jsonBuffer += pool.toString('ascii', offset, offset + length);
330+
jsonBuffer += decoder.write(pool.slice(offset, offset + length));
329331

330332
var i, start = 0;
331333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}));
34+
}

0 commit comments

Comments
 (0)