-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
zlib: fix node crashing on invalid options #13098
Changes from 1 commit
7d78533
c5440af
c5f18b1
e5c39bd
7b39725
f91e191
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,14 @@ function zlibOnError(message, errno) { | |
var error = new Error(message); | ||
error.errno = errno; | ||
error.code = codes[errno]; | ||
this.emit('error', error); | ||
|
||
if (this._initSuccess) { | ||
this.emit('error', error); | ||
} else { | ||
process.nextTick(() => { | ||
this.emit('error', error); | ||
}); | ||
} | ||
} | ||
|
||
function flushCallback(level, strategy, callback) { | ||
|
@@ -222,20 +229,32 @@ class Zlib extends Transform { | |
this._handle = new binding.Zlib(mode); | ||
this._handle.onerror = zlibOnError.bind(this); | ||
this._hadError = false; | ||
this._initSuccess = false; | ||
|
||
var level = constants.Z_DEFAULT_COMPRESSION; | ||
if (typeof opts.level === 'number') level = opts.level; | ||
|
||
var strategy = constants.Z_DEFAULT_STRATEGY; | ||
if (typeof opts.strategy === 'number') strategy = opts.strategy; | ||
|
||
this._handle.init(opts.windowBits || constants.Z_DEFAULT_WINDOWBITS, | ||
level, | ||
opts.memLevel || constants.Z_DEFAULT_MEMLEVEL, | ||
strategy, | ||
opts.dictionary); | ||
var windowBits = constants.Z_DEFAULT_WINDOWBITS; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't these both just be ternaries? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They could, and I'd like them to be, but I just wrote these in a style consistent with the code above. How about me addressing this comment in a follow-up PR, together with some more refactoring like replacing most There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
if (typeof opts.windowBits === 'number') windowBits = opts.windowBits; | ||
|
||
var memLevel = constants.Z_DEFAULT_MEMLEVEL; | ||
if (typeof opts.memLevel === 'number') memLevel = opts.memLevel; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change alters the existing behaviour a little. If we pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch, thanks. It uncovers a whole bunch of bugs in the validation logic above, i.e., I wanted to say that zero would be caught before this line, but after trying it out it appeared to be not. And it was also possible to pass |
||
|
||
this._initSuccess = this._handle.init(windowBits, | ||
level, | ||
memLevel, | ||
strategy, | ||
opts.dictionary); | ||
|
||
if (this._initSuccess) { | ||
this._buffer = Buffer.allocUnsafe(this._chunkSize); | ||
} else { | ||
this._buffer = null; | ||
} | ||
|
||
this._buffer = Buffer.allocUnsafe(this._chunkSize); | ||
this._offset = 0; | ||
this._level = level; | ||
this._strategy = strategy; | ||
|
@@ -467,7 +486,9 @@ function _close(engine, callback) { | |
if (!engine._handle) | ||
return; | ||
|
||
engine._handle.close(); | ||
if (engine._initSuccess) | ||
engine._handle.close(); | ||
|
||
engine._handle = null; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
namespace node { | ||
|
||
using v8::Array; | ||
using v8::Boolean; | ||
using v8::Context; | ||
using v8::FunctionCallbackInfo; | ||
using v8::FunctionTemplate; | ||
|
@@ -480,9 +481,11 @@ class ZCtx : public AsyncWrap { | |
memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len); | ||
} | ||
|
||
Init(ctx, level, windowBits, memLevel, strategy, | ||
dictionary, dictionary_len); | ||
bool result = Init(ctx, level, windowBits, memLevel, strategy, | ||
dictionary, dictionary_len); | ||
SetDictionary(ctx); | ||
|
||
args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), result)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m pretty sure you don’t need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, didn't know that :) |
||
} | ||
|
||
static void Params(const FunctionCallbackInfo<Value>& args) { | ||
|
@@ -499,7 +502,7 @@ class ZCtx : public AsyncWrap { | |
SetDictionary(ctx); | ||
} | ||
|
||
static void Init(ZCtx *ctx, int level, int windowBits, int memLevel, | ||
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel, | ||
int strategy, char* dictionary, size_t dictionary_len) { | ||
ctx->level_ = level; | ||
ctx->windowBits_ = windowBits; | ||
|
@@ -553,6 +556,9 @@ class ZCtx : public AsyncWrap { | |
|
||
if (ctx->err_ != Z_OK) { | ||
ZCtx::Error(ctx, "Init error"); | ||
if (dictionary != nullptr) | ||
delete[] dictionary; | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d have to admit I prefer the solution suggested by @cjihrig in #13082 (comment) (or just move this error-checking block to the end of the |
||
} | ||
|
||
|
||
|
@@ -561,6 +567,8 @@ class ZCtx : public AsyncWrap { | |
|
||
ctx->write_in_progress_ = false; | ||
ctx->init_done_ = true; | ||
|
||
return true; | ||
} | ||
|
||
static void SetDictionary(ZCtx* ctx) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
// For raw deflate or gzip encoding, a request for a 256-byte window is | ||
// rejected as invalid, since only zlib headers provide means of transmitting | ||
// the window size to the decompressor. | ||
// (http://zlib.net/manual.html#Advanced) | ||
const deflate = zlib.createDeflateRaw({ windowBits: 8 }); | ||
deflate.on('error', common.mustCall((error) => { | ||
assert.ok(/Init error/.test(error)); | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this error should still be thrown synchronously, if there is one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'll make it impossible to catch this error since there's on object returned to userland code yet to listen to the
error
event on. That's basically the reason for all this machinery, otherwise @cjihrig's one-line fix would be just fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could change the
ZCtx::Error(ctx, "Init error");
line to throw an error instead … that should work, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... yes, it should. And there would be no need for all those changes in
zlib.js
then.