From a54733d280f393b17e190c668133f241ed091f4b Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 12 Nov 2016 09:31:56 -0500 Subject: [PATCH] debugfs, e2fsck, mke2fs: fix miscellaneous memory leaks detected by ASAN Signed-off-by: Theodore Ts'o --- debugfs/journal.c | 2 ++ e2fsck/e2fsck.c | 4 ++++ e2fsck/pass2.c | 10 ++++++---- lib/support/quotaio.c | 5 ++++- misc/mke2fs.c | 9 ++++++++- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/debugfs/journal.c b/debugfs/journal.c index fcd5d5e..9997c7c 100644 --- a/debugfs/journal.c +++ b/debugfs/journal.c @@ -655,6 +655,8 @@ static void ext2fs_journal_release(ext2_filsys fs, journal_t *journal, if (fs->io != fs->journal_io) io_channel_close(fs->journal_io); fs->journal_io = NULL; + free(fs->journal_name); + fs->journal_name = NULL; } #ifndef USE_INODE_IO diff --git a/e2fsck/e2fsck.c b/e2fsck/e2fsck.c index 6fb2f40..5184a17 100644 --- a/e2fsck/e2fsck.c +++ b/e2fsck/e2fsck.c @@ -146,6 +146,10 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx) ext2fs_u32_list_free(ctx->encrypted_dirs); ctx->encrypted_dirs = 0; } + if (ctx->inode_count) { + ext2fs_free_icount(ctx->inode_count); + ctx->inode_count = 0; + } /* Clear statistic counters */ ctx->fs_directory_count = 0; diff --git a/e2fsck/pass2.c b/e2fsck/pass2.c index 5020a19..11c19e8 100644 --- a/e2fsck/pass2.c +++ b/e2fsck/pass2.c @@ -121,7 +121,7 @@ void e2fsck_pass2(e2fsck_t ctx) if (cd.pctx.errcode) { fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); ctx->flags |= E2F_FLAG_ABORT; - return; + goto cleanup; } buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, "directory scan buffer"); @@ -156,17 +156,17 @@ void e2fsck_pass2(e2fsck_t ctx) } if (ctx->flags & E2F_FLAG_RUN_RETURN) - return; + goto cleanup; if (cd.pctx.errcode) { fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); ctx->flags |= E2F_FLAG_ABORT; - return; + goto cleanup; } for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { if (ctx->flags & E2F_FLAG_SIGNAL_MASK) - return; + goto cleanup; if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) || dx_dir->numblocks == 0) continue; @@ -285,6 +285,8 @@ void e2fsck_pass2(e2fsck_t ctx) } print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io); +cleanup: + ext2fs_free_mem(&buf); } #define MAX_DEPTH 32000 diff --git a/lib/support/quotaio.c b/lib/support/quotaio.c index c250e31..befa832 100644 --- a/lib/support/quotaio.c +++ b/lib/support/quotaio.c @@ -243,13 +243,16 @@ errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h, if (qctx->quota_file[qtype]) { h = qctx->quota_file[qtype]; if (((flags & EXT2_FILE_WRITE) == 0) || - (h->qh_file_flags & EXT2_FILE_WRITE)) + (h->qh_file_flags & EXT2_FILE_WRITE)) { + ext2fs_file_close(e2_file); return 0; + } (void) quota_file_close(qctx, h); } err = ext2fs_get_mem(sizeof(struct quota_handle), &h); if (err) { log_err("Unable to allocate quota handle"); + ext2fs_file_close(e2_file); return err; } allocated_handle = 1; diff --git a/misc/mke2fs.c b/misc/mke2fs.c index 54dd926..9f18c83 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -632,6 +632,7 @@ write_superblock: retval = io_channel_write_blk64(fs->io, fs->super->s_first_data_block+1, 1, buf); + (void) ext2fs_free_mem(&buf); if (retval) { com_err("create_journal_dev", retval, "%s", _("while writing journal superblock")); @@ -1138,7 +1139,7 @@ struct str_list { static errcode_t init_list(struct str_list *sl) { sl->num = 0; - sl->max = 0; + sl->max = 1; sl->list = malloc((sl->max+1) * sizeof(char *)); if (!sl->list) return ENOMEM; @@ -2105,6 +2106,12 @@ profile_error: ext2fs_blocks_count_set(&fs_param, fs_blocks_count); if (ext2fs_has_feature_journal_dev(&fs_param)) { + int i; + + for (i=0; fs_types[i]; i++) { + free(fs_types[i]); + fs_types[i] = 0; + } fs_types[0] = strdup("journal"); fs_types[1] = 0; } -- 1.8.3.1