2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / testsuite / libjava.lang / newarray_overflow.java
blob17370b5374e137ffc0afd995d8ed1808774d953f
1 /* This test checks for two slightly different overflow scenarios in
2 * array allocation.
4 * The first is that the number of bytes needed for an array size
5 * overflows on a 32 bit machine.
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. */
11 class newarray_overflow
13 static boolean failed = false;
15 static void int_check()
17 int[] x;
18 try
20 x = new int [1 << 30];
22 catch (OutOfMemoryError e)
24 return;
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;
32 try
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;
39 catch (Throwable e)
41 System.err.print ("int check: ");
42 System.err.println (e);
43 failed = true;
47 static void object_check()
49 Object[] x;
50 try
52 x = new Object [1 << 30];
53 System.err.println ("Alloc succeeded.");
54 System.err.println (x);
56 catch (OutOfMemoryError e)
58 return;
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;
66 try
68 for (int i = 0; i < x.length; i += (1 << 24))
69 if (x[i] != null)
70 failed = true;
72 catch (Throwable e)
74 System.err.print ("Object check: ");
75 System.err.println (e);
76 failed = true;
80 public static void main (String[] ignore)
82 int_check();
83 object_check();
85 if (!failed)
86 System.out.println ("ok");