fix: add a breaker to avoid infinite loops from source root cycles

This commit is contained in:
David Hewitt 2024-06-13 06:23:49 +01:00
parent 51ea7e8318
commit a29d99de3b

View file

@ -2531,6 +2531,7 @@ macro_rules! _impl_for_config_data {
#[allow(non_snake_case)]
$vis fn $field(&self, source_root: Option<SourceRootId>) -> &$ty {
let mut par: Option<SourceRootId> = source_root;
let mut traversals = 0;
while let Some(source_root_id) = par {
par = self.source_root_parent_map.get(&source_root_id).copied();
if let Some((config, _)) = self.ratoml_files.get(&source_root_id) {
@ -2538,6 +2539,14 @@ macro_rules! _impl_for_config_data {
return value;
}
}
// Prevent infinite loops caused by cycles by giving up when it's
// clear that we must have either visited all source roots or
// encountered a cycle.
traversals += 1;
if traversals >= self.source_root_parent_map.len() {
// i.e. no source root contains the config we're looking for
break;
}
}
if let Some((root_path_ratoml, _)) = self.root_ratoml.as_ref() {