Skip to content

Commit

Permalink
src: fix invalid windowBits=8 gzip segfault
Browse files Browse the repository at this point in the history
`{ 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]>
  • Loading branch information
bnoordhuis authored and addaleax committed May 9, 2020
1 parent 3226731 commit b8ed2a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -997,7 +1002,7 @@ CompressionError ZlibContext::Init(
if (err_ != Z_OK) {
dictionary_.clear();
mode_ = NONE;
return ErrorForMessage(nullptr);
return ErrorForMessage("zlib error");
}

return SetDictionary();
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ 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_ZLIB_INITIALIZATION_FAILED',
name: 'Error',
message: 'Initialization failed',
});

let zlibPairs = [
[zlib.Deflate, zlib.Inflate],
[zlib.Gzip, zlib.Gunzip],
Expand Down

0 comments on commit b8ed2a0

Please sign in to comment.