From 45ab4741a5ace18e77b027a2f7e318327c33d4b7 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 30 Sep 2024 09:15:12 -0700 Subject: Tests for negative array size and runtime OOB --- chapter15/src/main/java/com/seaofnodes/simple/Parser.java | 2 ++ chapter15/src/main/java/com/seaofnodes/simple/node/ReturnNode.java | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'chapter15/src/main/java') diff --git a/chapter15/src/main/java/com/seaofnodes/simple/Parser.java b/chapter15/src/main/java/com/seaofnodes/simple/Parser.java index d9726d28..ec040b4f 100644 --- a/chapter15/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter15/src/main/java/com/seaofnodes/simple/Parser.java @@ -711,6 +711,8 @@ public class Parser { private Node newArray(Type t) { Node len = parseExpression().keep(); + if( !(len._type instanceof TypeInteger) ) + throw error("Cannot allocate an array with length "+len._type); TypeStruct ary = (TypeStruct)TYPES.get("[]"+t.str()); if( ary == null ) { TYPES.put("[]"+t.str(), ary = TypeStruct.makeAry(TypeInteger.BOT,ALIAS++,t,ALIAS++)); diff --git a/chapter15/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter15/src/main/java/com/seaofnodes/simple/node/ReturnNode.java index 444b76a8..e12f1fef 100644 --- a/chapter15/src/main/java/com/seaofnodes/simple/node/ReturnNode.java +++ b/chapter15/src/main/java/com/seaofnodes/simple/node/ReturnNode.java @@ -17,7 +17,8 @@ public class ReturnNode extends CFGNode { public ReturnNode(Node ctrl, Node data, ScopeNode scope) { super(ctrl, data); - // We lookup memory slices by the naming convention that they start with $ + // Add memory slices to Return, so all memory updates are live-on-exit. + // We look up memory slices by the naming convention that they start with $. // We could also use implicit knowledge that all memory projects are at offset >= 2 if( scope != null ) { String[] names = scope.reverseNames(); -- cgit v1.2.3