Skip to content

Commit

Permalink
[IO-640] NPE in org.apache.commons.io.IOUtils.contentEquals(InputStream,
Browse files Browse the repository at this point in the history
InputStream) when only one input is null.
  • Loading branch information
Gary Gregory committed Nov 25, 2019
1 parent e6c81e6 commit fa36ecd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ The <action> type attribute can be add,update,fix,remove.
Add and reuse org.apache.commons.io.IOUtils.closeQuitely(Closeable, Consumer&lt;IOException&gt;).
Add and reuse org.apache.commons.io.IOUtils.close(Closeable, IOConsumer&lt;IOException&gt;).
</action>
<action issue="IO-640" dev="ggregory" type="add" due-to="Gary Gregory">
NPE in org.apache.commons.io.IOUtils.contentEquals(InputStream, InputStream) when only one input is null.
</action>
</release>

<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ public static boolean contentEquals(final InputStream input1, final InputStream
if (input1 == input2) {
return true;
}
if (input1 == null ^ input2 == null) {
return false;
}
final BufferedInputStream bufferedInput1 = buffer(input1);
final BufferedInputStream bufferedInput2 = buffer(input2);
int ch = bufferedInput1.read();
Expand Down Expand Up @@ -740,25 +743,26 @@ public static boolean contentEquals(final InputStream input1, final InputStream
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static boolean contentEquals(Reader input1, Reader input2)
@SuppressWarnings("resource")
public static boolean contentEquals(final Reader input1, final Reader input2)
throws IOException {
if (input1 == input2) {
return true;
}

input1 = toBufferedReader(input1);
input2 = toBufferedReader(input2);
BufferedReader bufferedInput1 = toBufferedReader(input1);
BufferedReader bufferedInput2 = toBufferedReader(input2);

int ch = input1.read();
int ch = bufferedInput1.read();
while (EOF != ch) {
final int ch2 = input2.read();
final int ch2 = bufferedInput2.read();
if (ch != ch2) {
return false;
}
ch = input1.read();
ch = bufferedInput1.read();
}

final int ch2 = input2.read();
final int ch2 = bufferedInput2.read();
return ch2 == EOF;
}

Expand All @@ -776,6 +780,7 @@ public static boolean contentEquals(Reader input1, Reader input2)
* @throws IOException if an I/O error occurs
* @since 2.2
*/
@SuppressWarnings("resource")
public static boolean contentEqualsIgnoreEOL(final Reader input1, final Reader input2)
throws IOException {
if (input1 == input2) {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/commons/io/IOUtilsTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ public synchronized void close() throws IOException {
}

@Test public void testContentEquals_InputStream_InputStream() throws Exception {
{
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertTrue(IOUtils.contentEquals((InputStream) null, null));
}
{
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertFalse(IOUtils.contentEquals(input1, null));
}
{
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertFalse(IOUtils.contentEquals(null, input1));
}
{
final ByteArrayInputStream input1 = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
assertTrue(IOUtils.contentEquals(input1, input1));
Expand Down

0 comments on commit fa36ecd

Please sign in to comment.