Skip to content

Commit cef3cd9

Browse files
committed
Rust: Add tests for std::io sources.
1 parent 8631371 commit cef3cd9

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

rust/ql/test/library-tests/dataflow/sources/TaintSources.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
| test.rs:80:24:80:35 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
2323
| test.rs:112:35:112:46 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
2424
| test.rs:119:31:119:42 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
25-
| test.rs:203:16:203:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |
25+
| test.rs:352:16:352:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |

rust/ql/test/library-tests/dataflow/sources/test.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,155 @@ async fn test_hyper_http(case: i64) -> Result<(), Box<dyn std::error::Error>> {
198198
Ok(())
199199
}
200200

201+
use std::io::Read;
202+
use std::io::BufRead;
203+
204+
fn test_io_fs() -> std::io::Result<()> {
205+
// --- stdin ---
206+
207+
{
208+
let mut buffer = [0u8; 100];
209+
let _bytes = std::io::stdin().read(&mut buffer)?; // $ MISSING: Alert[rust/summary/taint-sources]
210+
sink(&buffer); // $ MISSING: hasTaintFlow
211+
}
212+
213+
{
214+
let mut buffer = Vec::<u8>::new();
215+
let _bytes = std::io::stdin().read_to_end(&mut buffer)?; // $ MISSING: Alert[rust/summary/taint-sources]
216+
sink(&buffer); // $ MISSING: hasTaintFlow
217+
}
218+
219+
{
220+
let mut buffer = String::new();
221+
let _bytes = std::io::stdin().read_to_string(&mut buffer)?; // $ MISSING: Alert[rust/summary/taint-sources]
222+
sink(&buffer); // $ MISSING: hasTaintFlow
223+
}
224+
225+
{
226+
let mut buffer = [0; 100];
227+
std::io::stdin().read_exact(&mut buffer)?; // $ MISSING: Alert[rust/summary/taint-sources]
228+
sink(&buffer); // $ MISSING: hasTaintFlow
229+
}
230+
231+
for byte in std::io::stdin().bytes() { // $ MISSING: Alert[rust/summary/taint-sources]
232+
sink(byte); // $ MISSING: hasTaintFlow
233+
}
234+
235+
// --- file ---
236+
237+
let mut file = std::fs::File::open("file.txt")?; // $ MISSING: Alert[rust/summary/taint-sources]
238+
239+
{
240+
let mut buffer = [0u8; 100];
241+
let _bytes = file.read(&mut buffer)?;
242+
sink(&buffer); // $ MISSING: hasTaintFlow
243+
}
244+
245+
{
246+
let mut buffer = Vec::<u8>::new();
247+
let _bytes = file.read_to_end(&mut buffer)?;
248+
sink(&buffer); // $ MISSING: hasTaintFlow
249+
}
250+
251+
{
252+
let mut buffer = String::new();
253+
let _bytes = file.read_to_string(&mut buffer)?;
254+
sink(&buffer); // $ MISSING: hasTaintFlow
255+
}
256+
257+
{
258+
let mut buffer = [0; 100];
259+
file.read_exact(&mut buffer)?;
260+
sink(&buffer); // $ MISSING: hasTaintFlow
261+
}
262+
263+
for byte in file.bytes() {
264+
sink(byte); // $ MISSING: hasTaintFlow
265+
}
266+
267+
// --- BufReader ---
268+
269+
{
270+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
271+
let data = reader.fill_buf()?;
272+
sink(&data); // $ MISSING: hasTaintFlow
273+
}
274+
275+
{
276+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
277+
let data = reader.buffer();
278+
sink(&data); // $ MISSING: hasTaintFlow
279+
}
280+
281+
{
282+
let mut buffer = String::new();
283+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
284+
reader.read_line(&mut buffer)?;
285+
sink(&buffer); // $ MISSING: hasTaintFlow
286+
}
287+
288+
{
289+
let mut buffer = Vec::<u8>::new();
290+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
291+
reader.read_until(b',', &mut buffer)?;
292+
sink(&buffer); // $ MISSING: hasTaintFlow
293+
}
294+
295+
{
296+
let mut buffer = Vec::<u8>::new();
297+
let mut reader_split = std::io::BufReader::new(std::io::stdin()).split(b','); // $ MISSING: Alert[rust/summary/taint-sources]
298+
while let Some(chunk) = reader_split.next() {
299+
sink(chunk.unwrap()); // $ MISSING: hasTaintFlow
300+
}
301+
}
302+
303+
{
304+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
305+
for line in reader.lines() {
306+
sink(line); // $ MISSING: Alert[rust/summary/taint-sources]
307+
}
308+
}
309+
310+
{
311+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
312+
let line = reader.lines().nth(1).unwrap();
313+
sink(line.unwrap().clone()); // $ MISSING: hasTaintFlow
314+
}
315+
316+
{
317+
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ MISSING: Alert[rust/summary/taint-sources]
318+
let lines: Vec<_> = reader.lines().collect();
319+
sink(lines[1].as_ref().unwrap().clone()); // $ MISSING: hasTaintFlow
320+
}
321+
322+
// --- misc operations ---
323+
324+
{
325+
let mut buffer = String::new();
326+
let mut file1 = std::fs::File::open("file.txt")?; // $ MISSING: Alert[rust/summary/taint-sources]
327+
let mut file2 = std::fs::File::open("another_file.txt")?; // $ MISSING: Alert[rust/summary/taint-sources]
328+
let mut reader = file1.chain(file2);
329+
reader.read_to_string(&mut buffer)?;
330+
sink(&buffer); // $ MISSING: hasTaintFlow
331+
}
332+
333+
{
334+
let mut buffer = String::new();
335+
let mut file1 = std::fs::File::open("file.txt")?; // $ MISSING: Alert[rust/summary/taint-sources]
336+
let mut reader = file1.take(100);
337+
reader.read_to_string(&mut buffer)?;
338+
sink(&buffer); // $ MISSING: hasTaintFlow
339+
}
340+
341+
{
342+
let mut buffer = String::new();
343+
let _bytes = std::io::stdin().lock().read_to_string(&mut buffer)?; // $ MISSING: Alert[rust/summary/taint-sources]
344+
sink(&buffer); // $ MISSING: hasTaintFlow
345+
}
346+
347+
Ok(())
348+
}
349+
201350
#[tokio::main]
202351
async fn main() -> Result<(), Box<dyn std::error::Error>> {
203352
let case = std::env::args().nth(1).unwrap_or(String::from("1")).parse::<i64>().unwrap(); // $ Alert[rust/summary/taint-sources]

0 commit comments

Comments
 (0)