Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix invalid windowBits=8 gzip segfault #33045

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,11 @@ function Zlib(opts, mode) {
mode === UNZIP)) {
windowBits = 0;
} else {
// `{ windowBits: 8 }` is valid for deflate but nog gzip.
addaleax marked this conversation as resolved.
Show resolved Hide resolved
const min = Z_MIN_WINDOWBITS + (mode === GZIP);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
windowBits = checkRangesOrGetDefault(
opts.windowBits, 'options.windowBits',
Z_MIN_WINDOWBITS, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS);
}

level = checkRangesOrGetDefault(
Expand Down
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) {
addaleax marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion test/parallel/test-zlib-failed-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-zlib-zero-windowBits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}
8 changes: 8 additions & 0 deletions test/parallel/test-zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down