From 8fa54c81463af091847f4a989f11b6c5ede1ffd0 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 25 Jun 2021 23:31:46 -0300 Subject: [PATCH] stack overflow in scripts shouldn't crash the game, fixes #934 --- .../java/carpet/commands/ScriptCommand.java | 4 ++++ .../java/carpet/script/CarpetExpression.java | 8 +++++++ .../java/carpet/script/CarpetScriptHost.java | 22 ++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index 0afc2858..368dd488 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -461,6 +461,10 @@ public class ScriptCommand { host.handleErrorWithStack("Math doesn't compute", ae); } + catch (StackOverflowError soe) + { + host.handleErrorWithStack("Your thoughts are too deep", soe); + } return 0; //host.resetErrorSnooper(); // lets say no need to reset the snooper in case something happens on the way } diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 67496d99..e23e2971 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -82,6 +82,10 @@ public class CarpetExpression { throw new CarpetExpressionException("Math doesn't compute... "+ae.getMessage(), null); } + catch (StackOverflowError soe) + { + throw new CarpetExpressionException("Your thoughts are too deep", null); + } } public Value scriptRunCommand(ScriptHost host, BlockPos pos) @@ -115,5 +119,9 @@ public class CarpetExpression { throw new CarpetExpressionException("Math doesn't compute... "+ae.getMessage(), null); } + catch (StackOverflowError soe) + { + throw new CarpetExpressionException("Your thoughts are too deep", null); + } } } diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index 4022714d..184d2196 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -136,6 +136,10 @@ public class CarpetScriptHost extends ScriptHost host.handleErrorWithStack("Math doesn't compute", ae); return null; } + catch (StackOverflowError soe) + { + host.handleErrorWithStack("Your thoughts are too deep", soe); + } finally { host.storeSource = null; @@ -698,13 +702,16 @@ public class CarpetScriptHost extends ScriptHost catch (CarpetExpressionException exc) { handleErrorWithStack("Error while running custom command", exc); - return Value.NULL; } catch (ArithmeticException ae) { handleErrorWithStack("Math doesn't compute", ae); - return Value.NULL; } + catch (StackOverflowError soe) + { + handleErrorWithStack("Your thoughts are too deep", soe); + } + return Value.NULL; } public Value handleCommand(ServerCommandSource source, FunctionValue function, List args) @@ -716,13 +723,16 @@ public class CarpetScriptHost extends ScriptHost catch (CarpetExpressionException exc) { handleErrorWithStack("Error while running custom command", exc); - return Value.NULL; } catch (ArithmeticException ae) { handleErrorWithStack("Math doesn't compute", ae); - return Value.NULL; } + catch (StackOverflowError soe) + { + handleErrorWithStack("Your thoughts are too deep", soe); + } + return Value.NULL; } public Value callLegacy(ServerCommandSource source, String call, List coords, String arg) @@ -1068,13 +1078,13 @@ public class CarpetScriptHost extends ScriptHost super.resetErrorSnooper(); } - public void handleErrorWithStack(String intro, Exception exception) + public void handleErrorWithStack(String intro, Throwable exception) { if (responsibleSource != null) { if (exception instanceof CarpetExpressionException) ((CarpetExpressionException) exception).printStack(responsibleSource); String message = exception.getMessage(); - Messenger.m(responsibleSource, "r "+intro+(message.isEmpty()?"":": "+message)); + Messenger.m(responsibleSource, "r "+intro+( (message == null || message.isEmpty())?"":": "+message)); } else {