diff --git a/lib/zlib.js b/lib/zlib.js index 502ee61aa3bd9d..be8f9401a310d0 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -613,9 +613,11 @@ function Zlib(opts, mode) { mode === UNZIP)) { windowBits = 0; } else { + // `{ windowBits: 8 }` is valid for deflate but not gzip. + const min = Z_MIN_WINDOWBITS + (mode === GZIP ? 1 : 0); windowBits = checkRangesOrGetDefault( opts.windowBits, 'options.windowBits', - Z_MIN_WINDOWBITS, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS); + min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS); } level = checkRangesOrGetDefault( diff --git a/src/node_zlib.cc b/src/node_zlib.cc index eacd710143a1b2..83698bd5192e4b 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -111,7 +111,12 @@ enum node_zlib_mode { struct CompressionError { CompressionError(const char* message, const char* code, int err) - : message(message), code(code), err(err) {} + : message(message), + code(code), + err(err) { + CHECK_NOT_NULL(message); + } + CompressionError() = default; const char* message = nullptr; @@ -997,7 +1002,7 @@ CompressionError ZlibContext::Init( if (err_ != Z_OK) { dictionary_.clear(); mode_ = NONE; - return ErrorForMessage(nullptr); + return ErrorForMessage("zlib error"); } return SetDictionary(); diff --git a/test/parallel/test-zlib-failed-init.js b/test/parallel/test-zlib-failed-init.js index 1f99a378fc6eda..95f401a3718f30 100644 --- a/test/parallel/test-zlib-failed-init.js +++ b/test/parallel/test-zlib-failed-init.js @@ -21,7 +21,7 @@ assert.throws( code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "options.windowBits" is out of range. It must ' + - 'be >= 8 and <= 15. Received 0' + 'be >= 9 and <= 15. Received 0' } ); diff --git a/test/parallel/test-zlib-zero-windowBits.js b/test/parallel/test-zlib-zero-windowBits.js index 730690a07c423f..a27fd6734a5425 100644 --- a/test/parallel/test-zlib-zero-windowBits.js +++ b/test/parallel/test-zlib-zero-windowBits.js @@ -28,6 +28,6 @@ const zlib = require('zlib'); code: 'ERR_OUT_OF_RANGE', name: 'RangeError', message: 'The value of "options.windowBits" is out of range. ' + - 'It must be >= 8 and <= 15. Received 0' + 'It must be >= 9 and <= 15. Received 0' }); } diff --git a/test/parallel/test-zlib.js b/test/parallel/test-zlib.js index 0d21cf6cc29e58..65050b85a036cf 100644 --- a/test/parallel/test-zlib.js +++ b/test/parallel/test-zlib.js @@ -27,6 +27,14 @@ const stream = require('stream'); const fs = require('fs'); const fixtures = require('../common/fixtures'); +// Should not segfault. +assert.throws(() => zlib.gzipSync(Buffer.alloc(0), { windowBits: 8 }), { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "options.windowBits" is out of range. ' + + 'It must be >= 9 and <= 15. Received 8', +}); + let zlibPairs = [ [zlib.Deflate, zlib.Inflate], [zlib.Gzip, zlib.Gunzip],