Skip to content

Commit

Permalink
zlib: fix node crashing on invalid options
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aqrln authored and MylesBorins committed Oct 25, 2017
1 parent 532a294 commit 31bf595
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 12 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,19 @@ function Zlib(opts, mode) {
var strategy = exports.Z_DEFAULT_STRATEGY;
if (typeof opts.strategy === 'number') strategy = opts.strategy;

this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
var windowBits = exports.Z_DEFAULT_WINDOWBITS;
if (opts.windowBits && typeof opts.windowBits === 'number') {
windowBits = opts.windowBits;
}

var memLevel = exports.Z_DEFAULT_MEMLEVEL;
if (opts.memLevel && typeof opts.memLevel === 'number') {
memLevel = opts.memLevel;
}

this._handle.init(windowBits,
level,
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
memLevel,
strategy,
opts.dictionary);

Expand Down
13 changes: 8 additions & 5 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,19 @@ class ZCtx : public AsyncWrap {
CHECK(0 && "wtf?");
}

if (ctx->err_ != Z_OK) {
ZCtx::Error(ctx, "Init error");
}


ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
ctx->dictionary_len_ = dictionary_len;

ctx->write_in_progress_ = false;
ctx->init_done_ = true;

if (ctx->err_ != Z_OK) {
if (dictionary != nullptr) {
delete[] dictionary;
ctx->dictionary_ = nullptr;
}
ctx->env()->ThrowError("Init error");
}
}

static void SetDictionary(ZCtx* ctx) {
Expand Down

0 comments on commit 31bf595

Please sign in to comment.