Skip to content

Commit 3e54c61

Browse files
committed
Rust: Add MaD trait tests
1 parent 5e265b1 commit 3e54c61

File tree

3 files changed

+439
-302
lines changed

3 files changed

+439
-302
lines changed

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

Lines changed: 81 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,81 @@ 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+
308+
impl Eq for MyStruct {}
309+
310+
impl Eq for MyStruct2 {}
311+
312+
use std::cmp::Ordering;
313+
314+
impl PartialOrd for MyStruct {
315+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
316+
Some(Ordering::Equal)
317+
}
318+
}
319+
320+
impl PartialOrd for MyStruct2 {
321+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
322+
Some(self.0.cmp(&other.0))
323+
}
324+
}
325+
326+
impl Ord for MyStruct {
327+
fn cmp(&self, other: &Self) -> Ordering {
328+
Ordering::Equal
329+
}
330+
}
331+
332+
impl Ord for MyStruct2 {
333+
fn cmp(&self, other: &Self) -> Ordering {
334+
self.0.cmp(&other.0)
335+
}
336+
337+
fn max(self, other: Self) -> Self {
338+
other
339+
}
340+
}
341+
342+
fn test_trait_model<T: Ord>(x: T) {
343+
let x1 = source(20).max(0);
344+
sink(x1); // $ hasValueFlow=20
345+
346+
let x2 = (MyStruct {
347+
field1: source(23),
348+
field2: 0,
349+
})
350+
.max(MyStruct {
351+
field1: 0,
352+
field2: 0,
353+
});
354+
sink(x2.field1); // $ hasValueFlow=23
355+
356+
let x3 = MyStruct2(source(24)).max(MyStruct2(0));
357+
sink(x3.0); // no flow, because the model does not apply when the target is in source code
358+
359+
let x4 = source(25).max(1);
360+
sink(x4); // $ hasValueFlow=25
361+
362+
let x5 = source(26).lt(&1);
363+
sink(x5); // $ MISSING: hasTaintFlow=26
364+
365+
let x6 = source(27) < 1;
366+
sink(x6); // $ MISSING: hasTaintFlow=27
367+
}
368+
291369
#[tokio::main]
292370
async fn main() {
293371
test_identify();

0 commit comments

Comments
 (0)