#2784 fan.sys.FileBuf.toDigest() bug

SlimerDude Wed 26 Feb 2020

Calculating an MD5 digest for large files causes this error to be thrown:

md5 := file.open("r").toDigest("MD5").toHex

sys::IndexErr: java.lang.IndexOutOfBoundsException
  java.io.RandomAccessFile.readBytes (Unknown)
  java.io.RandomAccessFile.read (Unknown)
  fan.sys.FileBuf.toDigest (FileBuf.java:339)

The offending line is this one:

int n = fp.read(temp, 0, (int)Math.min(temp.length, (int)size-total));

which reads data into a temp byte array before passing it to a MessageDigest. It all works hunky dory for small files but for files at around the 2 GB mark an IndexOutOfBoundsException exception is thrown.

I believe the problem is that size of the file (on line 339 above) is being downcast to an int before total is subtracted. This means that for files larger than 2^31 ~> 2 GB, dodgy values for len are being passed to RandomAccessFile.read().

So the fix should be to just change line 339 to the following - note the brackets around (size-total):

int n = fp.read(temp, 0, (int)Math.min(temp.length, (int)(size-total)));

Then it is the result of the calculation which is downcast to an int.

matthew Thu 27 Feb 2020

Ticket promoted to #2784 and assigned to matthew

Hi SlimerDude - I have reproduced this bug and will push a fix shortly.

matthew Thu 27 Feb 2020

Ticket resolved in 1.0.74

Login or Signup to reply.