mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8245036: DataInputStream.readFully(byte[], int, int) does not throw expected IndexOutOfBoundsExceptions
Reviewed-by: bpb
This commit is contained in:
parent
c8c4d8377a
commit
4ac45a3b50
2 changed files with 101 additions and 22 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -25,6 +25,8 @@
|
||||||
|
|
||||||
package java.io;
|
package java.io;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A data input stream lets an application read primitive Java data
|
* A data input stream lets an application read primitive Java data
|
||||||
* types from an underlying input stream in a machine-independent
|
* types from an underlying input stream in a machine-independent
|
||||||
|
@ -192,8 +194,7 @@ public class DataInputStream extends FilterInputStream implements DataInput {
|
||||||
* @see java.io.FilterInputStream#in
|
* @see java.io.FilterInputStream#in
|
||||||
*/
|
*/
|
||||||
public final void readFully(byte b[], int off, int len) throws IOException {
|
public final void readFully(byte b[], int off, int len) throws IOException {
|
||||||
if (len < 0)
|
Objects.checkFromIndexSize(off, len, b.length);
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (n < len) {
|
while (n < len) {
|
||||||
int count = in.read(b, off + n, len - n);
|
int count = in.read(b, off + n, len - n);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -22,30 +22,108 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 4214513
|
* @bug 4214513 8245036
|
||||||
* @summary Passing a negative length argument for readFully must throw
|
* @summary Passing a negative offset or length,
|
||||||
* IndexOutOfBoundsException.
|
* or passing a combination of offset and length too big
|
||||||
|
* for readFully must throw IndexOutOfBoundsException.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class ReadFully {
|
public class ReadFully {
|
||||||
public static final void main(String[] args) throws Exception {
|
|
||||||
byte[] buffer = new byte[100];
|
private static final void testNegativeOffset() throws Exception {
|
||||||
File file = new File(System.getProperty("test.src"),
|
File file = new File(System.getProperty("test.src"),
|
||||||
"ReadFully.java");
|
"ReadFully.java");
|
||||||
FileInputStream in = new FileInputStream(file);
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
DataInputStream dis = new DataInputStream(in);
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, -1, buffer.length);
|
||||||
|
throw new RuntimeException("Test testNegativeOffset() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
boolean caughtException = false;
|
private static final void testNegativeLength() throws Exception {
|
||||||
try {
|
File file = new File(System.getProperty("test.src"),
|
||||||
dis.readFully(buffer, 0, -20);
|
"ReadFully.java");
|
||||||
} catch (IndexOutOfBoundsException ie) {
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
caughtException = true;
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
} finally {
|
byte[] buffer = new byte[100];
|
||||||
dis.close();
|
dis.readFully(buffer, 0, -1);
|
||||||
if (!caughtException)
|
throw new RuntimeException("Test testNegativeLength() failed");
|
||||||
throw new RuntimeException("Test failed");
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final void testNegativeOffsetZeroLength() throws Exception {
|
||||||
|
File file = new File(System.getProperty("test.src"),
|
||||||
|
"ReadFully.java");
|
||||||
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, -1, 0);
|
||||||
|
throw new RuntimeException("Test testNegativeOffsetZeroLength() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final void testBigOffsetLength1() throws Exception {
|
||||||
|
File file = new File(System.getProperty("test.src"),
|
||||||
|
"ReadFully.java");
|
||||||
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, 0, buffer.length + 1);
|
||||||
|
throw new RuntimeException("Test testBigOffsetLength1() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final void testBigOffsetLength2() throws Exception {
|
||||||
|
File file = new File(System.getProperty("test.src"),
|
||||||
|
"ReadFully.java");
|
||||||
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, 1, buffer.length);
|
||||||
|
throw new RuntimeException("Test testBigOffsetLength2() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final void testBigOffsetLength3() throws Exception {
|
||||||
|
File file = new File(System.getProperty("test.src"),
|
||||||
|
"ReadFully.java");
|
||||||
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, buffer.length, 1);
|
||||||
|
throw new RuntimeException("Test testBigOffsetLength3() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final void testBigOffsetLength4() throws Exception {
|
||||||
|
File file = new File(System.getProperty("test.src"),
|
||||||
|
"ReadFully.java");
|
||||||
|
try (FileInputStream in = new FileInputStream(file);
|
||||||
|
DataInputStream dis = new DataInputStream(in);) {
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
dis.readFully(buffer, buffer.length + 1, 0);
|
||||||
|
throw new RuntimeException("Test testBigOffsetLength4() failed");
|
||||||
|
} catch (IndexOutOfBoundsException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void main(String[] args) throws Exception {
|
||||||
|
testNegativeOffset();
|
||||||
|
testNegativeLength();
|
||||||
|
testNegativeOffsetZeroLength();
|
||||||
|
testBigOffsetLength1();
|
||||||
|
testBigOffsetLength2();
|
||||||
|
testBigOffsetLength3();
|
||||||
|
testBigOffsetLength4();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue