Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit a711f2b

Browse files
Chadderz121WinterMute
authored andcommitted
Fix concurrency bugs in FAT_getAttr/FAT_setAttr. (#13)
A relatively rare concurrency bug existed in these methods due to them not correctly holding the partition lock during calls to _FAT_directory_entryFromPath. That method can trigger reads to the partition. Due to the lock not being held, another thread could still trigger a concurrent read, potentially corrupting the cache. The fix is simply to ensure the lock is held around the calls.
1 parent e7b04a7 commit a711f2b

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

source/fatfile.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "lock.h"
4747

4848
bool _FAT_findEntry(const char *path, DIR_ENTRY *dirEntry) {
49+
bool r;
4950
PARTITION *partition = _FAT_partition_getPartitionFromPath(path);
5051

5152
// Check Partition
@@ -61,8 +62,11 @@ bool _FAT_findEntry(const char *path, DIR_ENTRY *dirEntry) {
6162
}
6263

6364
// Search for the file on the disc
64-
return _FAT_directory_entryFromPath (partition, dirEntry, path, NULL);
65+
_FAT_lock(&partition->lock);
66+
r = _FAT_directory_entryFromPath (partition, dirEntry, path, NULL);
67+
_FAT_unlock(&partition->lock);
6568

69+
return r;
6670
}
6771

6872
int FAT_getAttr(const char *file) {
@@ -92,17 +96,18 @@ int FAT_setAttr(const char *file, uint8_t attr) {
9296
if (strchr (file, ':') != NULL)
9397
return -1;
9498

99+
// Lock Partition
100+
_FAT_lock(&partition->lock);
101+
95102
// Get DIR_ENTRY
96-
if( !_FAT_directory_entryFromPath (partition, &dirEntry, file, NULL) )
103+
if( !_FAT_directory_entryFromPath (partition, &dirEntry, file, NULL) ) {
104+
_FAT_unlock(&partition->lock); // Unlock Partition
97105
return -1;
106+
}
98107

99108
// Get Entry-End
100109
entryEnd = dirEntry.dataEnd;
101110

102-
// Lock Partition
103-
_FAT_lock(&partition->lock);
104-
105-
106111
// Write Data
107112
_FAT_cache_writePartialSector (
108113
partition->cache // Cache to write

0 commit comments

Comments
 (0)