Skip to content

Commit ef25033

Browse files
bnoordhuiscodebytere
authored andcommitted
src: fix invalid windowBits=8 gzip segfault
`{ windowBits: 8 }` is legal for deflate streams but not gzip streams. Fix a nullptr dereference when formatting the error message. Bug introduced in commit c34eae5 ("zlib: refactor zlib internals") from September 2018. PR-URL: #33045 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 0a78925 commit ef25033

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/node_zlib.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ enum node_zlib_mode {
111111

112112
struct CompressionError {
113113
CompressionError(const char* message, const char* code, int err)
114-
: message(message), code(code), err(err) {}
114+
: message(message),
115+
code(code),
116+
err(err) {
117+
CHECK_NOT_NULL(message);
118+
}
119+
115120
CompressionError() = default;
116121

117122
const char* message = nullptr;
@@ -997,7 +1002,7 @@ CompressionError ZlibContext::Init(
9971002
if (err_ != Z_OK) {
9981003
dictionary_.clear();
9991004
mode_ = NONE;
1000-
return ErrorForMessage(nullptr);
1005+
return ErrorForMessage("zlib error");
10011006
}
10021007

10031008
return SetDictionary();

test/parallel/test-zlib.js

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const stream = require('stream');
2727
const fs = require('fs');
2828
const fixtures = require('../common/fixtures');
2929

30+
// Should not segfault.
31+
assert.throws(() => zlib.gzipSync(Buffer.alloc(0), { windowBits: 8 }), {
32+
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
33+
name: 'Error',
34+
message: 'Initialization failed',
35+
});
36+
3037
let zlibPairs = [
3138
[zlib.Deflate, zlib.Inflate],
3239
[zlib.Gzip, zlib.Gunzip],

0 commit comments

Comments
 (0)