Skip to content

Commit

Permalink
8254742: InputStream::readNBytes(int) result may contain zeros not in…
Browse files Browse the repository at this point in the history
… input

Reviewed-by: shade, bchristi
  • Loading branch information
Brian Burkhalter committed Nov 4, 2020
1 parent 7dcaba6 commit c287170
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/java.base/share/classes/java/io/InputStream.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -416,6 +416,9 @@ public byte[] readNBytes(int len) throws IOException {
if (MAX_BUFFER_SIZE - total < nread) {
throw new OutOfMemoryError("Required array size too large");
}
if (nread < buf.length) {
buf = Arrays.copyOfRange(buf, 0, nread);
}
total += nread;
if (result == null) {
result = buf;
Expand Down
41 changes: 38 additions & 3 deletions test/jdk/java/io/InputStream/ReadNBytes.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,7 +31,7 @@

/*
* @test
* @bug 8080835 8139206
* @bug 8080835 8139206 8254742
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run main ReadNBytes
Expand All @@ -44,6 +44,7 @@ public class ReadNBytes {
private static Random generator = RandomFactory.getRandom();

public static void main(String[] args) throws IOException {
test();
test(new byte[]{1, 2, 3});
test(createRandomBytes(1024));
for (int shift : new int[] {13, 15, 17}) {
Expand Down Expand Up @@ -135,6 +136,19 @@ static void test(int max) throws IOException {
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) {
byte[] bytes = new byte[size];
generator.nextBytes(bytes);
Expand All @@ -150,11 +164,32 @@ static void check(boolean cond, Object ... failedArgs) {
throw new RuntimeException(sb.toString());
}


static class WrapperInputStream extends FilterInputStream {
private boolean closed;
WrapperInputStream(InputStream in) { super(in); }
@Override public void close() throws IOException { closed = true; in.close(); }
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);
}
}
}

0 comments on commit c287170

Please sign in to comment.