Skip to content

Commit 8f845ba

Browse files
indutnyrvagg
authored andcommitted
stream_wrap: error if stream has StringDecoder
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]>
1 parent e84aeec commit 8f845ba

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/_stream_wrap.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('assert');
44
const util = require('util');
55
const Socket = require('net').Socket;
66
const JSStream = process.binding('js_stream').JSStream;
7+
const Buffer = require('buffer').Buffer;
78
const uv = process.binding('uv');
89
const debug = util.debuglog('stream_wrap');
910

@@ -39,15 +40,24 @@ function StreamWrap(stream) {
3940
};
4041

4142
this.stream.pause();
42-
this.stream.on('error', function(err) {
43+
this.stream.on('error', function onerror(err) {
4344
self.emit('error', err);
4445
});
45-
this.stream.on('data', function(chunk) {
46+
this.stream.on('data', function ondata(chunk) {
47+
if (!(chunk instanceof Buffer)) {
48+
// Make sure that no further `data` events will happen
49+
this.pause();
50+
this.removeListener('data', ondata);
51+
52+
self.emit('error', new Error('Stream has StringDecoder'));
53+
return;
54+
}
55+
4656
debug('data', chunk.length);
4757
if (self._handle)
4858
self._handle.readBuffer(chunk);
4959
});
50-
this.stream.once('end', function() {
60+
this.stream.once('end', function onend() {
5161
debug('end');
5262
if (self._handle)
5363
self._handle.emitEOF();
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const StreamWrap = require('_stream_wrap');
6+
const Duplex = require('stream').Duplex;
7+
8+
const stream = new Duplex({
9+
read: function() {
10+
},
11+
write: function() {
12+
}
13+
});
14+
15+
stream.setEncoding('ascii');
16+
17+
const wrap = new StreamWrap(stream);
18+
19+
wrap.on('error', common.mustCall(function(err) {
20+
assert(/StringDecoder/.test(err.message));
21+
}));
22+
23+
stream.push('ohai');

0 commit comments

Comments
 (0)