Skip to content

Commit

Permalink
stream_wrap: error if stream has StringDecoder
Browse files Browse the repository at this point in the history
If `.setEncoding` was called on input stream - all emitted `data` will
be `String`s instances, not `Buffer`s. This is unacceptable for
`StreamWrap`, and should not lead to the crash.

Fix: #3970
PR-URL: #4031
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
indutny authored and Myles Borins committed Dec 15, 2015
1 parent a29d6d3 commit 535855c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/_stream_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');
const util = require('util');
const Socket = require('net').Socket;
const JSStream = process.binding('js_stream').JSStream;
const Buffer = require('buffer').Buffer;
const uv = process.binding('uv');
const debug = util.debuglog('stream_wrap');

Expand Down Expand Up @@ -39,15 +40,24 @@ function StreamWrap(stream) {
};

this.stream.pause();
this.stream.on('error', function(err) {
this.stream.on('error', function onerror(err) {
self.emit('error', err);
});
this.stream.on('data', function(chunk) {
this.stream.on('data', function ondata(chunk) {
if (!(chunk instanceof Buffer)) {
// Make sure that no further `data` events will happen
this.pause();
this.removeListener('data', ondata);

self.emit('error', new Error('Stream has StringDecoder'));
return;
}

debug('data', chunk.length);
if (self._handle)
self._handle.readBuffer(chunk);
});
this.stream.once('end', function() {
this.stream.once('end', function onend() {
debug('end');
if (self._handle)
self._handle.emitEOF();
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-stream-wrap-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const StreamWrap = require('_stream_wrap');
const Duplex = require('stream').Duplex;

const stream = new Duplex({
read: function() {
},
write: function() {
}
});

stream.setEncoding('ascii');

const wrap = new StreamWrap(stream);

wrap.on('error', common.mustCall(function(err) {
assert(/StringDecoder/.test(err.message));
}));

stream.push('ohai');

0 comments on commit 535855c

Please sign in to comment.