1 /* This test checks for two slightly different overflow scenarios in 2 * array allocation. 3 * 4 * The first is that the number of bytes needed for an array size 5 * overflows on a 32 bit machine. 6 * 7 * The second is that on a 64 machine, the number of bytes silently 8 * gets truncated, resulting in too small an object being 9 * allocated. */ 10 11 class newarray_overflow 12 { 13 static boolean failed = false; 14 int_check()15 static void int_check() 16 { 17 int[] x; 18 try 19 { 20 x = new int [1 << 30]; 21 } 22 catch (OutOfMemoryError e) 23 { 24 return; 25 } 26 /* If we really get away with it (64 bit machine), that's cool. */ 27 if (x == null) { 28 System.err.println ("int check: new returned null."); 29 failed = true; 30 return; 31 } 32 try 33 { 34 // Only check a few places so we don't thrash too badly. 35 for (int i = 0; i < x.length; i += (1 << 24)) 36 if (x[i] != 0) 37 failed = true; 38 } 39 catch (Throwable e) 40 { 41 System.err.print ("int check: "); 42 System.err.println (e); 43 failed = true; 44 } 45 } 46 object_check()47 static void object_check() 48 { 49 Object[] x; 50 try 51 { 52 x = new Object [1 << 30]; 53 System.err.println ("Alloc succeeded."); 54 System.err.println (x); 55 } 56 catch (OutOfMemoryError e) 57 { 58 return; 59 } 60 /* If we really get away with it (64 bit machine), that's cool. */ 61 if (x == null) { 62 System.err.println ("Object check: new returned null."); 63 failed = true; 64 return; 65 } 66 try 67 { 68 for (int i = 0; i < x.length; i += (1 << 24)) 69 if (x[i] != null) 70 failed = true; 71 } 72 catch (Throwable e) 73 { 74 System.err.print ("Object check: "); 75 System.err.println (e); 76 failed = true; 77 } 78 } 79 main(String[] ignore)80 public static void main (String[] ignore) 81 { 82 int_check(); 83 object_check(); 84 85 if (!failed) 86 System.out.println ("ok"); 87 } 88 } 89