From d074bcd6f5ecb5ae49091cd4ee223f71f03c05d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 24 Aug 2024 16:45:11 +0200 Subject: [PATCH] src: handle errors correctly in `permission.cc` Return an empty `MaybeLocal` to indicate that an exception is pending. PR-URL: https://github.com/nodejs/node/pull/54541 Reviewed-By: Anna Henningsen Reviewed-By: Rafael Gonzaga Reviewed-By: James M Snell Reviewed-By: Mohammed Keyvanzadeh --- src/permission/permission.cc | 49 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/permission/permission.cc b/src/permission/permission.cc index ad393df0e38976..7c84aa044dbbf5 100644 --- a/src/permission/permission.cc +++ b/src/permission/permission.cc @@ -18,6 +18,8 @@ namespace node { using v8::Context; using v8::FunctionCallbackInfo; using v8::Local; +using v8::MaybeLocal; +using v8::NewStringType; using v8::Object; using v8::String; using v8::Value; @@ -105,46 +107,43 @@ Permission::Permission() : enabled_(false) { #undef V } -Local CreateAccessDeniedError(Environment* env, - PermissionScope perm, - const std::string_view& res) { - Local err = ERR_ACCESS_DENIED(env->isolate()); - CHECK(err->IsObject()); - if (err.As() - ->Set(env->context(), - env->permission_string(), - v8::String::NewFromUtf8(env->isolate(), - Permission::PermissionToString(perm), - v8::NewStringType::kNormal) - .ToLocalChecked()) +MaybeLocal CreateAccessDeniedError(Environment* env, + PermissionScope perm, + const std::string_view& res) { + Local err = ERR_ACCESS_DENIED(env->isolate()); + Local perm_string; + Local resource_string; + if (!String::NewFromUtf8(env->isolate(), + Permission::PermissionToString(perm), + NewStringType::kNormal) + .ToLocal(&perm_string) || + !String::NewFromUtf8( + env->isolate(), std::string(res).c_str(), NewStringType::kNormal) + .ToLocal(&resource_string) || + err->Set(env->context(), env->permission_string(), perm_string) .IsNothing() || - err.As() - ->Set(env->context(), - env->resource_string(), - v8::String::NewFromUtf8(env->isolate(), - std::string(res).c_str(), - v8::NewStringType::kNormal) - .ToLocalChecked()) - .IsNothing()) - return Local(); + err->Set(env->context(), env->resource_string(), resource_string) + .IsNothing()) { + return MaybeLocal(); + } return err; } void Permission::ThrowAccessDenied(Environment* env, PermissionScope perm, const std::string_view& res) { - Local err = CreateAccessDeniedError(env, perm, res); + MaybeLocal err = CreateAccessDeniedError(env, perm, res); if (err.IsEmpty()) return; - env->isolate()->ThrowException(err); + env->isolate()->ThrowException(err.ToLocalChecked()); } void Permission::AsyncThrowAccessDenied(Environment* env, fs::FSReqBase* req_wrap, PermissionScope perm, const std::string_view& res) { - Local err = CreateAccessDeniedError(env, perm, res); + MaybeLocal err = CreateAccessDeniedError(env, perm, res); if (err.IsEmpty()) return; - return req_wrap->Reject(err); + return req_wrap->Reject(err.ToLocalChecked()); } void Permission::EnablePermissions() {