Skip to content

Commit 381f4b1

Browse files
Renegade334aduh95
authored andcommitted
stream: disallow writing string chunk with 'buffer' encoding
Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk> PR-URL: #63062 Refs: #33075 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent cbee0de commit 381f4b1

4 files changed

Lines changed: 19 additions & 30 deletions

File tree

‎lib/internal/streams/writable.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ function _write(stream, chunk, encoding, cb) {
466466
}
467467

468468
if(typeofchunk==='string'){
469+
if(encoding==='buffer'){
470+
thrownewERR_UNKNOWN_ENCODING(encoding);
471+
}
469472
if((state[kState]&kDecodeStrings)!==0){
470473
chunk=Buffer.from(chunk,encoding);
471474
encoding='buffer';

‎src/stream_base.cc‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,10 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
296296

297297
intStreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
298298
CHECK(args[0]->IsObject());
299+
CHECK(args[1]->IsUint8Array());
299300

300301
Environment* env = Environment::GetCurrent(args);
301302

302-
if (!args[1]->IsUint8Array()) {
303-
node::THROW_ERR_INVALID_ARG_TYPE(env, "Second argument must be a buffer");
304-
return0;
305-
}
306-
307303
Local<Object> req_wrap_obj = args[0].As<Object>();
308304
uv_buf_t buf;
309305
buf.base = Buffer::Data(args[1]);

‎test/parallel/test-stream-base-typechecking.js‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎test/parallel/test-stream-writable-write-error.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function test(autoDestroy) {
3131
{
3232
constw=newWritable({
3333
autoDestroy,
34-
_write(){}
34+
write(){}
3535
});
3636
w.end();
3737
expectError(w,['asd'],'ERR_STREAM_WRITE_AFTER_END');
@@ -40,34 +40,42 @@ function test(autoDestroy) {
4040
{
4141
constw=newWritable({
4242
autoDestroy,
43-
_write(){}
43+
write(){}
4444
});
4545
w.destroy();
4646
}
4747

4848
{
4949
constw=newWritable({
5050
autoDestroy,
51-
_write(){}
51+
write(){}
5252
});
5353
expectError(w,[null],'ERR_STREAM_NULL_VALUES',true);
5454
}
5555

5656
{
5757
constw=newWritable({
5858
autoDestroy,
59-
_write(){}
59+
write(){}
6060
});
6161
expectError(w,[{}],'ERR_INVALID_ARG_TYPE',true);
6262
}
6363

6464
{
6565
constw=newWritable({
66-
decodeStrings: false,
6766
autoDestroy,
68-
_write(){}
67+
write(){}
68+
});
69+
expectError(w,['asd','buffer'],'ERR_UNKNOWN_ENCODING',true);
70+
}
71+
72+
{
73+
constw=newWritable({
74+
autoDestroy,
75+
decodeStrings: false,
76+
write(){}
6977
});
70-
expectError(w,['asd','noencoding'],'ERR_UNKNOWN_ENCODING',true);
78+
expectError(w,['asd','buffer'],'ERR_UNKNOWN_ENCODING',true);
7179
}
7280
}
7381

0 commit comments

Comments
 (0)