From 27a4f5864089315b1a6e58c4b2b1e19bb8764c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 17 Nov 2023 12:04:45 +0000 Subject: [PATCH] permission: mark const functions as such MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise, non-mutating functions such as is_tree_granted unnecessarily require pointers to mutable data structures. PR-URL: https://github.com/nodejs/node/pull/50705 Reviewed-By: Rafael Gonzaga Reviewed-By: James M Snell Reviewed-By: Vinícius Lourenço Claro Cardoso --- src/permission/child_process_permission.cc | 2 +- src/permission/child_process_permission.h | 2 +- src/permission/fs_permission.cc | 9 +++++---- src/permission/fs_permission.h | 11 ++++++----- src/permission/inspector_permission.cc | 2 +- src/permission/inspector_permission.h | 2 +- src/permission/permission_base.h | 2 +- src/permission/worker_permission.cc | 2 +- src/permission/worker_permission.h | 2 +- 9 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/permission/child_process_permission.cc b/src/permission/child_process_permission.cc index de078febf4bcd9..da85083918aa21 100644 --- a/src/permission/child_process_permission.cc +++ b/src/permission/child_process_permission.cc @@ -15,7 +15,7 @@ void ChildProcessPermission::Apply(const std::vector& allow, } bool ChildProcessPermission::is_granted(PermissionScope perm, - const std::string_view& param) { + const std::string_view& param) const { return deny_all_ == false; } diff --git a/src/permission/child_process_permission.h b/src/permission/child_process_permission.h index cf0ec97d5021a3..58f05b01982d6c 100644 --- a/src/permission/child_process_permission.h +++ b/src/permission/child_process_permission.h @@ -15,7 +15,7 @@ class ChildProcessPermission final : public PermissionBase { void Apply(const std::vector& allow, PermissionScope scope) override; bool is_granted(PermissionScope perm, - const std::string_view& param = "") override; + const std::string_view& param = "") const override; private: bool deny_all_; diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 7a073f99a49440..042dc0ff297216 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -50,8 +50,9 @@ void FreeRecursivelyNode( delete node; } -bool is_tree_granted(node::permission::FSPermission::RadixTree* granted_tree, - const std::string_view& param) { +bool is_tree_granted( + const node::permission::FSPermission::RadixTree* granted_tree, + const std::string_view& param) { #ifdef _WIN32 // is UNC file path if (param.rfind("\\\\", 0) == 0) { @@ -147,7 +148,7 @@ void FSPermission::GrantAccess(PermissionScope perm, const std::string& res) { } bool FSPermission::is_granted(PermissionScope perm, - const std::string_view& param = "") { + const std::string_view& param = "") const { switch (perm) { case PermissionScope::kFileSystem: return allow_all_in_ && allow_all_out_; @@ -171,7 +172,7 @@ FSPermission::RadixTree::~RadixTree() { } bool FSPermission::RadixTree::Lookup(const std::string_view& s, - bool when_empty_return = false) { + bool when_empty_return = false) const { FSPermission::RadixTree::Node* current_node = root_node_; if (current_node->children.size() == 0) { return when_empty_return; diff --git a/src/permission/fs_permission.h b/src/permission/fs_permission.h index 74b1440536652a..1c818567934f7d 100644 --- a/src/permission/fs_permission.h +++ b/src/permission/fs_permission.h @@ -17,7 +17,8 @@ class FSPermission final : public PermissionBase { public: void Apply(const std::vector& allow, PermissionScope scope) override; - bool is_granted(PermissionScope perm, const std::string_view& param) override; + bool is_granted(PermissionScope perm, + const std::string_view& param) const override; struct RadixTree { struct Node { @@ -72,7 +73,7 @@ class FSPermission final : public PermissionBase { return wildcard_child; } - Node* NextNode(const std::string& path, size_t idx) { + Node* NextNode(const std::string& path, size_t idx) const { if (idx >= path.length()) { return nullptr; } @@ -115,7 +116,7 @@ class FSPermission final : public PermissionBase { // ---> '\000' ASCII (0) || \0 // ---> er // ---> n - bool IsEndNode() { + bool IsEndNode() const { if (children.size() == 0) { return true; } @@ -126,8 +127,8 @@ class FSPermission final : public PermissionBase { RadixTree(); ~RadixTree(); void Insert(const std::string& s); - bool Lookup(const std::string_view& s) { return Lookup(s, false); } - bool Lookup(const std::string_view& s, bool when_empty_return); + bool Lookup(const std::string_view& s) const { return Lookup(s, false); } + bool Lookup(const std::string_view& s, bool when_empty_return) const; private: Node* root_node_; diff --git a/src/permission/inspector_permission.cc b/src/permission/inspector_permission.cc index 401d801ac0adb5..34e55db4bef590 100644 --- a/src/permission/inspector_permission.cc +++ b/src/permission/inspector_permission.cc @@ -14,7 +14,7 @@ void InspectorPermission::Apply(const std::vector& allow, } bool InspectorPermission::is_granted(PermissionScope perm, - const std::string_view& param) { + const std::string_view& param) const { return deny_all_ == false; } diff --git a/src/permission/inspector_permission.h b/src/permission/inspector_permission.h index e5c6d1b81677f5..fc8b357c4fcb15 100644 --- a/src/permission/inspector_permission.h +++ b/src/permission/inspector_permission.h @@ -15,7 +15,7 @@ class InspectorPermission final : public PermissionBase { void Apply(const std::vector& allow, PermissionScope scope) override; bool is_granted(PermissionScope perm, - const std::string_view& param = "") override; + const std::string_view& param = "") const override; private: bool deny_all_; diff --git a/src/permission/permission_base.h b/src/permission/permission_base.h index c2f377424f6fc5..b817efb88a76bb 100644 --- a/src/permission/permission_base.h +++ b/src/permission/permission_base.h @@ -42,7 +42,7 @@ class PermissionBase { virtual void Apply(const std::vector& allow, PermissionScope scope) = 0; virtual bool is_granted(PermissionScope perm, - const std::string_view& param = "") = 0; + const std::string_view& param = "") const = 0; }; } // namespace permission diff --git a/src/permission/worker_permission.cc b/src/permission/worker_permission.cc index a18938e5fe1efd..78293589faa3bd 100644 --- a/src/permission/worker_permission.cc +++ b/src/permission/worker_permission.cc @@ -15,7 +15,7 @@ void WorkerPermission::Apply(const std::vector& allow, } bool WorkerPermission::is_granted(PermissionScope perm, - const std::string_view& param) { + const std::string_view& param) const { return deny_all_ == false; } diff --git a/src/permission/worker_permission.h b/src/permission/worker_permission.h index cdc224925c2291..99d6b039ee22c1 100644 --- a/src/permission/worker_permission.h +++ b/src/permission/worker_permission.h @@ -15,7 +15,7 @@ class WorkerPermission final : public PermissionBase { void Apply(const std::vector& allow, PermissionScope scope) override; bool is_granted(PermissionScope perm, - const std::string_view& param = "") override; + const std::string_view& param = "") const override; private: bool deny_all_;