Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,15 @@ class CompressionStream : public AsyncWrap,
Close();
}

// Used by the init() bindings: the context (and its buffers) must not be
// (re)initialized while the thread pool is still using it.
bool ThrowIfWriteInProgress() {
if (!write_in_progress_) return false;
env()->ThrowError(
"Cannot initialize zlib stream while a write is in progress");
return true;
}

static void Reset(const FunctionCallbackInfo<Value> &args) {
CompressionStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
Expand Down Expand Up @@ -798,6 +807,11 @@ class ZlibStream final : public CompressionStream<ZlibContext> {

// just pull the ints out of the args and call the other Init
static void Init(const FunctionCallbackInfo<Value>& args) {
ZlibStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());

if (wrap->ThrowIfWriteInProgress()) return;

// Refs: https://github.com/nodejs/node/issues/16649
// Refs: https://github.com/nodejs/node/issues/14161
if (args.Length() == 5) {
Expand All @@ -813,9 +827,6 @@ class ZlibStream final : public CompressionStream<ZlibContext> {
"writeCallback,"
" dictionary[, rejectGarbageAfterEnd])");

ZlibStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());

Local<Context> context = args.GetIsolate()->GetCurrentContext();

// windowBits is special. On the compression side, 0 is an invalid value.
Expand Down Expand Up @@ -914,6 +925,9 @@ class BrotliCompressionStream final :
static void Init(const FunctionCallbackInfo<Value>& args) {
BrotliCompressionStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());

if (wrap->ThrowIfWriteInProgress()) return;

CHECK((args.Length() == 3 || args.Length() == 4) &&
"init(params, writeResult, writeCallback[, dictionary])");

Expand Down Expand Up @@ -996,13 +1010,15 @@ class ZstdStream final : public CompressionStream<CompressionContext> {
}

static void Init(const FunctionCallbackInfo<Value>& args) {
ZstdStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());

if (wrap->ThrowIfWriteInProgress()) return;

CHECK((args.Length() >= 4 && args.Length() <= 6) &&
"init(params, pledgedSrcSize, writeResult, writeCallback[, "
"dictionary[, rejectGarbageAfterEnd]])");

ZstdStream* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());

CHECK(args[2]->IsUint32Array());
CHECK_GE(args[2].As<Uint32Array>()->Length(), 2);
uint32_t* write_result = reinterpret_cast<uint32_t*>(Buffer::Data(args[2]));
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-zlib-init-during-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
require('../common');
const assert = require('assert');
const {
createBrotliCompress,
createBrotliDecompress,
createDeflate,
createZstdCompress,
createZstdDecompress,
} = require('zlib');

// Tests that calling init() on the internal handle while an async write is in
// progress throws an error instead of releasing state still used by the
// thread pool.

const factories = [createBrotliCompress, createBrotliDecompress, createDeflate];
if (typeof createZstdCompress === 'function') {
factories.push(createZstdCompress, createZstdDecompress);
}

for (const factory of factories) {
const stream = factory();
const input = Buffer.alloc(1024, 0x41);

stream.write(input, () => {});
// The decompressors are fed invalid data and are expected to error later.
stream.on('error', () => {});
stream.on('data', () => {});

// The write has been dispatched to the thread pool.
assert.throws(() => {
stream._handle.init();
}, {
message: 'Cannot initialize zlib stream while a write is in progress',
});
}
Loading