stack overflow in scripts shouldn't crash the game, fixes #934

This commit is contained in:
gnembon committed 2021-06-25 23:31:46 -03:00
1 parent bf6fddd645
commit 8fa54c8146
3 files changed
+28 -6

No files matched your search

@@ -461,6 +461,10 @@ public class ScriptCommand
{ {
host.handleErrorWithStack("Math doesn't compute", ae); host.handleErrorWithStack("Math doesn't compute", ae);
} }
catch (StackOverflowError soe)
{
host.handleErrorWithStack("Your thoughts are too deep", soe);
}
return 0; return 0;
//host.resetErrorSnooper(); // lets say no need to reset the snooper in case something happens on the way //host.resetErrorSnooper(); // lets say no need to reset the snooper in case something happens on the way
} }
@@ -82,6 +82,10 @@ public class CarpetExpression
{ {
throw new CarpetExpressionException("Math doesn't compute... "+ae.getMessage(), null); 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) public Value scriptRunCommand(ScriptHost host, BlockPos pos)
@@ -115,5 +119,9 @@ public class CarpetExpression
{ {
throw new CarpetExpressionException("Math doesn't compute... "+ae.getMessage(), null); throw new CarpetExpressionException("Math doesn't compute... "+ae.getMessage(), null);
} }
catch (StackOverflowError soe)
{
throw new CarpetExpressionException("Your thoughts are too deep", null);
}
} }
} }
@@ -136,6 +136,10 @@ public class CarpetScriptHost extends ScriptHost
host.handleErrorWithStack("Math doesn't compute", ae); host.handleErrorWithStack("Math doesn't compute", ae);
return null; return null;
} }
catch (StackOverflowError soe)
{
host.handleErrorWithStack("Your thoughts are too deep", soe);
}
finally finally
{ {
host.storeSource = null; host.storeSource = null;
@@ -698,13 +702,16 @@ public class CarpetScriptHost extends ScriptHost
catch (CarpetExpressionException exc) catch (CarpetExpressionException exc)
{ {
handleErrorWithStack("Error while running custom command", exc); handleErrorWithStack("Error while running custom command", exc);
return Value.NULL;
} }
catch (ArithmeticException ae) catch (ArithmeticException ae)
{ {
handleErrorWithStack("Math doesn't compute", 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<Value> args) public Value handleCommand(ServerCommandSource source, FunctionValue function, List<Value> args)
@@ -716,13 +723,16 @@ public class CarpetScriptHost extends ScriptHost
catch (CarpetExpressionException exc) catch (CarpetExpressionException exc)
{ {
handleErrorWithStack("Error while running custom command", exc); handleErrorWithStack("Error while running custom command", exc);
return Value.NULL;
} }
catch (ArithmeticException ae) catch (ArithmeticException ae)
{ {
handleErrorWithStack("Math doesn't compute", 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<Integer> coords, String arg) public Value callLegacy(ServerCommandSource source, String call, List<Integer> coords, String arg)
@@ -1068,13 +1078,13 @@ public class CarpetScriptHost extends ScriptHost
super.resetErrorSnooper(); super.resetErrorSnooper();
} }
public void handleErrorWithStack(String intro, Exception exception) public void handleErrorWithStack(String intro, Throwable exception)
{ {
if (responsibleSource != null) if (responsibleSource != null)
{ {
if (exception instanceof CarpetExpressionException) ((CarpetExpressionException) exception).printStack(responsibleSource); if (exception instanceof CarpetExpressionException) ((CarpetExpressionException) exception).printStack(responsibleSource);
String message = exception.getMessage(); String message = exception.getMessage();
Messenger.m(responsibleSource, "r "+intro+(message.isEmpty()?"":": "+message)); Messenger.m(responsibleSource, "r "+intro+( (message == null || message.isEmpty())?"":": "+message));
} }
else else
{ {