Page 1 of 1

libfat crash if cluster size equal and aligned to write size

Posted: Thu Oct 22, 2009 2:31 am
by oggzee
example with FAT16 and 32k cluster size, a simple write of 32k will crash libfat.
i traced down the problem to be in the last commit, reverting one change fixes it, see below:
is this a valid fix or is there something else to it?

--- libfat-105-x/source/fatfile.c 2009-10-08 22:06:48.000000000 +0200
+++ libfat/source/fatfile.c 2009-10-22 03:15:50.744005200 +0200
@@ -757,12 +757,14 @@
}
}

- size_t chunkSize = partition->bytesPerCluster;
remain);

// Write whole clusters
while ((remain >= partition->bytesPerCluster) && flagNoError) {
uint32_t chunkEnd;
uint32_t nextChunkStart = position.cluster;
+ size_t chunkSize = 0;

do {
chunkEnd = nextChunkStart;

Re: libfat crash if cluster size equal and aligned to write size

Posted: Thu Oct 22, 2009 1:45 pm
by oggzee
ah, the fix is not proper, it causes all files to allocate more clusters than used
so the problem remains

Re: libfat crash if cluster size equal and aligned to write size

Posted: Fri Oct 23, 2009 1:02 pm
by oggzee
Ok i think i made a proper fix that solves both problems that i posted. Should i go ahead and post the diff here? It's not as short as the previous one and i don't know if this is the right place and if anyone is interested? Do only I get corrupted files using libfat?

Re: libfat crash if cluster size equal and aligned to write size

Posted: Sat Oct 24, 2009 12:51 am
by WinterMute
Add it to the patch tracker @ http://sourceforge.net/tracker/?group_i ... tid=668553

I was just about to have a look at that but if you've already solved it then I can move on to something else :)

Re: libfat crash if cluster size equal and aligned to write size

Posted: Sat Oct 24, 2009 11:25 pm
by oggzee
ok, submitted.
https://sourceforge.net/tracker/?func=d ... tid=668553
it of course needs a thorough review and testing.
also i made a wrapper for linux to use libfat in a user process which i used to test and debug the code, do you want me to upload that too somewhere?

Re: libfat crash if cluster size equal and aligned to write size

Posted: Mon Nov 02, 2009 4:54 pm
by WinterMute
Good catch on the bug, thanks for the patch. It's been committed and released now in libfat 1.0.6.

It would be interesting to see your wrapper for testing, mostly I test on hardware atm.

Re: libfat crash if cluster size equal and aligned to write size

Posted: Wed Nov 04, 2009 1:54 pm
by oggzee
Ok, you can find the linux wrapper for testing here:

http://cfg-loader.googlecode.com/files/ ... -linux.tgz

I have already updated it to version 1.0.6 of libfat.
To try it out run this:

cd libfat-106-linux/source
make
cd ../test
make
sh runtest.sh


runtest.sh is this:
dd if=/dev/zero of=fat.dat bs=1M count=10
mkdosfs fat.dat
file fat.dat
./testfat
dosfsck fat.dat
exit
# optional:
mkdir mnt
mount fat.dat mnt -o loop
ls -l mnt
umount mnt
rmdir mnt

hope it helps.

Oh and almost forgot, the dosfsck will complain with:

dosfsck 3.0.1, 23 Nov 2008, FAT32, LFN
FATs differ but appear to be intact. Use which FAT ?
1) Use first FAT
2) Use second FAT
? 1
Leaving file system unchanged.
fat.dat: 11 files, 1164/5101 clusters

Which is another little issue that needs fixing, but this one is not so critical.