[parted-devel] [PATCH 4/6] fat_op_context_new: free ctx->remap and goto correct label to avoid memleak

Wu Guanghao wuguanghao3 at huawei.com
Wed Dec 15 08:30:02 GMT 2021


When calc_deltas returns an error, need to release ctx->remap,
and eed to jump to the correct label to release ctx, otherwise
there will be memory leaks

Signed-off-by: Wu Guanghao <wuguanghao3 at huawei.com>
---
 libparted/fs/r/fat/context.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libparted/fs/r/fat/context.c b/libparted/fs/r/fat/context.c
index 5ca154c..fce77af 100644
--- a/libparted/fs/r/fat/context.c
+++ b/libparted/fs/r/fat/context.c
@@ -86,9 +86,9 @@ fat_op_context_new (PedFileSystem* new_fs, PedFileSystem* old_fs)
 	ctx->frag_sectors = PED_MIN (old_fs_info->cluster_sectors,
 				     new_fs_info->cluster_sectors);
 	if (!fat_set_frag_sectors (new_fs, ctx->frag_sectors))
-		goto error;
+		goto error_free_ctx;
 	if (!fat_set_frag_sectors (old_fs, ctx->frag_sectors))
-		goto error;
+		goto error_free_ctx;
 
 	ctx->buffer_frags = old_fs_info->buffer_sectors / ctx->frag_sectors;
 	ctx->buffer_map = (FatFragment*) ped_malloc (sizeof (FatFragment)
@@ -104,10 +104,12 @@ fat_op_context_new (PedFileSystem* new_fs, PedFileSystem* old_fs)
 	ctx->new_fs = new_fs;
 	ctx->old_fs = old_fs;
 	if (!calc_deltas (ctx))
-		goto error_free_buffer_map;
+		goto error_free_remap;
 
 	return ctx;
 
+error_free_remap:
+	free(ctx->remap);
 error_free_buffer_map:
 	free (ctx->buffer_map);
 error_free_ctx:
-- 
2.27.0




More information about the parted-devel mailing list