Skip to content

Commit d69f1e7

Browse files
committed
Rust: Add MaD trait tests
1 parent 3f60b0f commit d69f1e7

File tree

3 files changed

+437
-302
lines changed

3 files changed

+437
-302
lines changed

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

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ fn source(i: i64) -> i64 {
22
1000 + i
33
}
44

5-
fn sink(s: i64) {
6-
println!("{}", s);
5+
fn sink<T: std::fmt::Debug>(s: T) {
6+
println!("{:?}", s);
77
}
88

99
// has a flow model
@@ -176,7 +176,10 @@ fn test_set_tuple_element() {
176176
}
177177

178178
// has a flow model
179-
pub fn apply<F>(n: i64, f: F) -> i64 where F : FnOnce(i64) -> i64 {
179+
pub fn apply<F>(n: i64, f: F) -> i64
180+
where
181+
F: FnOnce(i64) -> i64,
182+
{
180183
0
181184
}
182185

@@ -288,6 +291,79 @@ fn test_arg_source() {
288291
sink(i) // $ hasValueFlow=i
289292
}
290293

294+
struct MyStruct2(i64);
295+
296+
impl PartialEq for MyStruct {
297+
fn eq(&self, other: &Self) -> bool {
298+
true
299+
}
300+
}
301+
302+
impl PartialEq for MyStruct2 {
303+
fn eq(&self, other: &Self) -> bool {
304+
self.0 == other.0
305+
}
306+
}
307+
impl Eq for MyStruct {}
308+
309+
impl Eq for MyStruct2 {}
310+
311+
use std::cmp::Ordering;
312+
313+
impl PartialOrd for MyStruct {
314+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
315+
Some(Ordering::Equal)
316+
}
317+
}
318+
impl PartialOrd for MyStruct2 {
319+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
320+
Some(self.0.cmp(&other.0))
321+
}
322+
}
323+
324+
impl Ord for MyStruct {
325+
fn cmp(&self, other: &Self) -> Ordering {
326+
Ordering::Equal
327+
}
328+
}
329+
330+
impl Ord for MyStruct2 {
331+
fn cmp(&self, other: &Self) -> Ordering {
332+
self.0.cmp(&other.0)
333+
}
334+
335+
fn max(self, other: Self) -> Self {
336+
other
337+
}
338+
}
339+
340+
fn test_trait_model<T: Ord>(x: T) {
341+
let x1 = source(20).max(0);
342+
sink(x1); // $ hasValueFlow=20
343+
344+
let x2 = (MyStruct {
345+
field1: source(23),
346+
field2: 0,
347+
})
348+
.max(MyStruct {
349+
field1: 0,
350+
field2: 0,
351+
});
352+
sink(x2.field1); // $ hasValueFlow=23
353+
354+
let x3 = MyStruct2(source(24)).max(MyStruct2(0));
355+
sink(x3.0); // no flow, because the model does not apply when the target is in source code
356+
357+
let x4 = source(25).max(1);
358+
sink(x4); // $ hasValueFlow=25
359+
360+
let x5 = source(26).lt(&1);
361+
sink(x5); // $ MISSING: hasTaintFlow=26
362+
363+
let x6 = source(27) < 1;
364+
sink(x6); // $ MISSING: hasTaintFlow=27
365+
}
366+
291367
#[tokio::main]
292368
async fn main() {
293369
test_identify();

0 commit comments

Comments
 (0)