8254742: InputStream::readNBytes(int) result may contain zeros not in input
Reviewed-by: shade, bchristi
This commit is contained in:
parent
7dcaba6312
commit
c287170c94
@ -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
|
||||||
@ -416,6 +416,9 @@ public abstract class InputStream implements Closeable {
|
|||||||
if (MAX_BUFFER_SIZE - total < nread) {
|
if (MAX_BUFFER_SIZE - total < nread) {
|
||||||
throw new OutOfMemoryError("Required array size too large");
|
throw new OutOfMemoryError("Required array size too large");
|
||||||
}
|
}
|
||||||
|
if (nread < buf.length) {
|
||||||
|
buf = Arrays.copyOfRange(buf, 0, nread);
|
||||||
|
}
|
||||||
total += nread;
|
total += nread;
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
result = buf;
|
result = buf;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2015, 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
|
||||||
@ -31,7 +31,7 @@ import jdk.test.lib.RandomFactory;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8080835 8139206
|
* @bug 8080835 8139206 8254742
|
||||||
* @library /test/lib
|
* @library /test/lib
|
||||||
* @build jdk.test.lib.RandomFactory
|
* @build jdk.test.lib.RandomFactory
|
||||||
* @run main ReadNBytes
|
* @run main ReadNBytes
|
||||||
@ -44,6 +44,7 @@ public class ReadNBytes {
|
|||||||
private static Random generator = RandomFactory.getRandom();
|
private static Random generator = RandomFactory.getRandom();
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
test();
|
||||||
test(new byte[]{1, 2, 3});
|
test(new byte[]{1, 2, 3});
|
||||||
test(createRandomBytes(1024));
|
test(createRandomBytes(1024));
|
||||||
for (int shift : new int[] {13, 15, 17}) {
|
for (int shift : new int[] {13, 15, 17}) {
|
||||||
@ -135,6 +136,19 @@ public class ReadNBytes {
|
|||||||
check(!in.isClosed(), "Stream unexpectedly closed");
|
check(!in.isClosed(), "Stream unexpectedly closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test() throws IOException {
|
||||||
|
final int chunkSize = 8192;
|
||||||
|
int size = (10 + generator.nextInt(11))*chunkSize;
|
||||||
|
|
||||||
|
byte[] buf = new byte[size];
|
||||||
|
generator.nextBytes(buf);
|
||||||
|
InputStream s = new ThrottledByteArrayInputStream(buf);
|
||||||
|
|
||||||
|
byte[] b = s.readNBytes(size);
|
||||||
|
|
||||||
|
check(Arrays.equals(b, buf), "Arrays not equal");
|
||||||
|
}
|
||||||
|
|
||||||
static byte[] createRandomBytes(int size) {
|
static byte[] createRandomBytes(int size) {
|
||||||
byte[] bytes = new byte[size];
|
byte[] bytes = new byte[size];
|
||||||
generator.nextBytes(bytes);
|
generator.nextBytes(bytes);
|
||||||
@ -150,11 +164,32 @@ public class ReadNBytes {
|
|||||||
throw new RuntimeException(sb.toString());
|
throw new RuntimeException(sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static class WrapperInputStream extends FilterInputStream {
|
static class WrapperInputStream extends FilterInputStream {
|
||||||
private boolean closed;
|
private boolean closed;
|
||||||
WrapperInputStream(InputStream in) { super(in); }
|
WrapperInputStream(InputStream in) { super(in); }
|
||||||
@Override public void close() throws IOException { closed = true; in.close(); }
|
@Override public void close() throws IOException { closed = true; in.close(); }
|
||||||
boolean isClosed() { return closed; }
|
boolean isClosed() { return closed; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class ThrottledByteArrayInputStream extends ByteArrayInputStream {
|
||||||
|
private int count = 0;
|
||||||
|
|
||||||
|
ThrottledByteArrayInputStream(byte[] buf) {
|
||||||
|
super(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
//
|
||||||
|
// Sometimes return zero or a smaller count than requested.
|
||||||
|
//
|
||||||
|
public int read(byte[] buf, int off, int len) {
|
||||||
|
if (generator.nextBoolean()) {
|
||||||
|
return 0;
|
||||||
|
} else if (++count / 3 == 0) {
|
||||||
|
len /= 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.read(buf, off, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user