You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Singleton, Andrew (SanTech)" <An...@santander.co.uk.INVALID> on 2021/01/21 12:55:47 UTC

[IOUtils] Possible bug in contentEquals(InputStream input1, InputStream input2)

Hi,

I was testing comparing some small input streams, looping through a list to see if a stream was already present and I got unexpected results. The bug can be recreated simply

@Test
void testCompare() throws Exception {
    byte[] b1 = new byte[]{'a','b','c'};
    byte[] b2 = new byte[]{'d','e','f'};
    InputStream is1 = new ByteArrayInputStream(b1);
    InputStream is2 = new ByteArrayInputStream(b2);
    for (int i=0; i<2; i++) {
        assertFalse(IOUtils.contentEquals(is1, is2));
    }
}
[cid:image001.png@01D6EFED.5E2B1E20]

Please ignore if I have misunderstood the meaning of "equals" with regards to InputStream.

Java\jdk1.8.0_241
Commons-io 2.5

Regards

[cid:image004.png@01D6EFF4.BB4FB4B0]



Andrew Singleton

Consultant

Programme Delivery

+44 (0)7729615505 email: Andrew.Singleton@santander.co.uk

Santander House CMK, 201 Grafton Gate East, Milton Keynes, MK9 1AN



Emails aren't always secure, and they may be intercepted or changed after they've been sent. Santander doesn't accept liability if this happens. If you think someone may have interfered with this email, please get in touch with the sender another way. This message doesn't create or change any contract. Santander doesn't accept responsibility for damage caused by any viruses contained in this email or its attachments. Emails may be monitored. If you've received this email by mistake, please let the sender know at once that it's gone to the wrong person and then destroy it without copying, using, or telling anyone about its contents.

Santander UK plc. Registered Office: 2 Triton Square, Regent's Place, London, NW1 3AN, United Kingdom. Registered Number 2294747. Registered in England and Wales. https://www.santander.co.uk. Telephone 0800 389 7000. Calls may be recorded or monitored. Authorised by the Prudential Regulation Authority and regulated by the Financial Conduct Authority and the Prudential Regulation Authority. Our Financial Services Register number is 106054. You can check this on the Financial Services Register by visiting the FCA’s website https://www.fca.org.uk/register.  Santander and the flame logo are registered trademarks.


Ref:[PDB#1-4B]

Re: [IOUtils] Possible bug in contentEquals(InputStream input1, InputStream input2)

Posted by Alex Herbert <al...@gmail.com>.
You are looping over the streams twice.

If you add an assertion error message you will find the failure is on the
second iteration of the loop where the two streams on the second pass are
equal as they both have no bytes left.

You must take care to close your InputStreams after each use and reopen
them each time you wish to compare the entire contents. This can be done
using a try-with-resources block [1]:

  @Test
  void testCompare() throws Exception {
    byte[] b1 = new byte[] {'a', 'b', 'c'};
    byte[] b2 = new byte[] {'d', 'e', 'f'};
    for (int i = 0; i < 2; i++) {
      try (InputStream is1 = new ByteArrayInputStream(b1);
          InputStream is2 = new ByteArrayInputStream(b2)) {
        assertFalse(IOUtils.contentEquals(is1, is2));
      }
    }
  }

[1]
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

On Thu, 21 Jan 2021 at 16:30, Singleton, Andrew (SanTech)
<An...@santander.co.uk.invalid> wrote:

> Hi,
>
>
>
> I was testing comparing some small input streams, looping through a list
> to see if a stream was already present and I got unexpected results. The
> bug can be recreated simply
>
>
>
> @Test
> void testCompare() throws Exception {
>     byte[] b1 = new byte[]{'a','b','c'};
>     byte[] b2 = new byte[]{'d','e','f'};
>     InputStream is1 = new ByteArrayInputStream(b1);
>     InputStream is2 = new ByteArrayInputStream(b2);
>     for (int i=0; i<2; i++) {
>         *assertFalse*(IOUtils.*contentEquals*(is1, is2));
>     }
> }
>
>
>
> Please ignore if I have misunderstood the meaning of “equals” with regards
> to InputStream.
>
>
>
> Java\jdk1.8.0_241
>
> Commons-io 2.5
>
>
>
> Regards
>
>
>
>
>
>
>
> *Andrew Singleton*
>
> Consultant
>
> Programme Delivery
>
> +44 (0)7729615505 email: Andrew.Singleton@santander.co.uk
>
> Santander House CMK, 201 Grafton Gate East, Milton Keynes, MK9 1AN
>
>
>
>
> Emails aren't always secure, and they may be intercepted or changed after
> they've been sent. Santander doesn't accept liability if this happens. If
> you think someone may have interfered with this email, please get in touch
> with the sender another way. This message doesn't create or change any
> contract. Santander doesn't accept responsibility for damage caused by any
> viruses contained in this email or its attachments. Emails may be
> monitored. If you've received this email by mistake, please let the sender
> know at once that it's gone to the wrong person and then destroy it without
> copying, using, or telling anyone about its contents.
>
> Santander UK plc. Registered Office: 2 Triton Square, Regent's Place,
> London, NW1 3AN, United Kingdom. Registered Number 2294747. Registered in
> England and Wales. https://www.santander.co.uk. Telephone 0800 389 7000.
> Calls may be recorded or monitored. Authorised by the Prudential Regulation
> Authority and regulated by the Financial Conduct Authority and the
> Prudential Regulation Authority. Our Financial Services Register number is
> 106054. You can check this on the Financial Services Register by visiting
> the FCA’s website https://www.fca.org.uk/register. Santander and the
> flame logo are registered trademarks.
>
>
> Ref:[PDB#1-4B]
>