Skip to content

Commit 9ead2dc

Browse files
committed
Rust: Add a query test.
1 parent 68a4ea3 commit 9ead2dc

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 0 |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: queries/security/CWE-696/BadCtorInitialization.ql
2+
postprocess: utils/InlineExpectationsTestQuery.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
qltest_cargo_check: true
2+
qltest_dependencies:
3+
- ctor = { version = "0.2.9" }
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
// --- attribute variants ---
3+
4+
use std::io::Write;
5+
6+
fn harmless1_1() {
7+
// ...
8+
}
9+
10+
#[ctor::ctor]
11+
fn harmless1_2() {
12+
// ...
13+
}
14+
15+
#[ctor::dtor]
16+
fn harmless1_3() {
17+
// ...
18+
}
19+
20+
fn harmless1_4() {
21+
_ = std::io::stdout().write(b"Hello, world!");
22+
}
23+
24+
#[rustfmt::skip]
25+
fn harmless1_5() {
26+
_ = std::io::stdout().write(b"Hello, world!");
27+
}
28+
29+
#[ctor::ctor]
30+
fn bad1_1() { // $ MISSING: Alert[rust/ctor-initialization]
31+
_ = std::io::stdout().write(b"Hello, world!");
32+
}
33+
34+
#[ctor::dtor]
35+
fn bad1_2() { // $ MISSING: Alert[rust/ctor-initialization]
36+
_ = std::io::stdout().write(b"Hello, world!");
37+
}
38+
39+
#[rustfmt::skip]
40+
#[ctor::dtor]
41+
#[rustfmt::skip]
42+
fn bad1_3() { // $ MISSING: Alert[rust/ctor-initialization]
43+
_ = std::io::stdout().write(b"Hello, world!");
44+
}
45+
46+
// --- code variants ---
47+
48+
use ctor::ctor;
49+
use std::io::*;
50+
51+
#[ctor]
52+
fn bad2_1() { // $ MISSING: Alert[rust/ctor-initialization]
53+
_ = stdout().write(b"Hello, world!");
54+
}
55+
56+
#[ctor]
57+
fn bad2_2() { // $ MISSING: Alert[rust/ctor-initialization]
58+
_ = stderr().write_all(b"Hello, world!");
59+
}
60+
61+
#[ctor]
62+
fn bad2_3() { // $ MISSING: Alert[rust/ctor-initialization]
63+
println!("Hello, world!");
64+
}
65+
66+
#[ctor]
67+
fn bad2_4() { // $ MISSING: Alert[rust/ctor-initialization]
68+
let mut buff = String::new();
69+
_ = std::io::stdin().read_line(&mut buff);
70+
}
71+
72+
use std::fs;
73+
74+
#[ctor]
75+
fn bad2_5() { // $ MISSING: Alert[rust/ctor-initialization]
76+
let _buff = fs::File::create("hello.txt").unwrap();
77+
}
78+
79+
#[ctor]
80+
fn bad2_6() { // $ MISSING: Alert[rust/ctor-initialization]
81+
let _t = std::time::Instant::now();
82+
}
83+
84+
use std::time::Duration;
85+
86+
const DURATION2_7: Duration = Duration::new(1, 0);
87+
88+
#[ctor]
89+
fn bad2_7() { // $ MISSING: Alert[rust/ctor-initialization]
90+
std::thread::sleep(DURATION2_7);
91+
}
92+
93+
use std::process;
94+
95+
#[ctor]
96+
fn bad2_8() { // $ MISSING: Alert[rust/ctor-initialization]
97+
process::exit(1234);
98+
}
99+
100+
// --- transitive cases ---
101+
102+
fn call_target3_1() {
103+
_ = stderr().write_all(b"Hello, world!");
104+
}
105+
106+
#[ctor]
107+
fn bad3_1() { // $ MISSING: Alert[rust/ctor-initialization]
108+
call_target3_1();
109+
}
110+
111+
fn call_target3_2() {
112+
for _x in 0..10 {
113+
// ...
114+
}
115+
}
116+
117+
#[ctor]
118+
fn harmless3_2() {
119+
call_target3_2();
120+
}
121+
122+
#[ctor]
123+
fn bad3_3() { // $ MISSING: Alert[rust/ctor-initialization]
124+
call_target3_1();
125+
call_target3_2();
126+
}
127+
128+
#[ctor]
129+
fn bad3_4() { // $ MISSING: Alert[rust/ctor-initialization]
130+
bad3_3();
131+
}
132+
133+
// --- macros ---
134+
135+
macro_rules! macro4_1 {
136+
() => {
137+
_ = std::io::stdout().write(b"Hello, world!");
138+
};
139+
}
140+
141+
#[ctor]
142+
fn bad4_1() { // $ MISSING: Alert[rust/ctor-initialization]
143+
macro4_1!();
144+
}

0 commit comments

Comments
 (0)