Skip to content

Commit 896eaf6

Browse files
committed
zlib: finish migrating to internal/errors
PR-URL: #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]>
1 parent 1cdcab0 commit 896eaf6

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,11 @@ Used when a given value is out of the accepted range.
14741474
Used when an attempt is made to use a `zlib` object after it has already been
14751475
closed.
14761476

1477+
<a id="ERR_ZLIB_INITIALIZATION_FAILED"></a>
1478+
### ERR_ZLIB_INITIALIZATION_FAILED
1479+
1480+
Used when creation of a [`zlib`][] object fails due to incorrect configuration.
1481+
14771482
[`--force-fips`]: cli.html#cli_force_fips
14781483
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
14791484
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
@@ -1515,3 +1520,4 @@ closed.
15151520
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
15161521
[vm]: vm.html
15171522
[WHATWG Supported Encodings]: util.html#util_whatwg_supported_encodings
1523+
[`zlib`]: zlib.html

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => {
364364
return `The value of "${start}" must be ${end}. Received "${value}"`;
365365
});
366366
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
367+
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed');
367368

368369
function invalidArgType(name, expected, actual) {
369370
internalAssert(name, 'name is required');

lib/zlib.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ function Zlib(opts, mode) {
161161
var memLevel = Z_DEFAULT_MEMLEVEL;
162162
var strategy = Z_DEFAULT_STRATEGY;
163163
var dictionary;
164+
165+
if (typeof mode !== 'number')
166+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'mode', 'number');
167+
if (mode < DEFLATE || mode > UNZIP)
168+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
169+
164170
if (opts) {
165171
chunkSize = opts.chunkSize;
166172
if (chunkSize !== undefined && chunkSize === chunkSize) {
@@ -258,8 +264,15 @@ function Zlib(opts, mode) {
258264
this._hadError = false;
259265
this._writeState = new Uint32Array(2);
260266

261-
this._handle.init(windowBits, level, memLevel, strategy, this._writeState,
262-
processCallback, dictionary);
267+
if (!this._handle.init(windowBits,
268+
level,
269+
memLevel,
270+
strategy,
271+
this._writeState,
272+
processCallback,
273+
dictionary)) {
274+
throw new errors.Error('ERR_ZLIB_INITIALIZATION_FAILED');
275+
}
263276

264277
this._outBuffer = Buffer.allocUnsafe(chunkSize);
265278
this._outOffset = 0;

src/node_zlib.cc

+11-13
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,8 @@ class ZCtx : public AsyncWrap {
418418

419419
static void New(const FunctionCallbackInfo<Value>& args) {
420420
Environment* env = Environment::GetCurrent(args);
421-
422-
if (args.Length() < 1 || !args[0]->IsInt32()) {
423-
return env->ThrowTypeError("Bad argument");
424-
}
421+
CHECK(args[0]->IsInt32());
425422
node_zlib_mode mode = static_cast<node_zlib_mode>(args[0]->Int32Value());
426-
427-
if (mode < DEFLATE || mode > UNZIP) {
428-
return env->ThrowTypeError("Bad argument");
429-
}
430-
431423
new ZCtx(env, args.This(), mode);
432424
}
433425

@@ -476,9 +468,14 @@ class ZCtx : public AsyncWrap {
476468
memcpy(dictionary, dictionary_, dictionary_len);
477469
}
478470

479-
Init(ctx, level, windowBits, memLevel, strategy, write_result,
480-
write_js_callback, dictionary, dictionary_len);
471+
bool ret = Init(ctx, level, windowBits, memLevel, strategy, write_result,
472+
write_js_callback, dictionary, dictionary_len);
473+
if (!ret) goto end;
474+
481475
SetDictionary(ctx);
476+
477+
end:
478+
return args.GetReturnValue().Set(ret);
482479
}
483480

484481
static void Params(const FunctionCallbackInfo<Value>& args) {
@@ -495,7 +492,7 @@ class ZCtx : public AsyncWrap {
495492
SetDictionary(ctx);
496493
}
497494

498-
static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
495+
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
499496
int strategy, uint32_t* write_result,
500497
Local<Function> write_js_callback, char* dictionary,
501498
size_t dictionary_len) {
@@ -561,11 +558,12 @@ class ZCtx : public AsyncWrap {
561558
ctx->dictionary_ = nullptr;
562559
}
563560
ctx->mode_ = NONE;
564-
ctx->env()->ThrowError("Init error");
561+
return false;
565562
}
566563

567564
ctx->write_result_ = write_result;
568565
ctx->write_js_callback_.Reset(ctx->env()->isolate(), write_js_callback);
566+
return true;
569567
}
570568

571569
static void SetDictionary(ZCtx* ctx) {

test/parallel/test-zlib-failed-init.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const zlib = require('zlib');
1111
// no such rejection which is the reason for the version check below
1212
// (http://zlib.net/ChangeLog.txt).
1313
if (!/^1\.2\.[0-8]$/.test(process.versions.zlib)) {
14-
assert.throws(() => {
15-
zlib.createDeflateRaw({ windowBits: 8 });
16-
}, /^Error: Init error$/);
14+
common.expectsError(
15+
() => zlib.createDeflateRaw({ windowBits: 8 }),
16+
{
17+
code: 'ERR_ZLIB_INITIALIZATION_FAILED',
18+
type: Error,
19+
message: 'Initialization failed'
20+
});
1721
}
1822

1923
// Regression tests for bugs in the validation logic.

0 commit comments

Comments
 (0)