openfat
openfat copied to clipboard
found a bug in function fat_read and give a fix
Firstly, thanks to the great work of openfat. I am using it in my own project.
Along my work with openfat, I met a error which was caused by bug here: https://github.com/tmolteno/openfat/blob/97362e141d618cab6a1aef5ad92e1b8083826acf/src/fat_core.c#L218
When "sector % h->fat->sectors_per_cluster == 0" is true, we want to get the next cluster, then get the first sector of that cluster. However, this condition is broken when h->fat->first_data_sector is not zero. For example, sector = 208, h->fat->sectors_per_cluster = 4 , h->fat->first_data_sector = 1, where "sector % h->fat->sectors_per_cluster == 0" is true, but we can't say this sector(208) is the end of cluster, right?
so we need to minus the offset. Bug fix seems like this:
uint32 tmpoff = h->fat->first_data_sector % h->fat->sectors_per_cluster;
if (((sector % h->fat->sectors_per_cluster) - tmpoff) == 0) {
// .....
}
And I test above fix successfuly.