Skip to content

Commit

Permalink
Merge pull request #18312 from hvitved/rust/operator-overloading-test
Browse files Browse the repository at this point in the history
Rust: Add data flow tests for operator overloading
  • Loading branch information
hvitved authored Dec 18, 2024
2 parents 927d359 + 3a63dbc commit 00688eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
34 changes: 34 additions & 0 deletions rust/ql/test/library-tests/dataflow/global/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ fn data_through_method() {
sink(b); // $ hasValueFlow=4
}

use std::ops::Add;

struct MyInt {
value: i64,
}

impl Add for MyInt {
type Output = MyInt;

fn add(self, _other: MyInt) -> MyInt {
// Ignore `_other` to get value flow for `self.value`
MyInt { value: self.value }
}
}

pub fn test_operator_overloading() {
let a = MyInt { value: source(5) };
let b = MyInt { value: 2 };
let c = a + b;
sink(c.value); // $ MISSING: hasValueFlow=5

let a = MyInt { value: 2 };
let b = MyInt { value: source(6) };
let d = a + b;
sink(d.value);

let a = MyInt { value: source(7) };
let b = MyInt { value: 2 };
let d = a.add(b);
sink(d.value); // $ MISSING: hasValueFlow=7
}

fn main() {
data_out_of_call();
data_in_to_call();
Expand All @@ -99,4 +131,6 @@ fn main() {
data_out_of_method();
data_in_to_method_call();
data_through_method();

test_operator_overloading();
}
20 changes: 14 additions & 6 deletions rust/ql/test/library-tests/dataflow/global/viableCallable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
| main.rs:89:13:89:21 | source(...) | main.rs:1:1:3:1 | fn source |
| main.rs:90:13:90:30 | mn.data_through(...) | main.rs:66:5:72:5 | fn data_through |
| main.rs:91:5:91:11 | sink(...) | main.rs:5:1:7:1 | fn sink |
| main.rs:95:5:95:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
| main.rs:96:5:96:21 | data_in_to_call(...) | main.rs:25:1:28:1 | fn data_in_to_call |
| main.rs:97:5:97:23 | data_through_call(...) | main.rs:34:1:38:1 | fn data_through_call |
| main.rs:99:5:99:24 | data_out_of_method(...) | main.rs:75:1:79:1 | fn data_out_of_method |
| main.rs:100:5:100:28 | data_in_to_method_call(...) | main.rs:81:1:85:1 | fn data_in_to_method_call |
| main.rs:101:5:101:25 | data_through_method(...) | main.rs:87:1:92:1 | fn data_through_method |
| main.rs:110:28:110:36 | source(...) | main.rs:1:1:3:1 | fn source |
| main.rs:113:5:113:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
| main.rs:116:28:116:36 | source(...) | main.rs:1:1:3:1 | fn source |
| main.rs:118:5:118:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
| main.rs:120:28:120:36 | source(...) | main.rs:1:1:3:1 | fn source |
| main.rs:122:13:122:20 | a.add(...) | main.rs:103:5:106:5 | fn add |
| main.rs:123:5:123:17 | sink(...) | main.rs:5:1:7:1 | fn sink |
| main.rs:127:5:127:22 | data_out_of_call(...) | main.rs:16:1:19:1 | fn data_out_of_call |
| main.rs:128:5:128:21 | data_in_to_call(...) | main.rs:25:1:28:1 | fn data_in_to_call |
| main.rs:129:5:129:23 | data_through_call(...) | main.rs:34:1:38:1 | fn data_through_call |
| main.rs:131:5:131:24 | data_out_of_method(...) | main.rs:75:1:79:1 | fn data_out_of_method |
| main.rs:132:5:132:28 | data_in_to_method_call(...) | main.rs:81:1:85:1 | fn data_in_to_method_call |
| main.rs:133:5:133:25 | data_through_method(...) | main.rs:87:1:92:1 | fn data_through_method |
| main.rs:135:5:135:31 | test_operator_overloading(...) | main.rs:109:1:124:1 | fn test_operator_overloading |

0 comments on commit 00688eb

Please sign in to comment.