Fixes for potential stack overflow

This commit is contained in:
crschnick 2024-10-13 05:40:45 +00:00
parent 62cb424040
commit bd06633931
2 changed files with 21 additions and 13 deletions

View file

@ -788,13 +788,20 @@ public abstract class DataStorage {
public Set<DataStoreEntry> getDeepStoreChildren(DataStoreEntry entry) {
var set = new HashSet<DataStoreEntry>();
getStoreChildren(entry).forEach(c -> {
set.add(c);
set.addAll(getDeepStoreChildren(c));
});
getDeepStoreChildren(entry, set);
return set;
}
private void getDeepStoreChildren(DataStoreEntry entry, Set<DataStoreEntry> current) {
getStoreChildren(entry).forEach(c -> {
var added = current.add(c);
// Guard against loop
if (added) {
getDeepStoreChildren(c, current);
}
});
}
public Set<DataStoreEntry> getStoreChildren(DataStoreEntry entry) {
if (entry.getValidity() == DataStoreEntry.Validity.LOAD_FAILED) {
return Set.of();

View file

@ -87,16 +87,17 @@ public class SimpleScriptStore extends ScriptStore implements ShellInitCommand.T
}
public void queryFlattenedScripts(LinkedHashSet<DataStoreEntryRef<SimpleScriptStore>> all) {
// Prevent loop
DataStoreEntryRef<SimpleScriptStore> ref = getSelfEntry().ref();
all.add(ref);
getEffectiveScripts().stream()
.filter(scriptStoreDataStoreEntryRef -> !all.contains(scriptStoreDataStoreEntryRef))
.forEach(scriptStoreDataStoreEntryRef -> {
scriptStoreDataStoreEntryRef.getStore().queryFlattenedScripts(all);
});
all.remove(ref);
all.add(ref);
var added = all.add(ref);
// Prevent loop
if (added) {
getEffectiveScripts().stream().filter(scriptStoreDataStoreEntryRef -> !all.contains(scriptStoreDataStoreEntryRef)).forEach(
scriptStoreDataStoreEntryRef -> {
scriptStoreDataStoreEntryRef.getStore().queryFlattenedScripts(all);
});
all.remove(ref);
all.add(ref);
}
}
@Override