From 7d6bacb824ad889f53d2c7f308f8c83049d767c0 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Wed, 7 Jan 2026 20:31:02 +0200 Subject: [PATCH] Fixed StackOverflows --- .../autocomplete/JSAutocompleteProvider.java | 28 ++++++++---- .../interpreter/js_parser/JSTypeInfo.java | 45 +++++++++++++++++-- .../interpreter/js_parser/JSTypeRegistry.java | 9 ++++ 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JSAutocompleteProvider.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JSAutocompleteProvider.java index e9217a4d3..c594ecd02 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JSAutocompleteProvider.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JSAutocompleteProvider.java @@ -101,13 +101,19 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider { * Recursively add methods from a type and its parents. */ protected void addMethodsFromType(JSTypeInfo type, List items, Set added) { - addMethodsFromType(type, items, added, 0); + addMethodsFromType(type, items, added, 0, new HashSet<>()); } /** - * Recursively add methods from a type and its parents with inheritance depth tracking. + * Recursively add methods from a type and its parents with inheritance depth tracking and cycle detection. */ - private void addMethodsFromType(JSTypeInfo type, List items, Set added, int depth) { + private void addMethodsFromType(JSTypeInfo type, List items, Set added, int depth, Set visited) { + // Prevent infinite recursion from circular inheritance + if (visited.contains(type.getFullName())) { + return; + } + visited.add(type.getFullName()); + for (JSMethodInfo method : type.getMethods().values()) { String name = method.getName(); // Skip overload markers (name$1, name$2, etc.) @@ -122,7 +128,7 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider { // Add from parent type with incremented depth if (type.getResolvedParent() != null) { - addMethodsFromType(type.getResolvedParent(), items, added, depth + 1); + addMethodsFromType(type.getResolvedParent(), items, added, depth + 1, visited); } } @@ -130,13 +136,19 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider { * Recursively add fields from a type and its parents. */ protected void addFieldsFromType(JSTypeInfo type, List items, Set added) { - addFieldsFromType(type, items, added, 0); + addFieldsFromType(type, items, added, 0, new HashSet<>()); } /** - * Recursively add fields from a type and its parents with inheritance depth tracking. + * Recursively add fields from a type and its parents with inheritance depth tracking and cycle detection. */ - private void addFieldsFromType(JSTypeInfo type, List items, Set added, int depth) { + private void addFieldsFromType(JSTypeInfo type, List items, Set added, int depth, Set visited) { + // Prevent infinite recursion from circular inheritance + if (visited.contains(type.getFullName())) { + return; + } + visited.add(type.getFullName()); + for (JSFieldInfo field : type.getFields().values()) { if (!added.contains(field.getName())) { added.add(field.getName()); @@ -146,7 +158,7 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider { // Add from parent type with incremented depth if (type.getResolvedParent() != null) { - addFieldsFromType(type.getResolvedParent(), items, added, depth + 1); + addFieldsFromType(type.getResolvedParent(), items, added, depth + 1, visited); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeInfo.java index 7efdd8481..416720f37 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeInfo.java @@ -89,12 +89,25 @@ public class JSTypeInfo { * Get a method by name, including inherited methods. */ public JSMethodInfo getMethod(String name) { + return getMethod(name, new HashSet<>()); + } + + /** + * Internal method to get method with cycle detection. + */ + private JSMethodInfo getMethod(String name, Set visited) { + // Prevent infinite recursion from circular inheritance + if (visited.contains(this.fullName)) { + return null; + } + visited.add(this.fullName); + JSMethodInfo method = methods.get(name); if (method != null) return method; // Check parent if (resolvedParent != null) { - return resolvedParent.getMethod(name); + return resolvedParent.getMethod(name, visited); } return null; } @@ -103,8 +116,21 @@ public class JSTypeInfo { * Get all methods with a given name (for overloads). */ public List getMethodOverloads(String name) { + return getMethodOverloads(name, new HashSet<>()); + } + + /** + * Internal method to get method overloads with cycle detection. + */ + private List getMethodOverloads(String name, Set visited) { List overloads = new ArrayList<>(); + // Prevent infinite recursion from circular inheritance + if (visited.contains(this.fullName)) { + return overloads; + } + visited.add(this.fullName); + // Get from this type if (methods.containsKey(name)) { overloads.add(methods.get(name)); @@ -118,7 +144,7 @@ public class JSTypeInfo { // Get from parent if (resolvedParent != null) { - overloads.addAll(resolvedParent.getMethodOverloads(name)); + overloads.addAll(resolvedParent.getMethodOverloads(name, visited)); } return overloads; @@ -135,12 +161,25 @@ public class JSTypeInfo { * Get a field by name, including inherited fields. */ public JSFieldInfo getField(String name) { + return getField(name, new HashSet<>()); + } + + /** + * Internal method to get field with cycle detection. + */ + private JSFieldInfo getField(String name, Set visited) { + // Prevent infinite recursion from circular inheritance + if (visited.contains(this.fullName)) { + return null; + } + visited.add(this.fullName); + JSFieldInfo field = fields.get(name); if (field != null) return field; // Check parent if (resolvedParent != null) { - return resolvedParent.getField(name); + return resolvedParent.getField(name, visited); } return null; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeRegistry.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeRegistry.java index 8dfec22cf..dd497e465 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeRegistry.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSTypeRegistry.java @@ -368,7 +368,16 @@ public class JSTypeRegistry { public void resolveInheritance() { for (JSTypeInfo type : types.values()) { JSTypeInfo child = type; + Set visited = new HashSet<>(); while (child != null && child.getExtendsType() != null && child.getResolvedParent() == null) { + // Prevent circular inheritance + if (visited.contains(child.getFullName())) { + System.out.println("WARNING: Circular inheritance detected: " + child.getFullName() + + " extends " + child.getExtendsType()); + break; + } + visited.add(child.getFullName()); + JSTypeInfo parent = getType(child.getExtendsType()); if (parent != null) { child.setResolvedParent(parent);