Skip to content

Commit 51f3acf

Browse files
committed
Rust: Add test for missing flow through captured variables
1 parent 2bc1b22 commit 51f3acf

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

rust/ql/lib/codeql/rust/controlflow/internal/Scope.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ final class CallableScope extends CfgScopeImpl, Callable {
3535
CallableScope() {
3636
// A function without a body corresponds to a trait method signature and
3737
// should not have a CFG scope.
38-
this.hasBody()
38+
this.hasBody() and
39+
this.fromSource() // exclude stubs in tests defined using `additionalExternalFile`
3940
}
4041

4142
override predicate scopeFirst(AstNode first) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn may_invoke_callback1<F: Fn(i64)>(f: F) {}
2+
3+
pub fn may_invoke_callback2<F: FnOnce(i64)>(f: F) {}
4+
5+
pub fn may_invoke_callback3(f: impl Fn(i64)) {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/rust-all
4+
extensible: additionalExternalFile
5+
data:
6+
- ["external_file.rs"]

rust/ql/test/library-tests/dataflow/lambdas/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ fn test_apply_wrap() {
102102
apply_wrap(|x| sink(x), 0);
103103
}
104104

105+
mod external_file;
106+
use external_file::*;
107+
108+
fn test_external_call() {
109+
let a = source(81);
110+
may_invoke_callback1(|x| sink(a)); // $ MISSING: hasValueFlow=81
111+
may_invoke_callback2(|x| sink(a)); // $ MISSING: hasValueFlow=81
112+
may_invoke_callback3(|x| sink(a)); // $ MISSING: hasValueFlow=81
113+
}
114+
105115
fn main() {
106116
closure_flow_out();
107117
closure_flow_in();

0 commit comments

Comments
 (0)