Skip to content

Commit

Permalink
permission: mark const functions as such
Browse files Browse the repository at this point in the history
Otherwise, non-mutating functions such as is_tree_granted unnecessarily
require pointers to mutable data structures.

PR-URL: #50705
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
  • Loading branch information
tniessen authored and targos committed Nov 23, 2023
1 parent 6b81666 commit 407d4a6
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/permission/child_process_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void ChildProcessPermission::Apply(const std::vector<std::string>& allow,
}

bool ChildProcessPermission::is_granted(PermissionScope perm,
const std::string_view& param) {
const std::string_view& param) const {
return deny_all_ == false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/permission/child_process_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ChildProcessPermission final : public PermissionBase {
void Apply(const std::vector<std::string>& 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_;
Expand Down
9 changes: 5 additions & 4 deletions src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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_;
Expand All @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class FSPermission final : public PermissionBase {
public:
void Apply(const std::vector<std::string>& 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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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_;
Expand Down
2 changes: 1 addition & 1 deletion src/permission/inspector_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void InspectorPermission::Apply(const std::vector<std::string>& allow,
}

bool InspectorPermission::is_granted(PermissionScope perm,
const std::string_view& param) {
const std::string_view& param) const {
return deny_all_ == false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/permission/inspector_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class InspectorPermission final : public PermissionBase {
void Apply(const std::vector<std::string>& 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_;
Expand Down
2 changes: 1 addition & 1 deletion src/permission/permission_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PermissionBase {
virtual void Apply(const std::vector<std::string>& 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
Expand Down
2 changes: 1 addition & 1 deletion src/permission/worker_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void WorkerPermission::Apply(const std::vector<std::string>& allow,
}

bool WorkerPermission::is_granted(PermissionScope perm,
const std::string_view& param) {
const std::string_view& param) const {
return deny_all_ == false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/permission/worker_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WorkerPermission final : public PermissionBase {
void Apply(const std::vector<std::string>& 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_;
Expand Down

0 comments on commit 407d4a6

Please sign in to comment.