Skip to content

Commit

Permalink
permission: handle end nodes with children cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Jun 23, 2023
1 parent 198affc commit 394d0ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ bool FSPermission::RadixTree::Lookup(const std::string_view& s,
if (current_node->children.size() == 0) {
return when_empty_return;
}

unsigned int parent_node_prefix_len = current_node->prefix.length();
const std::string path(s);
auto path_len = path.length();
Expand Down
12 changes: 9 additions & 3 deletions src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ class FSPermission final : public PermissionBase {
std::string prefix;
std::unordered_map<char, Node*> children;
Node* wildcard_child;
bool is_leaf;

explicit Node(const std::string& pre)
: prefix(pre), wildcard_child(nullptr) {}
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}

Node() : wildcard_child(nullptr) {}
Node() : wildcard_child(nullptr), is_leaf(false) {}

Node* CreateChild(std::string prefix) {
if (prefix.empty() && !is_leaf) {
is_leaf = true;
return this;
}
char label = prefix[0];

Node* child = children[label];
Expand All @@ -56,6 +61,7 @@ class FSPermission final : public PermissionBase {
return split_child->CreateChild(prefix.substr(i));
}
}
child->is_leaf = true;
return child->CreateChild(prefix.substr(i));
}

Expand Down Expand Up @@ -114,7 +120,7 @@ class FSPermission final : public PermissionBase {
if (children.size() == 0) {
return true;
}
return children['\0'] != nullptr;
return is_leaf;
}
};

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-permission-fs-wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ if (common.isWindows) {
'/slower',
'/slown',
'/home/foo/*',
'/files/index.js',
'/files/index.json',
'/files/i',
];
const { status, stderr } = spawnSync(
process.execPath,
Expand All @@ -74,6 +77,10 @@ if (common.isWindows) {
assert.ok(process.permission.has('fs.read', '/home/foo'));
assert.ok(process.permission.has('fs.read', '/home/foo/'));
assert.ok(!process.permission.has('fs.read', '/home/fo'));
assert.ok(process.permission.has('fs.read', '/files/index.js'));
assert.ok(process.permission.has('fs.read', '/files/index.json'));
assert.ok(!process.permission.has('fs.read', '/files/index.j'));
assert.ok(process.permission.has('fs.read', '/files/i'));
`,
]
);
Expand Down

0 comments on commit 394d0ea

Please sign in to comment.