]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
misc: clean up error handling for ext2fs_run_ext3_journal()
authorTheodore Ts'o <tytso@mit.edu>
Sun, 15 Oct 2017 04:22:20 +0000 (00:22 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 15 Oct 2017 04:22:20 +0000 (00:22 -0400)
The ext2fs_run_ext3_journal() function is in debugfs/journal.c, and in
some error conditions cases may close the passed-in file system handle.

Clean up the both the function so that it reliably clears the file
system handle if it has been freed, and its callers so that they do
not crash by dereferencing a null pointer if it has been freed.

Reported-by: Erkki Ruohtula <eru@netti.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
debugfs/journal.c
misc/fuse2fs.c
misc/tune2fs.c

index 9997c7c7c2e0083891ee505a1798d820f9a543c4..56a68be525c92747ed05bd5f49801b43c756316e 100644 (file)
@@ -793,14 +793,14 @@ errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
                kbytes_written = stats->bytes_written >> 10;
 
        ext2fs_mmp_stop(fs);
-       fsname = strdup(fs->device_name);
+       fsname = fs->device_name;
+       fs->device_name = NULL;
        fsflags = fs->flags;
        fsblocksize = fs->blocksize;
        ext2fs_free(fs);
-       retval = ext2fs_open(fsname, fsflags,
-                            0, fsblocksize, io_ptr,
-                            fsp);
-       free(fsname);
+       *fsp = NULL;
+       retval = ext2fs_open(fsname, fsflags, 0, fsblocksize, io_ptr, fsp);
+       ext2fs_free_mem(&fsname);
        if (retval)
                return retval;
 
index b5897685c4662b16a21f7a3d2b9d8864493ac93f..f14cefbadbec9d6c67a9d3739cf64b7577281080 100644 (file)
@@ -3759,7 +3759,7 @@ int main(int argc, char *argv[])
                fctx.err_fp = fopen(logfile, "a");
                if (!fctx.err_fp) {
                        perror(logfile);
-                       goto out_nofs;
+                       goto out;
                }
        } else
                fctx.err_fp = stderr;
@@ -3780,7 +3780,7 @@ int main(int argc, char *argv[])
        if (err) {
                printf(_("%s: %s.\n"), fctx.device, error_message(err));
                printf(_("Please run e2fsck -fy %s.\n"), fctx.device);
-               goto out_nofs;
+               goto out;
        }
        fctx.fs = global_fs;
        global_fs->priv_data = &fctx;
@@ -3869,12 +3869,12 @@ int main(int argc, char *argv[])
 
        ret = 0;
 out:
-       err = ext2fs_close(global_fs);
-       if (err)
-               com_err(argv[0], err, "while closing fs");
-       global_fs = NULL;
-out_nofs:
-
+       if (global_fs) {
+               err = ext2fs_close(global_fs);
+               if (err)
+                       com_err(argv[0], err, "while closing fs");
+               global_fs = NULL;
+       }
        return ret;
 }
 
index 3e7ca23e1784dda4a981058575ff7cb2a8100179..db06003e09b01f19b74ed23b1187bfda609a28df 100644 (file)
@@ -465,9 +465,6 @@ static void request_fsck_afterwards(ext2_filsys fs)
 
 static void convert_64bit(ext2_filsys fs, int direction)
 {
-       if (!direction)
-               return;
-
        /*
         * Is resize2fs going to demand a fsck run? Might as well tell the
         * user now.
@@ -3238,7 +3235,9 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
                if (err) {
                        com_err("tune2fs", err, "while recovering journal.\n");
                        printf(_("Please run e2fsck -fy %s.\n"), argv[1]);
-                       goto closefs;
+                       if (fs)
+                               ext2fs_close_free(&fs);
+                       exit(1);
                }
                ext2fs_clear_feature_journal_needs_recovery(fs->super);
                ext2fs_mark_super_dirty(fs);
@@ -3256,6 +3255,7 @@ closefs:
 #endif
        }
 
-       convert_64bit(fs, feature_64bit);
+       if (feature_64bit)
+               convert_64bit(fs, feature_64bit);
        return (ext2fs_close_free(&fs) ? 1 : 0);
 }