From 30383ffb9afc447f5c1536110cf81abd9b642c81 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 1 Sep 2024 12:49:20 -0400 Subject: [PATCH] zlib: throw brotli initialization error from c++ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/54698 Reviewed-By: Matteo Collina Reviewed-By: Gerhard Stöbich Reviewed-By: James M Snell --- lib/internal/errors.js | 1 - lib/zlib.js | 10 +--------- src/node_errors.h | 1 + src/node_zlib.cc | 14 ++++++++++---- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 64e95ffa603208..c03e285607c9cf 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1926,4 +1926,3 @@ E('ERR_WORKER_UNSERIALIZABLE_ERROR', 'Serializing an uncaught exception failed', Error); E('ERR_WORKER_UNSUPPORTED_OPERATION', '%s is not supported in workers', TypeError); -E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error); diff --git a/lib/zlib.js b/lib/zlib.js index a2c092f1037261..2fc3b88d72cf49 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -47,7 +47,6 @@ const { ERR_BUFFER_TOO_LARGE, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE, - ERR_ZLIB_INITIALIZATION_FAILED, }, genericNodeError, } = require('internal/errors'); @@ -816,14 +815,7 @@ function Brotli(opts, mode) { new binding.BrotliDecoder(mode) : new binding.BrotliEncoder(mode); this._writeState = new Uint32Array(2); - // TODO(addaleax): Sometimes we generate better error codes in C++ land, - // e.g. ERR_BROTLI_PARAM_SET_FAILED -- it's hard to access them with - // the current bindings setup, though. - if (!handle.init(brotliInitParamsArray, - this._writeState, - processCallback)) { - throw new ERR_ZLIB_INITIALIZATION_FAILED(); - } + handle.init(brotliInitParamsArray, this._writeState, processCallback); ReflectApply(ZlibBase, this, [opts, mode, handle, brotliDefaultOpts]); } diff --git a/src/node_errors.h b/src/node_errors.h index 1662491bac4431..ee99e2caf3c5f9 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -101,6 +101,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); V(ERR_VM_MODULE_CACHED_DATA_REJECTED, Error) \ V(ERR_VM_MODULE_LINK_FAILURE, Error) \ V(ERR_WASI_NOT_STARTED, Error) \ + V(ERR_ZLIB_INITIALIZATION_FAILED, Error) \ V(ERR_WORKER_INIT_FAILED, Error) \ V(ERR_PROTO_ACCESS, Error) diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 3ff3ac13112330..66370e4165979c 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -25,6 +25,7 @@ #include "async_wrap-inl.h" #include "env-inl.h" +#include "node_errors.h" #include "node_external_reference.h" #include "threadpoolwork-inl.h" #include "util-inl.h" @@ -271,6 +272,8 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { CHECK_EQ(unreported_allocations_, 0); } + Environment* env() const { return this->ThreadPoolWork::env(); } + void Close() { if (write_in_progress_) { pending_close_ = true; @@ -694,7 +697,11 @@ class BrotliCompressionStream final : static_cast*>(wrap)); if (err.IsError()) { wrap->EmitError(err); - args.GetReturnValue().Set(false); + // TODO(addaleax): Sometimes we generate better error codes in C++ land, + // e.g. ERR_BROTLI_PARAM_SET_FAILED -- it's hard to access them with + // the current bindings setup, though. + THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(), + "Initialization failed"); return; } @@ -708,12 +715,11 @@ class BrotliCompressionStream final : err = wrap->context()->SetParams(i, data[i]); if (err.IsError()) { wrap->EmitError(err); - args.GetReturnValue().Set(false); + THROW_ERR_ZLIB_INITIALIZATION_FAILED(wrap->env(), + "Initialization failed"); return; } } - - args.GetReturnValue().Set(true); } static void Params(const FunctionCallbackInfo& args) {