|
| 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] // $ Source=source1_1 |
| 30 | +fn bad1_1() { |
| 31 | + _ = std::io::stdout().write(b"Hello, world!"); // $ Alert[rust/ctor-initialization]=source1_1 |
| 32 | +} |
| 33 | + |
| 34 | +#[ctor::dtor] // $ Source=source1_2 |
| 35 | +fn bad1_2() { |
| 36 | + _ = std::io::stdout().write(b"Hello, world!"); // $ Alert[rust/ctor-initialization]=source1_2 |
| 37 | +} |
| 38 | + |
| 39 | +#[rustfmt::skip] |
| 40 | +#[ctor::dtor] // $ Source=source1_3 |
| 41 | +#[rustfmt::skip] |
| 42 | +fn bad1_3() { |
| 43 | + _ = std::io::stdout().write(b"Hello, world!"); // $ Alert[rust/ctor-initialization]=source1_3 |
| 44 | +} |
| 45 | + |
| 46 | +// --- code variants --- |
| 47 | + |
| 48 | +use ctor::ctor; |
| 49 | +use std::io::*; |
| 50 | + |
| 51 | +#[ctor] // $ Source=source2_1 |
| 52 | +fn bad2_1() { |
| 53 | + _ = stdout().write(b"Hello, world!"); // $ Alert[rust/ctor-initialization]=source2_1 |
| 54 | +} |
| 55 | + |
| 56 | +#[ctor] // $ Source=source2_2 |
| 57 | +fn bad2_2() { |
| 58 | + _ = stderr().write_all(b"Hello, world!"); // $ Alert[rust/ctor-initialization]=source2_2 |
| 59 | +} |
| 60 | + |
| 61 | +#[ctor] // $ Source=source2_3 |
| 62 | +fn bad2_3() { |
| 63 | + println!("Hello, world!"); // $ Alert[rust/ctor-initialization]=source2_3 |
| 64 | +} |
| 65 | + |
| 66 | +#[ctor] // $ Source=source2_4 |
| 67 | +fn bad2_4() { |
| 68 | + let mut buff = String::new(); |
| 69 | + _ = std::io::stdin().read_line(&mut buff); // $ Alert[rust/ctor-initialization]=source2_4 |
| 70 | +} |
| 71 | + |
| 72 | +use std::fs; |
| 73 | + |
| 74 | +#[ctor] // $ MISSING: Source=source2_5 |
| 75 | +fn bad2_5() { |
| 76 | + let _buff = fs::File::create("hello.txt").unwrap(); // $ MISSING: Alert[rust/ctor-initialization]=source2_5 |
| 77 | +} |
| 78 | + |
| 79 | +#[ctor] // $ MISSING: Source=source2_6 |
| 80 | +fn bad2_6() { |
| 81 | + let _t = std::time::Instant::now(); // $ MISSING: Alert[rust/ctor-initialization]=source2_6 |
| 82 | +} |
| 83 | + |
| 84 | +use std::time::Duration; |
| 85 | + |
| 86 | +const DURATION2_7: Duration = Duration::new(1, 0); |
| 87 | + |
| 88 | +#[ctor] // $ Source=source2_7 |
| 89 | +fn bad2_7() { |
| 90 | + std::thread::sleep(DURATION2_7); // $ Alert[rust/ctor-initialization]=source2_7 |
| 91 | +} |
| 92 | + |
| 93 | +use std::process; |
| 94 | + |
| 95 | +#[ctor] // $ Source=source2_8 |
| 96 | +fn bad2_8() { |
| 97 | + process::exit(1234); // $ Alert[rust/ctor-initialization]=source2_8 |
| 98 | +} |
| 99 | + |
| 100 | +#[ctor::ctor] |
| 101 | +fn harmless2_9() { |
| 102 | + libc_print::libc_println!("Hello, world!"); // does not use the std library |
| 103 | +} |
| 104 | + |
| 105 | +#[ctor::ctor] |
| 106 | +fn harmless2_10() { |
| 107 | + core::assert!(true); // core library should be OK in this context |
| 108 | +} |
| 109 | + |
| 110 | +extern crate alloc; |
| 111 | +use alloc::alloc::{alloc, dealloc, Layout}; |
| 112 | + |
| 113 | +#[ctor::ctor] |
| 114 | +unsafe fn harmless2_11() { |
| 115 | + let layout = Layout::new::<u64>(); |
| 116 | + let ptr = alloc(layout); // alloc library should be OK in this context |
| 117 | + |
| 118 | + if !ptr.is_null() { |
| 119 | + dealloc(ptr, layout); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// --- transitive cases --- |
| 124 | + |
| 125 | +fn call_target3_1() { |
| 126 | + _ = stderr().write_all(b"Hello, world!"); // $ MISSING: Alert=source3_1 Alert=source3_3 Alert=source3_4 |
| 127 | +} |
| 128 | + |
| 129 | +#[ctor] // $ MISSING: Source=source3_1 |
| 130 | +fn bad3_1() { |
| 131 | + call_target3_1(); |
| 132 | +} |
| 133 | + |
| 134 | +fn call_target3_2() { |
| 135 | + for _x in 0..10 { |
| 136 | + // ... |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[ctor] // $ MISSING: Source=source3_2 |
| 141 | +fn harmless3_2() { |
| 142 | + call_target3_2(); |
| 143 | +} |
| 144 | + |
| 145 | +#[ctor] |
| 146 | +fn bad3_3() { |
| 147 | + call_target3_1(); |
| 148 | + call_target3_2(); |
| 149 | +} |
| 150 | + |
| 151 | +#[ctor] // $ MISSING: Source=source3_4 |
| 152 | +fn bad3_4() { |
| 153 | + bad3_3(); |
| 154 | +} |
| 155 | + |
| 156 | +// --- macros --- |
| 157 | + |
| 158 | +macro_rules! macro4_1 { |
| 159 | + () => { |
| 160 | + _ = std::io::stdout().write(b"Hello, world!"); |
| 161 | + }; |
| 162 | +} |
| 163 | + |
| 164 | +#[ctor] // $ Source=source4_1 |
| 165 | +fn bad4_1() { |
| 166 | + macro4_1!(); // $ Alert[rust/ctor-initialization]=source4_1 |
| 167 | +} |
0 commit comments