Skip to content

Commit 8250c43

Browse files
aqrlngibfahn
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. PR-URL: #13098 Backport-PR-URL: #13201 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 a61e89d commit 8250c43

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
@@ -408,6 +408,10 @@ added: v0.5.8
408408

409409
Returns a new [DeflateRaw][] object with an [options][].
410410

411+
*Note*: The zlib library rejects requests for 256-byte windows (i.e.,
412+
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating a
413+
[DeflateRaw][] object with this specific value of the `windowBits` option.
414+
411415
## zlib.createGunzip([options])
412416
<!-- YAML
413417
added: v0.5.8

lib/zlib.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,19 @@ function Zlib(opts, mode) {
379379
var strategy = exports.Z_DEFAULT_STRATEGY;
380380
if (typeof opts.strategy === 'number') strategy = opts.strategy;
381381

382-
this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
382+
var windowBits = exports.Z_DEFAULT_WINDOWBITS;
383+
if (opts.windowBits && typeof opts.windowBits === 'number') {
384+
windowBits = opts.windowBits;
385+
}
386+
387+
var memLevel = exports.Z_DEFAULT_MEMLEVEL;
388+
if (opts.memLevel && typeof opts.memLevel === 'number') {
389+
memLevel = opts.memLevel;
390+
}
391+
392+
this._handle.init(windowBits,
383393
level,
384-
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
394+
memLevel,
385395
strategy,
386396
opts.dictionary);
387397

src/node_zlib.cc

+8-5
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,19 @@ class ZCtx : public AsyncWrap {
530530
CHECK(0 && "wtf?");
531531
}
532532

533-
if (ctx->err_ != Z_OK) {
534-
ZCtx::Error(ctx, "Init error");
535-
}
536-
537-
538533
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
539534
ctx->dictionary_len_ = dictionary_len;
540535

541536
ctx->write_in_progress_ = false;
542537
ctx->init_done_ = true;
538+
539+
if (ctx->err_ != Z_OK) {
540+
if (dictionary != nullptr) {
541+
delete[] dictionary;
542+
ctx->dictionary_ = nullptr;
543+
}
544+
ctx->env()->ThrowError("Init error");
545+
}
543546
}
544547

545548
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)