Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
zlib: finish migrating to internal/errors
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#16540
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
jasnell authored and Qard committed Nov 2, 2017
1 parent af2afc3 commit 69bcc09
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,11 @@ Used when a given value is out of the accepted range.
Used when an attempt is made to use a `zlib` object after it has already been
closed.

<a id="ERR_ZLIB_INITIALIZATION_FAILED"></a>
### ERR_ZLIB_INITIALIZATION_FAILED

Used when creation of a [`zlib`][] object fails due to incorrect configuration.

[`--force-fips`]: cli.html#cli_force_fips
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
Expand Down Expand Up @@ -1515,3 +1520,4 @@ closed.
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
[vm]: vm.html
[WHATWG Supported Encodings]: util.html#util_whatwg_supported_encodings
[`zlib`]: zlib.html
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ E('ERR_WORKER_UNSERIALIZABLE_ERROR',
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
'The worker script extension must be ".js" or ".mjs". Received "%s"');
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed');

function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required');
Expand Down
17 changes: 15 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ function Zlib(opts, mode) {
var memLevel = Z_DEFAULT_MEMLEVEL;
var strategy = Z_DEFAULT_STRATEGY;
var dictionary;

if (typeof mode !== 'number')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
if (mode < DEFLATE || mode > UNZIP)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');

if (opts) {
chunkSize = opts.chunkSize;
if (chunkSize !== undefined && chunkSize === chunkSize) {
Expand Down Expand Up @@ -258,8 +264,15 @@ function Zlib(opts, mode) {
this._hadError = false;
this._writeState = new Uint32Array(2);

this._handle.init(windowBits, level, memLevel, strategy, this._writeState,
processCallback, dictionary);
if (!this._handle.init(windowBits,
level,
memLevel,
strategy,
this._writeState,
processCallback,
dictionary)) {
throw new errors.Error('ERR_ZLIB_INITIALIZATION_FAILED');
}

this._outBuffer = Buffer.allocUnsafe(chunkSize);
this._outOffset = 0;
Expand Down
24 changes: 11 additions & 13 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,8 @@ class ZCtx : public AsyncWrap {

static void New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1 || !args[0]->IsInt32()) {
return env->ThrowTypeError("Bad argument");
}
CHECK(args[0]->IsInt32());
node_zlib_mode mode = static_cast<node_zlib_mode>(args[0]->Int32Value());

if (mode < DEFLATE || mode > UNZIP) {
return env->ThrowTypeError("Bad argument");
}

new ZCtx(env, args.This(), mode);
}

Expand Down Expand Up @@ -483,9 +475,14 @@ class ZCtx : public AsyncWrap {
memcpy(dictionary, dictionary_, dictionary_len);
}

Init(ctx, level, windowBits, memLevel, strategy, write_result,
write_js_callback, dictionary, dictionary_len);
bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result,
write_js_callback, dictionary, dictionary_len);
if (!ret) goto end;

SetDictionary(ctx);

end:
return args.GetReturnValue().Set(ret);
}

static void Params(const FunctionCallbackInfo<Value>& args) {
Expand All @@ -502,7 +499,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, uint32_t* write_result,
Local<Function> write_js_callback, char* dictionary,
size_t dictionary_len) {
Expand Down Expand Up @@ -568,11 +565,12 @@ class ZCtx : public AsyncWrap {
ctx->dictionary_ = nullptr;
}
ctx->mode_ = NONE;
ctx->env()->ThrowError("Init error");
return false;
}

ctx->write_result_ = write_result;
ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback);
return true;
}

static void SetDictionary(ZCtx* ctx) {
Expand Down
10 changes: 7 additions & 3 deletions test/parallel/test-zlib-failed-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const zlib = require('zlib');
// no such rejection which is the reason for the version check below
// (http://zlib.net/ChangeLog.txt).
if (!/^1\.2\.[0-8]$/.test(process.versions.zlib)) {
assert.throws(() => {
zlib.createDeflateRaw({ windowBits: 8 });
}, /^Error: Init error$/);
common.expectsError(
() => zlib.createDeflateRaw({ windowBits: 8 }),
{
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
type: Error,
message: 'Initialization failed'
});
}

// Regression tests for bugs in the validation logic.
Expand Down

0 comments on commit 69bcc09

Please sign in to comment.