Skip to content

Commit

Permalink
Merge pull request #278 from CoinFabrik/fix/lazy-values-not-set-crash…
Browse files Browse the repository at this point in the history
…-stack-overflow

Fix/lazy values not set crash stack overflow
  • Loading branch information
tenuki authored Sep 30, 2024
2 parents ced98eb + e5c91c4 commit 68fd15a
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions detectors/lazy-values-not-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate rustc_middle;
extern crate rustc_span;

use std::collections::HashMap;
use std::collections::HashSet;

use clippy_utils::match_def_path;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -125,16 +126,18 @@ impl<'tcx> LateLintPass<'tcx> for LazyValuesNotSet {
}
}

fn clean_local_upwards(local: Local, hm: &HashMap<Local, Vec<Local>>) -> Vec<Local> {
fn clean_local_upwards(local: Local, hm: &HashMap<Local, Vec<Local>>, visited_locals: &mut HashSet<Local>) -> HashSet<Local> {
let val = hm.get(&local);
let mut ret_vec: Vec<Local> = vec![];
if let Some(locals_vec) = val {
ret_vec.extend(locals_vec);
for local in locals_vec {
ret_vec.extend(clean_local_upwards(*local, hm))
let mut ret_vec: HashSet<Local> = HashSet::new();
if ! visited_locals.contains(&local) {
visited_locals.insert(local);
if let Some(locals_vec) = val {
ret_vec.extend(locals_vec);
for local in locals_vec {
ret_vec.extend(clean_local_upwards(*local, hm, visited_locals))
}
}
}
ret_vec.dedup();
ret_vec
}

Expand All @@ -157,7 +160,7 @@ impl LazyValuesNotSet {
let mut lazy_get_tainted_args: Vec<Local> = tainted_get_lazy.to_owned();
let mut span_local: HashMap<Local, Span> = HashMap::new();
let mut locals_dependencies: HashMap<Local, Vec<Local>> = HashMap::new();
let mut locals_to_clean: Vec<Local> = vec![];
let mut locals_to_clean: HashSet<Local> = HashSet::new();
for basicblock in mir_preorder {
for stmt in basicblock.1.statements.iter().rev() {
if let rustc_middle::mir::StatementKind::Assign(box_) = &stmt.kind {
Expand Down Expand Up @@ -231,7 +234,7 @@ impl LazyValuesNotSet {
.retain(|i| a.local != *i);
lazy_get_tainted_args.retain(|i| a.local != *i);
//push locals to be cleaned before
locals_to_clean.push(a.local)
locals_to_clean.insert(a.local);
}
Operand::Constant(_) => {}
}
Expand All @@ -243,7 +246,7 @@ impl LazyValuesNotSet {
for arg in args {
match arg {
Operand::Copy(a) | Operand::Move(a) => {
locals_to_clean.push(a.local);
locals_to_clean.insert(a.local);
}
Operand::Constant(_) => {}
}
Expand All @@ -253,7 +256,7 @@ impl LazyValuesNotSet {
for arg in args {
match arg {
Operand::Copy(a) | Operand::Move(a) => {
locals_to_clean.push(a.local);
locals_to_clean.insert(a.local);
}
Operand::Constant(_) => {}
}
Expand All @@ -277,9 +280,8 @@ impl LazyValuesNotSet {
}
}
for local in locals_to_clean.clone() {
locals_to_clean.extend(clean_local_upwards(local, &locals_dependencies));
locals_to_clean.extend(clean_local_upwards(local, &locals_dependencies, &mut HashSet::new()));
}
locals_to_clean.dedup();
for local in &locals_to_clean {
span_local.remove(local);
}
Expand Down

0 comments on commit 68fd15a

Please sign in to comment.