Skip to content

Commit 408397f

Browse files
authored
Adding Fuzz targets for lr planar (#1434)
* Adding Fuzz targets for lr planar Added a fuzz target to check the robustness of lr planar function. * Fixing Lint
1 parent 30897c5 commit 408397f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

rustworkx-core/fuzz/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ test = false
4242
doc = false
4343
bench = false
4444

45+
[[bin]]
46+
name = "test_fuzz_planar"
47+
path = "fuzz_targets/test_fuzz_planar.rs"
48+
test = false
49+
doc = false
50+
bench = false
51+
4552
[[bin]]
4653
name = "test_fuzz_contraction"
4754
path = "fuzz_targets/test_fuzz_contraction.rs"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![no_main]
2+
3+
use arbitrary::{Arbitrary, Unstructured};
4+
use libfuzzer_sys::fuzz_target;
5+
use rustworkx_core::petgraph::graph::UnGraph;
6+
use rustworkx_core::planar::is_planar;
7+
8+
#[derive(Debug, Arbitrary)]
9+
struct FuzzGraph {
10+
edges: Vec<(usize, usize)>,
11+
node_count: usize,
12+
}
13+
14+
fuzz_target!(|data: &[u8]| {
15+
if let Ok(fuzz_input) = FuzzGraph::arbitrary(&mut Unstructured::new(data)) {
16+
fuzz_check_is_planar(&fuzz_input);
17+
}
18+
});
19+
20+
fn fuzz_check_is_planar(input: &FuzzGraph) {
21+
if input.node_count == 0 || input.edges.is_empty() || input.node_count > 1000 {
22+
return;
23+
}
24+
25+
let mut graph = UnGraph::<(), ()>::default();
26+
let mut nodes = Vec::with_capacity(input.node_count);
27+
28+
for _ in 0..input.node_count {
29+
nodes.push(graph.add_node(()));
30+
}
31+
32+
for &(u, v) in &input.edges {
33+
if u < input.node_count && v < input.node_count {
34+
graph.add_edge(nodes[u], nodes[v], ());
35+
}
36+
}
37+
38+
let _ = is_planar(&graph);
39+
}

0 commit comments

Comments
 (0)