diff options
| author | Cliff Click <cliffc@acm.org> | 2024-09-30 09:15:12 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-10-06 08:56:22 -0700 |
| commit | 45ab4741a5ace18e77b027a2f7e318327c33d4b7 (patch) | |
| tree | 3ef67cd27b512fc9832f02f2df48d8ce4cec43a8 /chapter15/src/test | |
| parent | 31ba4ecbc5214b3c25917fd97d030bfd39a784a3 (diff) | |
| download | Simple-45ab4741a5ace18e77b027a2f7e318327c33d4b7.tar.gz Simple-45ab4741a5ace18e77b027a2f7e318327c33d4b7.zip | |
Tests for negative array size and runtime OOB
Diffstat (limited to 'chapter15/src/test')
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java | 28 | ||||
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java | 4 |
2 files changed, 31 insertions, 1 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 2955361a..a3dd48d0 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -89,4 +89,32 @@ int is = new int[2]; try { parser.parse(false).iterate(false); fail(); } catch( Exception e ) { assertEquals("Type *[]int is not of declared type int",e.getMessage()); } } + + @Test + public void testBad2() { + Parser parser = new Parser( +""" +int[] is = new int[3.14]; +return is[1]; +"""); + try { parser.parse(false).iterate(false); fail(); } + catch( Exception e ) { assertEquals("Cannot allocate an array with length 3.14",e.getMessage()); } + } + + @Test + public void testBad3() { + Parser parser = new Parser( +""" +int[] is = new int[arg]; +return is[1]; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return 0;", stop.toString()); + assertEquals(0L, Evaluator.evaluate(stop, 4)); + try { Evaluator.evaluate(stop,0); } + catch( ArrayIndexOutOfBoundsException e ) { assertEquals("Array index 1 out of bounds for array length 0",e.getMessage()); } + try { Evaluator.evaluate(stop,-1); } + catch( NegativeArraySizeException e ) { assertEquals("-1",e.getMessage()); } + } + } diff --git a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java index 40d73b19..e59017e7 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java @@ -91,6 +91,8 @@ public class Evaluator { int base = alloc._ptr._obj.aryBase(); int scale = alloc._ptr._obj.aryScale(); long n = (sz-base)>>scale; + if( n < 0 ) + throw new NegativeArraySizeException(""+n); body = new Object[(int)n+1]; // Array body body[0] = n; // Array length } else { @@ -113,7 +115,7 @@ public class Evaluator { int scale = from.struct.aryScale(); long i = ((Long)off - base)>>scale; if( i < 0 || i >= len ) - throw new RuntimeException("Array index out of bounds " + off + " <= " + len); + throw new ArrayIndexOutOfBoundsException("Array index out of bounds " + off + " <= " + len); return from.fields[(int)i+1]; } else |
