Skip to content

Commit 69e549f

Browse files
committed
Rust: Generate canonical paths for builtins
1 parent 31770ed commit 69e549f

File tree

6 files changed

+22
-2
lines changed

6 files changed

+22
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ class CrateItemNode extends ItemNode instanceof Crate {
374374
not file = child.(SourceFileItemNode).getSuper() and
375375
file = super.getSourceFile()
376376
)
377+
or
378+
this.getName() = "core" and
379+
child instanceof Builtins::BuiltinType
377380
}
378381

379382
override string getCanonicalPath(Crate c) { c = this and result = Crate.super.getName() }

rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
canonicalPath
22
| anonymous.rs:3:1:32:1 | fn canonicals | test::anonymous::canonicals |
33
| anonymous.rs:34:1:36:1 | fn other | test::anonymous::other |
4+
| {EXTERNAL LOCATION} | fn trim | <core::str>::trim |
45
| lib.rs:1:1:1:14 | mod anonymous | test::anonymous |
56
| lib.rs:2:1:2:12 | mod regular | test::regular |
67
| regular.rs:1:1:2:18 | struct Struct | test::regular::Struct |

rust/ql/test/extractor-tests/canonical_path/canonical_paths.ql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import rust
22
import TestUtils
3+
private import codeql.rust.internal.PathResolution
4+
private import codeql.rust.frameworks.stdlib.Bultins
35

46
query predicate canonicalPath(Addressable a, string path) {
5-
toBeTested(a) and
7+
(
8+
toBeTested(a)
9+
or
10+
// test that we also generate canonical paths for builtins
11+
a =
12+
any(ImplItemNode i |
13+
i.resolveSelfTy() instanceof Str and
14+
not i.(Impl).hasTrait()
15+
).getAnAssocItem() and
16+
a.(Function).getName().getText() = "trim"
17+
) and
618
path = a.getCanonicalPath(_)
719
}
820

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: canonical_paths.ql
2+
postprocess: utils/test/ExternalLocationPostProcessing.ql

rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
canonicalPath
22
| anonymous.rs:6:1:35:1 | fn canonicals | test::anonymous::canonicals |
33
| anonymous.rs:37:1:39:1 | fn other | test::anonymous::other |
4+
| {EXTERNAL LOCATION} | fn trim | <core::str>::trim |
45
| lib.rs:1:1:1:14 | mod anonymous | test::anonymous |
56
| lib.rs:2:1:2:12 | mod regular | test::regular |
67
| regular.rs:4:1:5:18 | struct Struct | test::regular::Struct |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
extractor-tests/canonical_path/canonical_paths.ql
1+
query: extractor-tests/canonical_path/canonical_paths.ql
2+
postprocess: utils/test/ExternalLocationPostProcessing.ql

0 commit comments

Comments
 (0)