Skip to content

Commit 1cd85d0

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Enforce bounds checking in the null output stream.
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
1 parent 09960ff commit 1cd85d0

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

android/guava-tests/test/com/google/common/io/ByteStreamsTest.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,49 @@ public void testNullOutputStream() throws Exception {
562562
// write to the output stream
563563
nos.write('n');
564564
String test = "Test string for NullOutputStream";
565-
nos.write(test.getBytes());
566-
nos.write(test.getBytes(), 2, 10);
565+
byte[] bytes = test.getBytes(Charsets.US_ASCII);
566+
nos.write(bytes);
567+
nos.write(bytes, 2, 10);
568+
nos.write(bytes, bytes.length - 5, 5);
567569
// nothing really to assert?
568570
assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream());
569571
}
570572

573+
public void testNullOutputStream_exceptions() throws Exception {
574+
OutputStream nos = ByteStreams.nullOutputStream();
575+
try {
576+
nos.write(null);
577+
fail();
578+
} catch (NullPointerException expected) {
579+
}
580+
try {
581+
nos.write(null, 0, 1);
582+
fail();
583+
} catch (NullPointerException expected) {
584+
}
585+
byte[] tenBytes = new byte[10];
586+
try {
587+
nos.write(tenBytes, -1, 1);
588+
fail("Expected exception from negative offset");
589+
} catch (IndexOutOfBoundsException expected) {
590+
}
591+
try {
592+
nos.write(tenBytes, 1, -1);
593+
fail("Expected exception from negative length");
594+
} catch (IndexOutOfBoundsException expected) {
595+
}
596+
try {
597+
nos.write(tenBytes, 9, 2);
598+
fail("Expected exception from offset+length > array size");
599+
} catch (IndexOutOfBoundsException expected) {
600+
}
601+
try {
602+
nos.write(tenBytes, 9, 100);
603+
fail("Expected exception from offset+length > array size");
604+
} catch (IndexOutOfBoundsException expected) {
605+
}
606+
}
607+
571608
public void testLimit() throws Exception {
572609
byte[] big = newPreFilledByteArray(5);
573610
InputStream bin = new ByteArrayInputStream(big);

android/guava/src/com/google/common/io/ByteStreams.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ public void write(byte[] b) {
671671
@Override
672672
public void write(byte[] b, int off, int len) {
673673
checkNotNull(b);
674+
checkPositionIndexes(off, off + len, b.length);
674675
}
675676

676677
@Override

guava-tests/test/com/google/common/io/ByteStreamsTest.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,49 @@ public void testNullOutputStream() throws Exception {
562562
// write to the output stream
563563
nos.write('n');
564564
String test = "Test string for NullOutputStream";
565-
nos.write(test.getBytes());
566-
nos.write(test.getBytes(), 2, 10);
565+
byte[] bytes = test.getBytes(Charsets.US_ASCII);
566+
nos.write(bytes);
567+
nos.write(bytes, 2, 10);
568+
nos.write(bytes, bytes.length - 5, 5);
567569
// nothing really to assert?
568570
assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream());
569571
}
570572

573+
public void testNullOutputStream_exceptions() throws Exception {
574+
OutputStream nos = ByteStreams.nullOutputStream();
575+
try {
576+
nos.write(null);
577+
fail();
578+
} catch (NullPointerException expected) {
579+
}
580+
try {
581+
nos.write(null, 0, 1);
582+
fail();
583+
} catch (NullPointerException expected) {
584+
}
585+
byte[] tenBytes = new byte[10];
586+
try {
587+
nos.write(tenBytes, -1, 1);
588+
fail("Expected exception from negative offset");
589+
} catch (IndexOutOfBoundsException expected) {
590+
}
591+
try {
592+
nos.write(tenBytes, 1, -1);
593+
fail("Expected exception from negative length");
594+
} catch (IndexOutOfBoundsException expected) {
595+
}
596+
try {
597+
nos.write(tenBytes, 9, 2);
598+
fail("Expected exception from offset+length > array size");
599+
} catch (IndexOutOfBoundsException expected) {
600+
}
601+
try {
602+
nos.write(tenBytes, 9, 100);
603+
fail("Expected exception from offset+length > array size");
604+
} catch (IndexOutOfBoundsException expected) {
605+
}
606+
}
607+
571608
public void testLimit() throws Exception {
572609
byte[] big = newPreFilledByteArray(5);
573610
InputStream bin = new ByteArrayInputStream(big);

guava/src/com/google/common/io/ByteStreams.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ public void write(byte[] b) {
671671
@Override
672672
public void write(byte[] b, int off, int len) {
673673
checkNotNull(b);
674+
checkPositionIndexes(off, off + len, b.length);
674675
}
675676

676677
@Override

0 commit comments

Comments
 (0)