Skip to content

Commit 52401aa

Browse files
committed
Address review comments
1 parent 13f4a6a commit 52401aa

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

rust/ql/lib/codeql/rust/internal/PathResolution.qll

+7-2
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ private predicate fileModule(SourceFile f, string name, Folder folder) {
655655
)
656656
}
657657

658+
/**
659+
* Gets the `Meta` of the module `m`'s [path attribute][1].
660+
*
661+
* [1]: https://doc.rust-lang.org/reference/items/modules.html#r-items.mod.outlined.path
662+
*/
658663
private Meta getPathAttrMeta(Module m) {
659664
result = m.getAnAttr().getMeta() and
660665
result.getPath().getText() = "path"
@@ -725,7 +730,7 @@ private predicate pathAttrImport(Folder f, Module m, string relativePath) {
725730
)
726731
}
727732

728-
private predicate append(Folder f, string relativePath) { pathAttrImport(f, _, relativePath) }
733+
private predicate shouldAppend(Folder f, string relativePath) { pathAttrImport(f, _, relativePath) }
729734

730735
/** Holds if `m` is a `mod name;` item importing file `f`. */
731736
private predicate fileImport(Module m, SourceFile f) {
@@ -743,7 +748,7 @@ private predicate fileImport(Module m, SourceFile f) {
743748
or
744749
exists(Folder folder, string relativePath |
745750
pathAttrImport(folder, m, relativePath) and
746-
f.getFile() = Folder::Append<append/2>::append(folder, relativePath)
751+
f.getFile() = Folder::Append<shouldAppend/2>::append(folder, relativePath)
747752
)
748753
}
749754

shared/util/codeql/util/FileSystem.qll

+14-7
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,27 @@ module Make<InputSig Input> {
222222
/** Provides logic related to `Folder`s. */
223223
module Folder {
224224
/** Holds if `relativePath` needs to be appended to `f`. */
225-
signature predicate appendSig(Folder f, string relativePath);
225+
signature predicate shouldAppendSig(Folder f, string relativePath);
226226

227227
/** Provides the `append` predicate for appending a relative path onto a folder. */
228-
module Append<appendSig/2 app> {
228+
module Append<shouldAppendSig/2 shouldAppend> {
229229
pragma[nomagic]
230230
private string getComponent(string relativePath, int i) {
231-
app(_, relativePath) and
231+
shouldAppend(_, relativePath) and
232232
result = relativePath.replaceAll("\\", "/").regexpFind("[^/]+", i, _)
233233
}
234234

235+
private int getNumberOfComponents(string relativePath) {
236+
result = strictcount(int i | exists(getComponent(relativePath, i)) | i)
237+
or
238+
relativePath = "" and
239+
result = 0
240+
}
241+
235242
pragma[nomagic]
236243
private Container appendStep(Folder f, string relativePath, int i) {
237244
i = -1 and
238-
app(f, relativePath) and
245+
shouldAppend(f, relativePath) and
239246
result = f
240247
or
241248
exists(Container mid, string comp |
@@ -258,9 +265,9 @@ module Make<InputSig Input> {
258265
*/
259266
pragma[nomagic]
260267
Container append(Folder f, string relativePath) {
261-
exists(int components |
262-
components = (-1).maximum(max(int comp | exists(getComponent(relativePath, comp)) | comp)) and
263-
result = appendStep(f, relativePath, components)
268+
exists(int last |
269+
last = getNumberOfComponents(relativePath) - 1 and
270+
result = appendStep(f, relativePath, last)
264271
)
265272
}
266273
}

0 commit comments

Comments
 (0)