libfat crash if cluster size equal and aligned to write size

Support for the GBA/DS/Gamecube/Wii media card library
Post Reply
oggzee
Posts: 7
Joined: Thu Oct 22, 2009 2:25 am

libfat crash if cluster size equal and aligned to write size

Post by oggzee » Thu Oct 22, 2009 2:31 am

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;

oggzee
Posts: 7
Joined: Thu Oct 22, 2009 2:25 am

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

Post by oggzee » Thu Oct 22, 2009 1:45 pm

ah, the fix is not proper, it causes all files to allocate more clusters than used
so the problem remains

oggzee
Posts: 7
Joined: Thu Oct 22, 2009 2:25 am

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

Post by oggzee » Fri Oct 23, 2009 1:02 pm

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?

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

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

Post by WinterMute » Sat Oct 24, 2009 12:51 am

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 :)
Help keep devkitPro toolchains free, Donate today

Personal Blog

oggzee
Posts: 7
Joined: Thu Oct 22, 2009 2:25 am

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

Post by oggzee » Sat Oct 24, 2009 11:25 pm

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?

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

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

Post by WinterMute » Mon Nov 02, 2009 4:54 pm

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

oggzee
Posts: 7
Joined: Thu Oct 22, 2009 2:25 am

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

Post by oggzee » Wed Nov 04, 2009 1:54 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests