Skip to content

Commit 2a8ef7a

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 <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 273e02d commit 2a8ef7a

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

doc/api/zlib.md

+4
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ added: v0.5.8
373373

374374
Returns a new [DeflateRaw][] object with an [options][].
375375

376+
*Note*: The zlib library rejects requests for 256-byte windows (i.e.,
377+
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating a
378+
[DeflateRaw][] object with this specific value of the `windowBits` option.
379+
376380
## zlib.createGunzip([options])
377381
<!-- YAML
378382
added: v0.5.8

lib/zlib.js

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

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

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) {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const zlib = require('zlib');
7+
8+
// For raw deflate encoding, requests for 256-byte windows are rejected as
9+
// invalid by zlib.
10+
// (http://zlib.net/manual.html#Advanced)
11+
assert.throws(() => {
12+
zlib.createDeflateRaw({ windowBits: 8 });
13+
}, /^Error: Init error$/);

0 commit comments

Comments
 (0)