Skip to content

Commit f672f6b

Browse files
authored
Merge pull request #20727 from hvitved/rust/variable-overlap-fix
Rust: Fix variable access overlap
2 parents a1a9626 + 6d64800 commit f672f6b

File tree

7 files changed

+1816
-1753
lines changed

7 files changed

+1816
-1753
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,12 @@ module Impl {
460460
VariableAccessCand cand, VariableScope scope, string name, int nestLevel, int ord
461461
) {
462462
name = cand.getName() and
463-
scope = [cand.(VariableScope), getEnclosingScope(cand)] and
463+
(
464+
scope = cand
465+
or
466+
not cand instanceof VariableScope and
467+
scope = getEnclosingScope(cand)
468+
) and
464469
ord = getPreOrderNumbering(scope, cand) and
465470
nestLevel = 0
466471
or

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ query predicate multiplePositions(Element parent, int pos1, int pos2, string acc
7373
pos1 != pos2
7474
}
7575

76+
/**
77+
* Holds if `va` is a variable access that refers to multiple variables.
78+
*/
79+
query predicate multipleVariableTargets(VariableAccess va, Variable v1) {
80+
va = v1.getAnAccess() and
81+
strictcount(va.getVariable()) > 1
82+
}
83+
7684
/**
7785
* Gets counts of abstract syntax tree inconsistencies of each type.
7886
*/
@@ -98,4 +106,7 @@ int getAstInconsistencyCounts(string type) {
98106
or
99107
type = "Multiple positions" and
100108
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
109+
or
110+
type = "Multiple variable targets" and
111+
result = count(VariableAccess va | multipleVariableTargets(va, _) | va)
101112
}

rust/ql/test/library-tests/variables/Cfg.expected

Lines changed: 1031 additions & 1009 deletions
Large diffs are not rendered by default.

rust/ql/test/library-tests/variables/Ssa.expected

Lines changed: 395 additions & 388 deletions
Large diffs are not rendered by default.

rust/ql/test/library-tests/variables/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ fn match_pattern14() {
360360
}
361361
}
362362

363+
fn match_pattern15() {
364+
let x = Some(0); // x1
365+
match x { // $ read_access=x1
366+
Some(x) // x2
367+
=> x, // $ read_access=x2
368+
_ => 0
369+
};
370+
}
371+
363372
fn param_pattern1(
364373
a8: &str, // a8
365374
(
@@ -757,6 +766,7 @@ fn main() {
757766
match_pattern12();
758767
match_pattern13();
759768
match_pattern14();
769+
match_pattern15();
760770
param_pattern1("a", ("b", "c"));
761771
param_pattern2(Either::Left(45));
762772
destruct_assignment();

0 commit comments

Comments
 (0)