Skip to content

Commit b38bc9f

Browse files
authored
permission: handle end nodes with children cases
PR-URL: #48531 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 7cd4e70 commit b38bc9f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/permission/fs_permission.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ bool FSPermission::RadixTree::Lookup(const std::string_view& s,
132132
if (current_node->children.size() == 0) {
133133
return when_empty_return;
134134
}
135-
136135
unsigned int parent_node_prefix_len = current_node->prefix.length();
137136
const std::string path(s);
138137
auto path_len = path.length();

src/permission/fs_permission.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ class FSPermission final : public PermissionBase {
2525
std::string prefix;
2626
std::unordered_map<char, Node*> children;
2727
Node* wildcard_child;
28+
bool is_leaf;
2829

2930
explicit Node(const std::string& pre)
30-
: prefix(pre), wildcard_child(nullptr) {}
31+
: prefix(pre), wildcard_child(nullptr), is_leaf(false) {}
3132

32-
Node() : wildcard_child(nullptr) {}
33+
Node() : wildcard_child(nullptr), is_leaf(false) {}
3334

3435
Node* CreateChild(std::string prefix) {
36+
if (prefix.empty() && !is_leaf) {
37+
is_leaf = true;
38+
return this;
39+
}
3540
char label = prefix[0];
3641

3742
Node* child = children[label];
@@ -56,6 +61,7 @@ class FSPermission final : public PermissionBase {
5661
return split_child->CreateChild(prefix.substr(i));
5762
}
5863
}
64+
child->is_leaf = true;
5965
return child->CreateChild(prefix.substr(i));
6066
}
6167

@@ -114,7 +120,7 @@ class FSPermission final : public PermissionBase {
114120
if (children.size() == 0) {
115121
return true;
116122
}
117-
return children['\0'] != nullptr;
123+
return is_leaf;
118124
}
119125
};
120126

test/parallel/test-permission-fs-wildcard.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ if (common.isWindows) {
5959
'/slower',
6060
'/slown',
6161
'/home/foo/*',
62+
'/files/index.js',
63+
'/files/index.json',
64+
'/files/i',
6265
];
6366
const { status, stderr } = spawnSync(
6467
process.execPath,
@@ -74,6 +77,10 @@ if (common.isWindows) {
7477
assert.ok(process.permission.has('fs.read', '/home/foo'));
7578
assert.ok(process.permission.has('fs.read', '/home/foo/'));
7679
assert.ok(!process.permission.has('fs.read', '/home/fo'));
80+
assert.ok(process.permission.has('fs.read', '/files/index.js'));
81+
assert.ok(process.permission.has('fs.read', '/files/index.json'));
82+
assert.ok(!process.permission.has('fs.read', '/files/index.j'));
83+
assert.ok(process.permission.has('fs.read', '/files/i'));
7784
`,
7885
]
7986
);

0 commit comments

Comments
 (0)