magnusja / libaums

Open source library to access USB Mass Storage devices on Android without rooting your device
Apache License 2.0
1.28k stars 272 forks source link

OutOfMemoryError FAT.getChain() #284

Closed younes127 closed 1 year ago

younes127 commented 3 years ago

This has happened on some devices, I have no further information at the moment.

java.lang.OutOfMemoryError: at java.util.Arrays.copyOf (Arrays.java:3139) at java.util.Arrays.copyOf (Arrays.java:3109) at java.util.ArrayList.grow (ArrayList.java:275) at java.util.ArrayList.ensureExplicitCapacity (ArrayList.java:249) at java.util.ArrayList.ensureCapacityInternal (ArrayList.java:241) at java.util.ArrayList.add (ArrayList.java:467) at com.github.mjdev.libaums.fs.fat32.FAT.getChain$libaums_release (SourceFile:6) at com.github.mjdev.libaums.fs.fat32.ClusterChain. (SourceFile:2) at com.github.mjdev.libaums.fs.fat32.FatDirectory$Companion.readRoot$libaums_release (SourceFile:2) at com.github.mjdev.libaums.fs.fat32.Fat32FileSystem. (SourceFile:7) at com.github.mjdev.libaums.fs.fat32.Fat32FileSystem. (SourceFile:1) at com.github.mjdev.libaums.fs.fat32.Fat32FileSystem$Companion.read (SourceFile:12) at com.github.mjdev.libaums.fs.fat32.Fat32FileSystemCreator.read (SourceFile:1) at com.github.mjdev.libaums.fs.FileSystemFactory.createFileSystem (SourceFile:2) at com.github.mjdev.libaums.partition.Partition$Companion.createPartition (SourceFile:2) at com.github.mjdev.libaums.UsbMassStorageDevice.initPartitions (SourceFile:5) at com.github.mjdev.libaums.UsbMassStorageDevice.setupDevice (SourceFile:14) at com.github.mjdev.libaums.UsbMassStorageDevice.init (SourceFile:2)

I think on some devices this loop never ends.

FAT.kt

do {
            result.add(currentCluster)
            offset = (fatOffset[0] + currentCluster * 4) / bufferSize * bufferSize
            offsetInBlock = (fatOffset[0] + currentCluster * 4) % bufferSize

            // if we have a new offset we are forced to read again
            if (lastOffset != offset) {
                buffer.clear()
                blockDevice.read(offset, buffer)
                lastOffset = offset
            }

            currentCluster = (buffer.getInt(offsetInBlock.toInt()) and 0x0FFFFFFF).toLong()
        } while (currentCluster < FAT32_EOF_CLUSTER)

.. therefore it does not stop adding elements to the list:

result.add (currentCluster)

magnusja commented 3 years ago

Seems that there is no end marker in the cluster chain Might be related to #247

magnusja commented 1 year ago

Fixed with v0.9.4