From 1a1ba0053830b98e99f9b2713f64dbcb36e2a6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= Date: Sun, 18 Nov 2018 20:47:29 +0100 Subject: [PATCH] Fix memory leaks in read_fat() function Function read_fat() allocates memory to the user supplied buffer. Therefore that function needs complement function for releasing allocated memory and user needs to call if after finish its work. This patch fixes memory leaks in fsck.fat and fatlabel tools. Conflicts: src/fsck.fat.c src/fatlabel.c [Zhiqiang Liu modifies context] Fixes #13 --- src/fat.c | 17 +++++++++++------ src/fat.h | 5 +++++ src/fsck.fat.c | 1 + 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/fat.c b/src/fat.c index d994e8e..849c758 100644 --- a/src/fat.c +++ b/src/fat.c @@ -75,6 +75,16 @@ void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs) } } +void release_fat(DOS_FS * fs) +{ + if (fs->fat) + free(fs->fat); + if (fs->cluster_owner) + free(fs->cluster_owner); + fs->fat = NULL; + fs->cluster_owner = NULL; +} + /** * Build a bookkeeping structure from the partition's FAT table. * If the partition has multiple FATs and they don't agree, try to pick a winner, @@ -92,12 +102,7 @@ void read_fat(DOS_FS * fs) uint32_t total_num_clusters; /* Clean up from previous pass */ - if (fs->fat) - free(fs->fat); - if (fs->cluster_owner) - free(fs->cluster_owner); - fs->fat = NULL; - fs->cluster_owner = NULL; + release_fat(fs); total_num_clusters = fs->data_clusters + 2; eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL; diff --git a/src/fat.h b/src/fat.h index 5c77634..f9b7643 100644 --- a/src/fat.h +++ b/src/fat.h @@ -28,6 +28,11 @@ void read_fat(DOS_FS * fs); /* Loads the FAT of the filesystem described by FS. Initializes the FAT, replaces broken FATs and rejects invalid cluster entries. */ +void release_fat(DOS_FS * fs); + +/* Release the FAT of the filesystem described by FS and free allocated memory. + Call it after finish work with FAT. */ + void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs); /* Retrieve the FAT entry (next chained cluster) for CLUSTER. */ diff --git a/src/fsck.fat.c b/src/fsck.fat.c index c244aba..b1aafe9 100644 --- a/src/fsck.fat.c +++ b/src/fsck.fat.c @@ -203,6 +203,7 @@ int main(int argc, char **argv) reclaim_free(&fs); qfree(&mem_queue); } + release_fat(&fs); exit: if (fs_changed()) { -- 1.8.3.1