author | jbachorik |
Tue, 21 Jan 2014 13:04:55 +0100 | |
changeset 22353 | d09e3ff5fd63 |
parent 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
25 |
* @test |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
26 |
* @bug 4017728 4079849 6788196 |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
27 |
* @summary Check for correct Array Bounds check in read of FileInputStream and |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
28 |
* RandomAccessFile |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
29 |
*/ |
2 | 30 |
|
31 |
import java.io.*; |
|
32 |
||
33 |
/* |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
34 |
* The test calls the read(byte buf[] , int off , int len) of |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
35 |
* FileInputStream with different values of off and len to see if the |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
36 |
* IndexOutOfBoundsException is thrown. The read(...) method calls |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
37 |
* readBytes(...) in native code(io_util.c). The read(...) method in |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
38 |
* RandomAccessFile also calls the same native method. So one should |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
39 |
* see similar results. |
2 | 40 |
*/ |
41 |
||
42 |
public class ReadBytesBounds { |
|
43 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
44 |
static final FileInputStream fis; |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
45 |
static final RandomAccessFile raf; |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
46 |
static final byte[] b = new byte[32]; |
2 | 47 |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
48 |
static { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
49 |
try { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
50 |
String dir = System.getProperty("test.src", "."); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
51 |
File testFile = new File(dir, "input.txt"); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
52 |
fis = new FileInputStream(testFile); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
53 |
raf = new RandomAccessFile(testFile , "r"); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
54 |
} catch (Throwable t) { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
55 |
throw new Error(t); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
56 |
} |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
57 |
} |
2 | 58 |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
59 |
public static void main(String argv[]) throws Throwable { |
5810
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
60 |
try { |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
61 |
testRead(-1, -1, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
62 |
testRead(-1, 0, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
63 |
testRead( 0, -1, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
64 |
testRead( 0, 33, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
65 |
testRead(33, 0, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
66 |
testRead(33, 4, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
67 |
testRead( 0, 32, true); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
68 |
testRead(32, 0, true); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
69 |
testRead(32, 4, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
70 |
testRead( 4, 16, true); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
71 |
testRead( 1, 31, true); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
72 |
testRead( 0, 0, true); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
73 |
testRead(31, Integer.MAX_VALUE, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
74 |
testRead( 0, Integer.MAX_VALUE, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
75 |
testRead(-1, Integer.MAX_VALUE, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
76 |
testRead(-4, Integer.MIN_VALUE, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
77 |
testRead( 0, Integer.MIN_VALUE, false); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
78 |
} finally { |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
79 |
fis.close(); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
80 |
raf.close(); |
e83d67ad8c96
6962419: TEST_BUG: java_io tests fails in samevm mode
alanb
parents:
5506
diff
changeset
|
81 |
} |
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
82 |
} |
2 | 83 |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
84 |
static void testRead(int off, int len, boolean expected) throws Throwable { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
85 |
System.err.printf("off=%d len=%d expected=%b%n", off, len, expected); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
86 |
boolean result; |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
87 |
try { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
88 |
fis.read(b, off, len); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
89 |
raf.read(b, off, len); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
90 |
result = true; |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
91 |
} catch (IndexOutOfBoundsException e) { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
92 |
result = false; |
2 | 93 |
} |
94 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
95 |
if (result != expected) { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
96 |
throw new RuntimeException |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
97 |
(String.format("Unexpected result off=%d len=%d expected=%b", |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
98 |
off, len, expected)); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
2
diff
changeset
|
99 |
} |
2 | 100 |
} |
101 |
} |