Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/lazy values not set crash stack overflow #278

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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