Skip to content

Commit

Permalink
Enforce bounds checking in the null output stream.
Browse files Browse the repository at this point in the history
This is consistent with the contract of `OutputStream.write`.

RELNOTES=`ByteStreams.nullOutputStream()` now follows the contract of `OutputStream.write` by throwing an exception if the range of bytes is out of bounds.
PiperOrigin-RevId: 428511460
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Feb 14, 2022
1 parent 09960ff commit 1cd85d0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
41 changes: 39 additions & 2 deletions android/guava-tests/test/com/google/common/io/ByteStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,49 @@ public void testNullOutputStream() throws Exception {
// write to the output stream
nos.write('n');
String test = "Test string for NullOutputStream";
nos.write(test.getBytes());
nos.write(test.getBytes(), 2, 10);
byte[] bytes = test.getBytes(Charsets.US_ASCII);
nos.write(bytes);
nos.write(bytes, 2, 10);
nos.write(bytes, bytes.length - 5, 5);
// nothing really to assert?
assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream());
}

public void testNullOutputStream_exceptions() throws Exception {
OutputStream nos = ByteStreams.nullOutputStream();
try {
nos.write(null);
fail();
} catch (NullPointerException expected) {
}
try {
nos.write(null, 0, 1);
fail();
} catch (NullPointerException expected) {
}
byte[] tenBytes = new byte[10];
try {
nos.write(tenBytes, -1, 1);
fail("Expected exception from negative offset");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 1, -1);
fail("Expected exception from negative length");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 9, 2);
fail("Expected exception from offset+length > array size");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 9, 100);
fail("Expected exception from offset+length > array size");
} catch (IndexOutOfBoundsException expected) {
}
}

public void testLimit() throws Exception {
byte[] big = newPreFilledByteArray(5);
InputStream bin = new ByteArrayInputStream(big);
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/io/ByteStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ public void write(byte[] b) {
@Override
public void write(byte[] b, int off, int len) {
checkNotNull(b);
checkPositionIndexes(off, off + len, b.length);
}

@Override
Expand Down
41 changes: 39 additions & 2 deletions guava-tests/test/com/google/common/io/ByteStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,49 @@ public void testNullOutputStream() throws Exception {
// write to the output stream
nos.write('n');
String test = "Test string for NullOutputStream";
nos.write(test.getBytes());
nos.write(test.getBytes(), 2, 10);
byte[] bytes = test.getBytes(Charsets.US_ASCII);
nos.write(bytes);
nos.write(bytes, 2, 10);
nos.write(bytes, bytes.length - 5, 5);
// nothing really to assert?
assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream());
}

public void testNullOutputStream_exceptions() throws Exception {
OutputStream nos = ByteStreams.nullOutputStream();
try {
nos.write(null);
fail();
} catch (NullPointerException expected) {
}
try {
nos.write(null, 0, 1);
fail();
} catch (NullPointerException expected) {
}
byte[] tenBytes = new byte[10];
try {
nos.write(tenBytes, -1, 1);
fail("Expected exception from negative offset");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 1, -1);
fail("Expected exception from negative length");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 9, 2);
fail("Expected exception from offset+length > array size");
} catch (IndexOutOfBoundsException expected) {
}
try {
nos.write(tenBytes, 9, 100);
fail("Expected exception from offset+length > array size");
} catch (IndexOutOfBoundsException expected) {
}
}

public void testLimit() throws Exception {
byte[] big = newPreFilledByteArray(5);
InputStream bin = new ByteArrayInputStream(big);
Expand Down
1 change: 1 addition & 0 deletions guava/src/com/google/common/io/ByteStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ public void write(byte[] b) {
@Override
public void write(byte[] b, int off, int len) {
checkNotNull(b);
checkPositionIndexes(off, off + len, b.length);
}

@Override
Expand Down

0 comments on commit 1cd85d0

Please sign in to comment.