Skip to content

Commit 1925c20

Browse files
committed
Rust: Make crate fallback logic more conservative in path resolution library
1 parent b0fa3e7 commit 1925c20

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,16 @@ class CrateItemNode extends NamedItemNode instanceof Crate {
568568
)
569569
}
570570

571+
pragma[nomagic]
572+
predicate isLatestVersion(string name) {
573+
this =
574+
max(CrateItemNode c, string ver |
575+
name = c.getName() and ver = c.(Crate).getVersion()
576+
|
577+
c order by ver
578+
)
579+
}
580+
571581
override string getName() { result = Crate.super.getName() }
572582

573583
override Namespace getNamespace() {
@@ -1529,11 +1539,11 @@ private predicate crateDependencyEdge(SourceFileItemNode file, string name, Crat
15291539
crateDependency(file, name, dep)
15301540
or
15311541
// As a fallback, give all files access to crates that do not conflict with known dependencies
1532-
// and declarations. This is in order to workaround incomplete crate dependency information
1533-
// provided by the extractor, as well as `CrateItemNode.getASourceFile()` being unable to map
1534-
// a given file to its crate (for example, if the file is `mod` imported inside a macro that the
1535-
// extractor is unable to expand).
1536-
name = dep.getName() and
1542+
// and declarations, as long as those crates have a unique latest version.
1543+
// This is in order to workaround incomplete crate dependency information provided by the extractor,
1544+
// as well as `CrateItemNode.getASourceFile()` being unable to map a given file to its crate (for
1545+
// example, if the file is `mod` imported inside a macro that the extractor is unable to expand).
1546+
dep = unique(CrateItemNode dep0 | dep0.isLatestVersion(name)) and
15371547
not hasDeclOrDep(file, name)
15381548
}
15391549

@@ -2385,6 +2395,11 @@ private module Debug {
23852395
useImportEdge(use, name, item, kind)
23862396
}
23872397

2398+
predicate debugCrateDependencyEdge(SourceFileItemNode file, string name, CrateItemNode dep) {
2399+
file = getRelevantLocatable() and
2400+
crateDependencyEdge(file, name, dep)
2401+
}
2402+
23882403
ItemNode debugGetASuccessor(ItemNode i, string name, SuccessorKind kind) {
23892404
i = getRelevantLocatable() and
23902405
result = i.getASuccessor(name, kind, _)

0 commit comments

Comments
 (0)