Skip to content

Commit 31bf595

Browse files
aqrlnMylesBorins
authored andcommitted
zlib: fix node crashing on invalid options
This is a partial backport of semver-patch bits of 9e4660b. This commit fixes the Node process crashing when constructors of classes of the zlib module are given invalid options. * Throw an Error when the zlib library rejects the value of windowBits, instead of crashing with an assertion. * Treat windowBits and memLevel options consistently with other ones and don't crash when non-numeric values are given. Backport-PR-URL: #14860 PR-URL: #13098 Fixes: #13082 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 532a294 commit 31bf595

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/zlib.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,19 @@ function Zlib(opts, mode) {
373373
var strategy = exports.Z_DEFAULT_STRATEGY;
374374
if (typeof opts.strategy === 'number') strategy = opts.strategy;
375375

376-
this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
376+
var windowBits = exports.Z_DEFAULT_WINDOWBITS;
377+
if (opts.windowBits && typeof opts.windowBits === 'number') {
378+
windowBits = opts.windowBits;
379+
}
380+
381+
var memLevel = exports.Z_DEFAULT_MEMLEVEL;
382+
if (opts.memLevel && typeof opts.memLevel === 'number') {
383+
memLevel = opts.memLevel;
384+
}
385+
386+
this._handle.init(windowBits,
377387
level,
378-
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
388+
memLevel,
379389
strategy,
380390
opts.dictionary);
381391

src/node_zlib.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,19 @@ class ZCtx : public AsyncWrap {
469469
CHECK(0 && "wtf?");
470470
}
471471

472-
if (ctx->err_ != Z_OK) {
473-
ZCtx::Error(ctx, "Init error");
474-
}
475-
476-
477472
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
478473
ctx->dictionary_len_ = dictionary_len;
479474

480475
ctx->write_in_progress_ = false;
481476
ctx->init_done_ = true;
477+
478+
if (ctx->err_ != Z_OK) {
479+
if (dictionary != nullptr) {
480+
delete[] dictionary;
481+
ctx->dictionary_ = nullptr;
482+
}
483+
ctx->env()->ThrowError("Init error");
484+
}
482485
}
483486

484487
static void SetDictionary(ZCtx* ctx) {

0 commit comments

Comments
 (0)