Éamonn McManus created IO-871:
---------------------------------

             Summary: IOUtils.contentEquals is incorrect when 
InputStream.available under-reports
                 Key: IO-871
                 URL: https://issues.apache.org/jira/browse/IO-871
             Project: Commons IO
          Issue Type: Bug
          Components: Utilities
    Affects Versions: 2.19.0
            Reporter: Éamonn McManus


A [recent 
change|https://github.com/apache/commons-io/commit/c832eb20796b25d8ee2437a8663616b1112cd7bd]
 to IOUtils.contentEquals(InputStream,InputStream) introduced a bug when at 
least one of the InputStream instances under-reports the number of available 
bytes. That is completely allowed by the contract of InputStream. I think the 
underlying problem may be [this 
line|https://github.com/apache/commons-io/blob/0d41d2fd958f28a0c4eb21afb6f34be8efa0e4fb/src/main/java/org/apache/commons/io/channels/FileChannels.java#L94]
 in FileChannels. There's no requirement for a ReadableByteChannel.read to 
return all available bytes, and therefore it is possible for one of the two 
reads being compared to be shorter than the other, even though the contents of 
the two channels are in fact equal.

This problem showed up when importing the latest source snapshot to Google's 
internal repository and running all of Google's tests against it. Here is a 
simple repro:
{code:java}
    assertThat(
            IOUtils.contentEquals(
                new ByteArrayInputStream("ab".getBytes()),
                new SequenceInputStream(
                    new ByteArrayInputStream("a".getBytes()),
                    new ByteArrayInputStream("b".getBytes()))))
        .isTrue(); {code}
The assertion passes before this change and fails after it.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to