e2fsprogs: Misc cleanups & fixes
cover e2fsprogs: Misc cleanups & fixes
I ran Coverity over e2fsprogs this week, hoping to find any recent-ish flaws, but most of what it came up with seems longstanding. I guess that's a good sign for the recent work, anyway!
Some of these are just tidyups, some are bugfixes. It gets the defect count down to around 25 from 70 to start with, but I'll need to take a deep breath before tackling the rest.
Thanks,
-Eric
PATCH 1/25 libext2: Fix EXT2_LIB_SOFTSUPP masking
EXT2_LIB_SOFTSUPP_INCOMPAT_* are supposed to be bitmasks of features which can be opened even though they are under development. The intent is that these are masked out of the features list, so that they will be ignored on open.
However, the code does a logical not vs. a bitwise not:
features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
which will not have the desired effect...
--- lib/ext2fs/openfs.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
lib/ext2fs/openfs.c +2 -2
@@ -214,7 +214,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options, features = fs->super->s_feature_incompat;#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT if (flags &EXT2_FLAG_SOFTSUPP_FEATURES ) - features &= !EXT2_LIB_SOFTSUPP_INCOMPAT ; + features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT ;#endif if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP ) { retval =EXT2_ET_UNSUPP_FEATURE ;
@@ -224,7 +224,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options, features = fs->super->s_feature_ro_compat;#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT if (flags &EXT2_FLAG_SOFTSUPP_FEATURES ) - features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT ; + features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT ;#endif if ((flags &EXT2_FLAG_RW ) && (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP )) {
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 01/25] libext2: Fix EXT2_LIB_SOFTSUPP masking
Quoted 6 lines · Eric Sandeen
EXT2_LIB_SOFTSUPP_INCOMPAT_* are supposed to be bitmasks of features which can be opened even though they are under development. The intent is that these are masked out of the features list, so that they will be ignored on open.
Quoted 7 lines
However, the code does a logical not vs. a bitwise not:
features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;which will not have the desired effect...
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 2/25 e2fsprogs: Remove impossible name_len tests.
In reply to Eric SandeenThe name_len field in ext2_dir_entry is actually comprised of the name length in the lower 8 bytes, and the filetype in the high 8 bytes. So in places, we mask name_len with 0xFF to get the actual length.
But once we have masked name_len with 0xFF, there is no point in testing whether it is greater than EXT2_NAME_LEN, which is 255 - or 0xFF. So all of these tests are extraneous.
--- debugfs/dump.c | 3 +-- debugfs/htree.c | 3 +-- debugfs/ls.c | 3 +-- e2fsck/message.c | 2 -- e2fsck/pass2.c | 6 ------ 5 files changed, 3 insertions(+), 14 deletions(-)
debugfs/dump.c +1 -2
@@ -300,8 +300,7 @@ static int rdump_dirent(struct ext2_dir_entry *dirent,const char *dumproot = private;struct ext2_inode inode; - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN - ? (dirent->name_len &0xFF ) :EXT2_NAME_LEN ); + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] =0 ;
debugfs/htree.c +1 -2
@@ -81,8 +81,7 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino, blk);break ; } - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN ) ? - (dirent->name_len &0xFF ) :EXT2_NAME_LEN ; + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] ='\0' ; errcode = ext2fs_dirhash(hash_alg, name,
debugfs/ls.c +1 -2
@@ -60,8 +60,7 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),int thislen;struct list_dir_struct *ls = (struct list_dir_struct *) private; - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN ) ? - (dirent->name_len &0xFF ) :EXT2_NAME_LEN ; + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] ='\0' ; ino = dirent->inode;
e2fsck/message.c +0 -2
@@ -372,8 +372,6 @@ static _INLINE_ void expand_dirent_expression(ext2_filsys fs, char ch,break ;case 'n' : len = dirent->name_len &0xFF ; -if (len >EXT2_NAME_LEN ) - len =EXT2_NAME_LEN ;if ((ext2fs_get_rec_len (fs, dirent, &rec_len) ==0 ) && (len > rec_len)) len = rec_len;
e2fsck/pass2.c +0 -6
@@ -858,12 +858,6 @@ out_htree: }else goto abort_free_dict; } -if ((dirent->name_len &0xFF ) >EXT2_NAME_LEN ) { -if (fix_problem (ctx,PR_2_FILENAME_LONG , &cd->pctx)) { - dirent->name_len =EXT2_NAME_LEN ; - dir_modified++; - } - }if (dot_state ==0 ) {if (check_dot (ctx, dirent, ino, &cd->pctx))
Signature · 1 line
1.7.4.1
Re: [PATCH 02/25] e2fsprogs: Remove impossible name_len tests.
Quoted 8 lines · Eric Sandeen
The
name_lenfield inext2_dir_entryis actually comprised of the name length in the lower 8 bytes, and the filetype in the high 8 bytes. So in places, we maskname_lenwith0xFFto get the actual length.But once we have masked
name_lenwith0xFF, there is no point in testing whether it is greater thanEXT2_NAME_LEN, which is 255 - or0xFF. So all of these tests are extraneous.
This makes me want to remove uses of ext2_dir_entry and instead use ext2_dir_entry_2 everywhere. That will avoid a lot of potential errors in the code, and I think the only possible use for the old ext2_dir_entry is for ancient big-endian filesystems that were "v1" format and didn't have EXT2_INCOMPAT_FILETYPE (which appeared in e2fsprogs commit 89fe431a8 from Linux 2.1.90).
Quoted 88 lines
Signed-off-by Eric Sandeen <[email protected]>---
debugfs/dump.c | 3 +-- debugfs/htree.c | 3 +-- debugfs/ls.c | 3 +-- e2fsck/message.c | 2 -- e2fsck/pass2.c | 6 ------ 5 files changed, 3 insertions(+), 14 deletions(-)debugfs/dump.c +1
-2@@ -300,8 +300,7 @@ static int rdump_dirent(struct ext2_dir_entry *dirent,const char *dumproot = private;struct ext2_inode inode; - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN - ? (dirent->name_len &0xFF ) :EXT2_NAME_LEN ); + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] =0 ;debugfs/htree.c +1
-2@@ -81,8 +81,7 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino, blk);break ; } - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN ) ? - (dirent->name_len &0xFF ) :EXT2_NAME_LEN ; + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] ='\0' ; errcode = ext2fs_dirhash(hash_alg, name,debugfs/ls.c +1
-2@@ -60,8 +60,7 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),int thislen;struct list_dir_struct *ls = (struct list_dir_struct *) private; - thislen = ((dirent->name_len &0xFF ) <EXT2_NAME_LEN ) ? - (dirent->name_len &0xFF ) :EXT2_NAME_LEN ; + thislen = dirent->name_len &0xFF ;strncpy (name, dirent->name, thislen); name[thislen] ='\0' ; ino = dirent->inode;e2fsck/message.c +0
-2@@ -372,8 +372,6 @@ static _INLINE_ void expand_dirent_expression(ext2_filsys fs, char ch,break ;case 'n' : len = dirent->name_len &0xFF ; -if (len >EXT2_NAME_LEN ) - len =EXT2_NAME_LEN ;if ((ext2fs_get_rec_len (fs, dirent, &rec_len) ==0 ) && (len > rec_len)) len = rec_len;e2fsck/pass2.c +0
-6@@ -858,12 +858,6 @@ out_htree: }else goto abort_free_dict; } -if ((dirent->name_len &0xFF ) >EXT2_NAME_LEN ) { -if (fix_problem (ctx,PR_2_FILENAME_LONG , &cd->pctx)) { - dirent->name_len =EXT2_NAME_LEN ; - dir_modified++; - } - }if (dot_state ==0 ) {if (check_dot (ctx, dirent, ino, &cd->pctx))Signature · 6 lines
1.7.4.1 -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Cheers, Andreas
Ted Ts'o
AppliedRe: [PATCH 02/25] e2fsprogs: Remove impossible name_len tests.
In reply to Eric SandeenQuoted 10 lines · Eric Sandeen
The
name_lenfield inext2_dir_entryis actually comprised of the name length in the lower 8 bytes, and the filetype in the high 8 bytes. So in places, we maskname_lenwith0xFFto get the actual length.But once we have masked
name_lenwith0xFF, there is no point in testing whether it is greater thanEXT2_NAME_LEN, which is 255 - or0xFF. So all of these tests are extraneous.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 3/25 fsck: fix -C option parsing
In reply to Eric SandeenThe i++; statement is unreachable; fix same as commit f1c2eaac535bd9172a35ce39b6d8f392321f274d in util-linux
--- misc/fsck.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
misc/fsck.c +1 -1
@@ -1179,8 +1179,8 @@ static void PRS(int argc, char *argv[])if (progress_fd <0 ) progress_fd =0 ;else { + ++i; goto next_arg; - i++; } }break ;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 03/25] fsck: fix -C option parsing
Quoted 4 lines · Eric Sandeen
The i++; statement is unreachable; fix same as commit
f1c2eaac535bd9172a35ce39b6d8f392321f274din util-linuxSigned-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 4/25 mke2fs: remove impossible tests for null usage_types
In reply to Eric Sandeenparse_fs_type explicitly sets usage_types if it is null, so there is no need to test for null later.
--- misc/mke2fs.c | 7 ++----- 1 files changed, 2 insertions(+), 5 deletions(-)
misc/mke2fs.c +2 -5
@@ -1029,15 +1029,12 @@ static char **parse_fs_type(const char *fs_type,if (!usage_types) usage_types = size_type; - parse_str =malloc (usage_types ?strlen (usage_types)+1 :1 ); + parse_str =malloc (strlen (usage_types)+1 );if (!parse_str) {free (list.list);return 0 ; } -if (usage_types) -strcpy (parse_str, usage_types); -else - *parse_str ='\0' ; +strcpy (parse_str, usage_types);if (ext_type)push_string (&list, ext_type);
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 04/25] mke2fs: remove impossible tests for null usage_types
Quoted 4 lines · Eric Sandeen
parse_fs_typeexplicitly setsusage_typesif it is null, so there is no need to test for null later.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 5/25 libext2: move buf variable completely under ifdef
In reply to Eric SandeenIf !WORDS_BIGENDIAN, it is pointless to test whether buf is NULL, because it is initialized to NULL and never changed. This makes Coverity complain, so we can just move all handling of "buf" under the #ifdef.
--- lib/ext2fs/ext_attr.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
lib/ext2fs/ext_attr.c +4 -3
@@ -82,9 +82,9 @@ errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf) {errcode_t retval;char *write_buf; +#ifdef WORDS_BIGENDIAN char *buf =NULL ; -#ifdef WORDS_BIGENDIAN retval =ext2fs_get_mem (fs->blocksize, &buf);if (retval)return retval;
@@ -94,8 +94,9 @@ errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf) write_buf = (char *) inbuf;#endif retval =io_channel_write_blk64 (fs->io, block,1 , write_buf); -if (buf) -ext2fs_free_mem (&buf); +#ifdef WORDS_BIGENDIAN +ext2fs_free_mem (&buf); +#endif if (!retval)ext2fs_mark_changed (fs);return retval;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 05/25] libext2: move buf variable completely under ifdef
Quoted 6 lines · Eric Sandeen
If !WORDS_BIGENDIAN, it is pointless to test whether buf is NULL, because it is initialized to NULL and never changed. This makes Coverity complain, so we can just move all handling of "buf" under the #ifdef.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 6/25 libext2fs: Potential null ptr deref in undo_err_handler_init
In reply to Eric SandeenIn the !undo_io_backing_manager case, undo_err_handler_init will be passed a null data->real, which will be dereferenced.
--- lib/ext2fs/undo_io.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
lib/ext2fs/undo_io.c +2 -1
@@ -400,7 +400,8 @@ static errcode_t undo_open(const char *name, int flags, io_channel *channel)* setup err handler for read so that we know * when the backing manager fails do short read */ -undo_err_handler_init (data->real); +if (data->real) +undo_err_handler_init (data->real); *channel = io;return 0 ;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 06/25] libext2fs: Potential null ptr deref in undo_err_handler_init
Quoted 4 lines · Eric Sandeen
In the !undo_io_backing_manager case,
undo_err_handler_initwill be passed a nulldata->real, which will be dereferenced.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 7/25 e2fsck: handle null fs in print_pathname()
In reply to Eric Sandeentesting fs for NULL in expand_percent_expression():
e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
implies that fs could be NULL, but it's passed to print_pathname() which defererences it without further testing.
So make this safe by returning "???" for a nul fs.
--- e2fsck/message.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
e2fsck/message.c +3 -2
@@ -203,8 +203,9 @@ static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)return ; } - retval =ext2fs_get_pathname (fs, dir, ino, &path); -if (retval) +if (fs) + retval =ext2fs_get_pathname (fs, dir, ino, &path); +if (!fs || retval)fputs ("???" , stdout);else {safe_print (path,-1 );
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 07/25] e2fsck: handle null fs in print_pathname()
Quoted 10 lines · Eric Sandeen
testing fs for NULL in
expand_percent_expression():e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;implies that fs could be NULL, but it's passed to
print_pathname()which defererences it without further testing.So make this safe by returning "???" for a nul fs.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 8/25 e2fsprogs: annotate intentional fallthroughs in case statements
In reply to Eric SandeenUsing the /* fallthrough */ comment lets Coverity (and humans) know that we really do want to fall through in these case statements.
--- lib/e2p/feature.c | 1 + lib/e2p/mntopts.c | 1 + lib/e2p/parse_num.c | 3 +++ lib/ext2fs/dirhash.c | 3 +++ lib/ext2fs/extent.c | 2 ++ 5 files changed, 10 insertions(+), 0 deletions(-)
lib/e2p/feature.c +1 -0
@@ -307,6 +307,7 @@ int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,case '-' :case '^' : neg++; +/* fallthrough */ case '+' : cp++;break ;
lib/e2p/mntopts.c +1 -0
@@ -122,6 +122,7 @@ int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok)case '-' :case '^' : neg++; +/* fallthrough */ case '+' : cp++;break ;
lib/e2p/parse_num.c +3 -0
@@ -26,10 +26,13 @@ unsigned long long parse_num_blocks2(const char *arg, int log_block_size)switch (*p) {/* Using fall-through logic */ case 'T' :case 't' : num <<=10 ; +/* fallthrough */ case 'G' :case 'g' : num <<=10 ; +/* fallthrough */ case 'M' :case 'm' : num <<=10 ; +/* fallthrough */ case 'K' :case 'k' : num >>= log_block_size;break ;
lib/ext2fs/dirhash.c +3 -0
@@ -217,11 +217,13 @@ errcode_t ext2fs_dirhash(int version, const char *name, int len,switch (version) {case EXT2_HASH_LEGACY_UNSIGNED : unsigned_flag++; +/* fallthrough */ case EXT2_HASH_LEGACY : hash =dx_hack_hash (name, len, unsigned_flag);break ;case EXT2_HASH_HALF_MD4_UNSIGNED : unsigned_flag++; +/* fallthrough */ case EXT2_HASH_HALF_MD4 : p = name;while (len >0 ) {
@@ -235,6 +237,7 @@ errcode_t ext2fs_dirhash(int version, const char *name, int len,break ;case EXT2_HASH_TEA_UNSIGNED : unsigned_flag++; +/* fallthrough */ case EXT2_HASH_TEA : p = name;while (len >0 ) {
lib/ext2fs/extent.c +2 -0
@@ -373,9 +373,11 @@ retry:case EXT2_EXTENT_ROOT : handle->level =0 ; path = handle->path + handle->level; +/* fallthrough */ case EXT2_EXTENT_FIRST_SIB : path->left = path->entries; path->curr =0 ; +/* fallthrough */ case EXT2_EXTENT_NEXT_SIB :if (path->left <=0 )return EXT2_ET_EXTENT_NO_NEXT ;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 08/25] e2fsprogs: annotate intentional fallthroughs in case statements
Quoted 4 lines · Eric Sandeen
Using the /* fallthrough */ comment lets Coverity (and humans) know that we really do want to fall through in these case statements.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 9/25 uuidd: Add missing break to option case statement
In reply to Eric SandeenSpecifying the "-n" option to uuidd would incorrectly fall through to the "-p" case, and assign that number to the pidfile_path.
--- misc/uuidd.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
misc/uuidd.c +1 -0
@@ -472,6 +472,7 @@ int main(int argc, char **argv)fprintf (stderr,_ ("Bad number: %s\n" ), optarg);exit (1 ); } +break ;case 'p' : pidfile_path = optarg; drop_privs =1 ;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 09/25] uuidd: Add missing break to option case statement
Quoted 5 lines · Eric Sandeen
Specifying the "-n" option to uuidd would incorrectly fall through to the "-p" case, and assign that number to the
pidfile_path.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
Can you do me a favor and make sure this gets fixed in util-linux-ng as well?
- Ted
Re: [PATCH 09/25] uuidd: Add missing break to option case statement
Quoted 11 lines · Ted Ts'o
Quoted 5 lines · Eric Sandeen
Specifying the "-n" option to uuidd would incorrectly fall through to the "-p" case, and assign that number to the
pidfile_path.Signed-off-by Eric Sandeen <[email protected]>Applied, thanks.
Can you do me a favor and make sure this gets fixed in util-linux-ng as well?
sure thing
-eric
Quoted 5 lines
- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 10/25 freefrag: fix up getopt case statement
In reply to Eric SandeenThere is no need to print out a "bad option" message; getopt does that for us, and in fact will change "c" to "?" so it's not even useful.
--- misc/e2freefrag.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)
misc/e2freefrag.c +1 -3
@@ -282,10 +282,8 @@ int main(int argc, char *argv[]) } chunk_info.chunkbytes *=1024 ;break ; -default : -fprintf (stderr,"%s: bad option '%c'\n" , - progname, c);case 'h' : +default :usage (progname);break ; }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 10/25] freefrag: fix up getopt case statement
Quoted 5 lines · Eric Sandeen
There is no need to print out a "bad option" message; getopt does that for us, and in fact will change "c" to "?" so it's not even useful.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 11/25 libe2p: reach unreachable code
In reply to Eric SandeenThe EOPNOTSUPP case is unreachable, being outside a set of:
#if ... return; #else ... return;
#endif
Fix this up so that if neither HAVE_CHFLAGS norHAVE_EXT2_IOCTLS applies, we set EOPNOTSUPP.
--- lib/e2p/setflags.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
lib/e2p/setflags.c +4 -3
@@ -55,7 +55,7 @@ int setflags (int fd, unsigned long flags)#endif return fchflags (fd, bsd_flags); -#else +#else /* ! HAVE_CHFLAGS */ #if HAVE_EXT2_IOCTLS struct stat buf;int f;
@@ -68,8 +68,9 @@ int setflags (int fd, unsigned long flags) f = (int ) flags;return ioctl (fd,EXT2_IOC_SETFLAGS , &f); -#endif /* HAVE_EXT2_IOCTLS */ -#endif +#else errno =EOPNOTSUPP ;return -1 ; +#endif /* HAVE_EXT2_IOCTLS */ +#endif /* HAVE_CHFLAGS */ }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 11/25] libe2p: reach unreachable code
Quoted 7 lines · Eric Sandeen
The EOPNOTSUPP case is unreachable, being outside a set of:
#if ... return; #else ... return;
Quoted 6 lines
#endif
Fix this up so that if neither
HAVE_CHFLAGSnorHAVE_EXT2_IOCTLSapplies, we set EOPNOTSUPP.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 12/25 e2fsck: Don't store old_op from ehandler_operation if we don't restore it.
In reply to Eric Sandeenold_op is set but never used, because we restore "0" not old_op. So don't bother with it.
--- e2fsck/pass1.c | 3 +-- e2fsck/pass2.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
e2fsck/pass1.c +1 -2
@@ -402,7 +402,6 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx, {struct ext2_inode *inode = pctx->inode;struct ext2_dir_entry *dirent; -const char *old_op;errcode_t retval;blk64_t blk;unsigned int i, rec_len, not_device =0 ;
@@ -472,7 +471,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,return ;/* read the first block */ - old_op =ehandler_operation (_ ("reading directory block" )); +ehandler_operation (_ ("reading directory block" )); retval =ext2fs_read_dir_block3 (ctx->fs, blk, buf,0 );ehandler_operation (0 );if (retval)
e2fsck/pass2.c +1 -2
@@ -711,7 +711,6 @@ static int check_dir_block(ext2_filsys fs,struct ext2_dir_entry *dirent, *prev;ext2_dirhash_t hash;unsigned int offset =0 ; -const char * old_op;int dir_modified =0 ;int dot_state;unsigned int rec_len;
@@ -774,7 +773,7 @@ static int check_dir_block(ext2_filsys fs, db->blockcnt, ino);#endif - old_op =ehandler_operation (_ ("reading directory block" )); +ehandler_operation (_ ("reading directory block" )); cd->pctx.errcode =ext2fs_read_dir_block3 (fs, block_nr, buf,0 );ehandler_operation (0 );if (cd->pctx.errcode ==EXT2_ET_DIR_CORRUPTED )
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 12/25] e2fsck: Don't store old_op from ehandler_operation if we don't restore it.
Quoted 4 lines · Eric Sandeen
old_opis set but never used, because we restore "0" notold_op. So don't bother with it.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 13/25 e2fsprogs: Fix some error cleanup path bugs
In reply to Eric SandeenIn inode_open(), if the allocation of &io fails, we go to cleanup and dereference io to test io->name, which is a bug.
Similarly in undo_open() if allocation of &data fails, we go to cleanup and dereference data to test data->real.
In the test_open() case we explicitly set retval to the only possible error return from ext2fs_get_mem(), so remove that for tidiness.
The other changes just make make earlier returns go through the error goto for consistency.
In many cases we returned directly from the first error, but "goto cleanup" etc for every subsequent error. In some cases this leads to "impossible" tests such as:
if (ptr) ext2fs_free_mem(&ptr)
on paths where ptr cannot be null because we would have returned directly earlier, and Coverity flags this.
This isn't really indicative of an error in most cases, but I think it can be clearer to always exit through the error goto if it's used later in the function.
--- lib/ext2fs/dblist.c | 2 +- lib/ext2fs/inode.c | 6 ++++-- lib/ext2fs/inode_io.c | 2 +- lib/ext2fs/test_io.c | 6 ++---- lib/ext2fs/undo_io.c | 4 ++-- lib/ext2fs/unix_io.c | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-)
lib/ext2fs/dblist.c +1 -1
@@ -73,7 +73,7 @@ static errcode_t make_dblist(ext2_filsys fs, ext2_ino_t size, retval =ext2fs_get_mem (sizeof (struct ext2_struct_dblist ), &dblist);if (retval) -return retval; + goto cleanup;memset (dblist,0 ,sizeof (struct ext2_struct_dblist )); dblist->magic =EXT2_ET_MAGIC_DBLIST ;
lib/ext2fs/inode.c +4 -2
@@ -671,8 +671,10 @@ errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,if (length > (int )sizeof (struct ext2_inode_large )) { w_inode =malloc (length); -if (!w_inode) -return ENOMEM ; +if (!w_inode) { + retval =ENOMEM ; + goto errout; + } }else w_inode = &temp_inode;memset (w_inode,0 , length);
lib/ext2fs/inode_io.c +1 -1
@@ -163,7 +163,7 @@ static errcode_t inode_open(const char *name, int flags, io_channel *channel)return 0 ; cleanup: -if (io->name) +if (io && io->name)ext2fs_free_mem (&io->name);if (data)ext2fs_free_mem (&data);
lib/ext2fs/test_io.c +2 -4
@@ -190,14 +190,12 @@ static errcode_t test_open(const char *name, int flags, io_channel *channel)return EXT2_ET_BAD_DEVICE_NAME ; retval =ext2fs_get_mem (sizeof (struct struct_io_channel ), &io);if (retval) -return retval; + goto cleanup;memset (io,0 ,sizeof (struct struct_io_channel )); io->magic =EXT2_ET_MAGIC_IO_CHANNEL ; retval =ext2fs_get_mem (sizeof (struct test_private_data ), &data); -if (retval) { - retval =EXT2_ET_NO_MEMORY ; +if (retval) goto cleanup; - } io->manager = test_io_manager; retval =ext2fs_get_mem (strlen (name)+1 , &io->name);if (retval)
lib/ext2fs/undo_io.c +2 -2
@@ -357,7 +357,7 @@ static errcode_t undo_open(const char *name, int flags, io_channel *channel)return EXT2_ET_BAD_DEVICE_NAME ; retval =ext2fs_get_mem (sizeof (struct struct_io_channel ), &io);if (retval) -return retval; + goto cleanup;memset (io,0 ,sizeof (struct struct_io_channel )); io->magic =EXT2_ET_MAGIC_IO_CHANNEL ; retval =ext2fs_get_mem (sizeof (struct undo_private_data ), &data);
@@ -407,7 +407,7 @@ static errcode_t undo_open(const char *name, int flags, io_channel *channel)return 0 ; cleanup: -if (data->real) +if (data && data->real)io_channel_close (data->real);if (data)ext2fs_free_mem (&data);
lib/ext2fs/unix_io.c +1 -1
@@ -453,7 +453,7 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)return EXT2_ET_BAD_DEVICE_NAME ; retval =ext2fs_get_mem (sizeof (struct struct_io_channel ), &io);if (retval) -return retval; + goto cleanup;memset (io,0 ,sizeof (struct struct_io_channel )); io->magic =EXT2_ET_MAGIC_IO_CHANNEL ; retval =ext2fs_get_mem (sizeof (struct unix_private_data ), &data);
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 13/25] e2fsprogs: Fix some error cleanup path bugs
Quoted 20 lines · Eric Sandeen
In
inode_open(), if the allocation of &io fails, we go to cleanup and dereference io to testio->name, which is a bug.Similarly in
undo_open()if allocation of &data fails, we go to cleanup and dereference data to testdata->real.In the
test_open()case we explicitly set retval to the only possible error return fromext2fs_get_mem(), so remove that for tidiness.The other changes just make make earlier returns go through the error goto for consistency.
In many cases we returned directly from the first error, but "goto cleanup" etc for every subsequent error. In some cases this leads to "impossible" tests such as:
if (ptr) ext2fs_free_mem(&ptr)
Quoted 8 lines
on paths where ptr cannot be null because we would have returned directly earlier, and Coverity flags this.
This isn't really indicative of an error in most cases, but I think it can be clearer to always exit through the error goto if it's used later in the function.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 14/25 subst: Fix free of uninit pointers
In reply to Eric Sandeenin add_subst(), if the malloc of ent->name fails, we goto fail; which will free ent->name (which is null, so OK) but also free ent->value (which is uninitialized). There is no case where we must free ent->value on an error (it is allocated last, and if it fails it of course doesn't need to be freed) so just remove it.
Also "retval" is only assigned once to the constant ENOMEM, so we can just return that explicitly in the failure case.
--- util/subst.c | 5 +---- 1 files changed, 1 insertions(+), 4 deletions(-)
util/subst.c +1 -4
@@ -35,9 +35,7 @@ struct subst_entry *subst_table = 0;static int add_subst (char *name,char *value) {struct subst_entry *ent =0 ; -int retval; - retval =ENOMEM ; ent = (struct subst_entry *)malloc (sizeof (struct subst_entry ));if (!ent) goto fail;
@@ -55,10 +53,9 @@ static int add_subst(char *name, char *value) fail:if (ent) {free (ent->name); -free (ent->value);free (ent); } -return retval; +return ENOMEM ; }static struct subst_entry *fetch_subst_entry (char *name)
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 14/25] subst: Fix free of uninit pointers
Quoted 7 lines · Eric Sandeen
in
add_subst(), if the malloc ofent->namefails, we goto fail; which will freeent->name(which is null, so OK) but also freeent->value(which is uninitialized). There is no case where we must freeent->valueon an error (it is allocated last, and if it fails it of course doesn't need to be freed) so just remove it.
Quoted 5 lines
Also "retval" is only assigned once to the constant ENOMEM, so we can just return that explicitly in the failure case.
Signed-off-by Eric Saneeen <[email protected]>Signed-off-by Eric Sandeen <[email protected]>
Applied, although I removed the double sign off. :-)
- Ted
PATCH 15/25 filefrag: Fix uninitialized "expected" value
In reply to Eric SandeenThe "count" variable is only ever set if FIBMAP is used, due to the -B switch, or a fiemap failure. However, we use it unconditionally to calculate "expected" for extN files, so we can end up printing garbage.
Initialize count to 0, and unless we go through the FIBMAP path, expected will be 0 as well, and in that case do not print the message.
--- misc/filefrag.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
misc/filefrag.c +3 -2
@@ -272,7 +272,7 @@ static void frag_report(const char *filename)#endif int bs;long fd; -unsigned long block, last_block =0 , numblocks, i, count; +unsigned long block, last_block =0 , numblocks, i, count =0 ;long bpib;/* Blocks per indirect block */ long cylgroups;int num_extents =0 , expected;
@@ -373,8 +373,9 @@ static void frag_report(const char *filename)printf ("%s: 1 extent found" , filename); else printf("%s: %d extents found", filename, num_extents); +/* count, and thus expected, only set for indirect FIBMAP'd files */ expected = (count/((bs*8 )-(fsinfo.f_files/8 /cylgroups)-3 ))+1 ; -if (is_ext2 && expected < num_extents) +if (is_ext2 && expected && expected < num_extents)printf (", perfection would be %d extent%s\n" , expected, (expected>1 ) ?"s" :"" );else
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 15/25] filefrag: Fix uninitialized "expected" value
Quoted 10 lines · Eric Sandeen
The "count" variable is only ever set if FIBMAP is used, due to the -B switch, or a fiemap failure. However, we use it unconditionally to calculate "expected" for extN files, so we can end up printing garbage.
Initialize count to 0, and unless we go through the FIBMAP path, expected will be 0 as well, and in that case do not print the message.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 16/25 e2fsck: remove extraneous memset
In reply to Eric Sandeene2fsck_allocate_memory() already sets allocated memory to 0, so remove the explicit memset.
Especially since it was setting the wrong size (iter not *iter)
--- e2fsck/dirinfo.c | 1 - 1 files changed, 0 insertions(+), 1 deletions(-)
e2fsck/dirinfo.c +0 -1
@@ -318,7 +318,6 @@ extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx) iter =e2fsck_allocate_memory (ctx,sizeof (struct dir_info_iter ),"dir_info iterator" ); -memset (iter,0 ,sizeof (iter));if (db->tdb) iter->tdb_iter =tdb_firstkey (db->tdb);
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 16/25] e2fsck: remove extraneous memset
Quoted 6 lines · Eric Sandeen
e2fsck_allocate_memory()already sets allocated memory to 0, so remove the explicit memset.Especially since it was setting the wrong size (iter not *iter)
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 17/25 mke2fs: Do not let -t or -T be specified more than once
In reply to Eric SandeenIn addition to not making since, it causes a memory leak when fs_type gets overwritten.
--- misc/mke2fs.c | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-)
misc/mke2fs.c +10 -0
@@ -1445,9 +1445,19 @@ profile_error: super_only =1 ;break ;case 't' : +if (fs_type) { +com_err (program_name,0 , +_ ("The -t option may only be used once" )); +exit (1 ); + } fs_type =strdup (optarg);break ;case 'T' : +if (usage_types) { +com_err (program_name,0 , +_ ("The -T option may only be used once" )); +exit (1 ); + } usage_types =strdup (optarg);break ;case 'U' :
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 17/25] mke2fs: Do not let -t or -T be specified more than once
Quoted 4 lines · Eric Sandeen
In addition to not making since, it causes a memory leak when
fs_typegets overwritten.Signed-off-by Eric Sandeen <[email protected]>
Applied, with whitespace cleanups.
- Ted
Re: [PATCH 17/25] mke2fs: Do not let -t or -T be specified more than once
Quoted 7 lines · Ted Ts'o
Quoted 4 lines · Eric Sandeen
In addition to not making since, it causes a memory leak when
fs_typegets overwritten.Signed-off-by Eric Sandeen <[email protected]>Applied, with whitespace cleanups.
Oh, and s/since/sense/
- Ted
Re: [PATCH 17/25] mke2fs: Do not let -t or -T be specified more than once
Quoted 5 lines · Ted Ts'o
Quoted 4 lines · Ted Ts'o
Quoted 3 lines · Eric Sandeen
In addition to not making since, it causes a memory leak when
fs_typegets overwritten.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Eric Sandeen <[email protected]>Applied, with whitespace cleanups.
Oh, and s/since/sense/
- Ted
whoops thanks.
Usually my English is ok, sorry! ;)
PATCH 18/25 e2initrd_helper: Fix memory leak on error
In reply to Eric SandeenSome error paths did not properly free "buf"
And the normal exit seemed to close e2_file twice (?)
--- misc/e2initrd_helper.c | 11 +++++++---- 1 files changed, 7 insertions(+), 4 deletions(-)
misc/e2initrd_helper.c +7 -4
@@ -73,7 +73,7 @@ static errcode_t get_file(ext2_filsys fs, const char * filename, {errcode_t retval;char *buf; -ext2_file_t e2_file; +ext2_file_t e2_file =NULL ;unsigned int got;struct ext2_inode inode;ext2_ino_t ino;
@@ -101,7 +101,7 @@ static errcode_t get_file(ext2_filsys fs, const char * filename, retval =ext2fs_file_open (fs, ino,0 , &e2_file);if (retval) -return retval; + goto errout; retval =ext2fs_file_read (e2_file, buf, inode.i_size, &got);if (retval)
@@ -109,13 +109,16 @@ static errcode_t get_file(ext2_filsys fs, const char * filename, retval =ext2fs_file_close (e2_file);if (retval) -return retval; + goto errout; ret_file->buf = buf; ret_file->size = (int ) got; +return 0 ; errout: -ext2fs_file_close (e2_file); +free (buf); +if (e2_file) +ext2fs_file_close (e2_file);return retval; }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 18/25] e2initrd_helper: Fix memory leak on error
Quoted 5 lines · Eric Sandeen
Some error paths did not properly free "buf"
And the normal exit seemed to close
e2_filetwice (?)Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 19/25 libext2: Fix leaks in write_bitmaps on error returns
In reply to Eric Sandeenblock_buf and/or inode_buf may not be properly freed on an error return.
Create a new errout: target to free them as needed in error conditions.
--- lib/ext2fs/rw_bitmaps.c | 28 +++++++++++++++++++--------- 1 files changed, 19 insertions(+), 9 deletions(-)
lib/ext2fs/rw_bitmaps.c +19 -9
@@ -34,7 +34,7 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)int block_nbytes, inode_nbytes;unsigned int nbits;errcode_t retval; -char *block_buf, *inode_buf; +char *block_buf =NULL , *inode_buf =NULL ;int csum_flag =0 ;blk64_t blk;blk64_t blk_itr =EXT2FS_B2C (fs, fs->super->s_first_data_block);
@@ -55,7 +55,7 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval =ext2fs_get_memalign (fs->blocksize, fs->blocksize, &block_buf);if (retval) -return retval; + goto errout;memset (block_buf,0xff , fs->blocksize); }if (do_inode) {
@@ -64,7 +64,7 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval =ext2fs_get_memalign (fs->blocksize, fs->blocksize, &inode_buf);if (retval) -return retval; + goto errout;memset (inode_buf,0xff , fs->blocksize); }
@@ -79,7 +79,7 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval =ext2fs_get_block_bitmap_range2 (fs->block_map, blk_itr, block_nbytes <<3 , block_buf);if (retval) -return retval; + goto errout;if (i == fs->group_desc_count -1 ) {/* Force bitmap padding for the last group */
@@ -95,8 +95,10 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)if (blk) { retval =io_channel_write_blk64 (fs->io, blk,1 , block_buf); -if (retval) -return EXT2_ET_BLOCK_BITMAP_WRITE ; +if (retval) { + retval =EXT2_ET_BLOCK_BITMAP_WRITE ; + goto errout; + } } skip_this_block_bitmap: blk_itr += block_nbytes <<3 ;
@@ -112,14 +114,16 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block) retval =ext2fs_get_inode_bitmap_range2 (fs->inode_map, ino_itr, inode_nbytes <<3 , inode_buf);if (retval) -return retval; + goto errout; blk =ext2fs_inode_bitmap_loc (fs, i);if (blk) { retval =io_channel_write_blk64 (fs->io, blk,1 , inode_buf); -if (retval) -return EXT2_ET_INODE_BITMAP_WRITE ; +if (retval) { + retval =EXT2_ET_INODE_BITMAP_WRITE ; + goto errout; + } } skip_this_inode_bitmap: ino_itr += inode_nbytes <<3 ;
@@ -134,6 +138,12 @@ static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)ext2fs_free_mem (&inode_buf); }return 0 ; +errout: +if (inode_buf) +ext2fs_free_mem (&inode_buf); +if (block_buf) +ext2fs_free_mem (&block_buf); +return retval; }static errcode_t read_bitmaps (ext2_filsys fs,int do_inode,int do_block)
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 19/25] libext2: Fix leaks in write_bitmaps on error returns
Quoted 6 lines · Eric Sandeen
block_bufand/orinode_bufmay not be properly freed on an error return.Create a new errout: target to free them as needed in error conditions.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 20/25 test_icount: fclose() before exit
In reply to Eric SandeenJust to be tidy.
--- tests/progs/test_icount.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
tests/progs/test_icount.c +2 -0
@@ -289,6 +289,8 @@ static int source_file(const char *cmd_file, int sci_idx) exit_status++; } } +if (f != stdin) +fclose (f);return exit_status; }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 20/25] test_icount: fclose() before exit
Quoted 3 lines · Eric Sandeen
Just to be tidy.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 21/25 e2fsck: Fix leaks in error paths
In reply to Eric Sandeenfn and/or array was not freed in some error paths.
--- e2fsck/profile.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
e2fsck/profile.c +2 -0
@@ -276,6 +276,7 @@ static errcode_t get_dirlist(const char *dirname, char***ret_array) new_array =realloc (array,sizeof (char *) * (max+1 ));if (!new_array) { retval =ENOMEM ; +free (fn); goto errout; } array = new_array;
@@ -345,6 +346,7 @@ profile_init(const char **files, profile_t *ret_profile)* If all the files were not found, return the appropriate error. */ if (!profile->first_file) { +free_list (array);profile_release (profile);return ENOENT ; }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 21/25] e2fsck: Fix leaks in error paths
Quoted 3 lines · Eric Sandeen
fn and/or array was not freed in some error paths.
Signed-off-by Eric Sandeen <[email protected]>
Applied, but I did make a two changes.
Review profile_init(const char **files, profile_t *ret_profile) · line 352
Quoted 8 lines
(quoted diff) +1
-0@@ -345,6 +346,7 @@ profile_init(const char **files, profile_t *ret_profile) * If all the files were not found, return the appropriate error. */ if (!profile->first_file) { + free_list(array); profile_release(profile); return ENOENT; }
I changed this to be:
if (!profile->first_file) {
retval = ENOENT; goto errout;
}
Which allows us to unify the error cleanup path and avoid duplicating code.
Also, I noticed another bug while I was validating your patch. If the realloc() in get_dirlist() fails due to a ENOMEM, and we jump to errout, the array hasn't been null terminated yet. This could lead to lead a kernel oops or worse when free_list(array) tries to free the array.
So I added:
errout:
+ if (array)
+ array[num] = 0;
to take care of this problem.
- Ted
commit 04bfa75f42a5adb3510551f4d153526d94be37fb
Author: Eric Sandeen <[email protected]> Date: Fri Sep 16 15:49:36 2011 -0500
e2fsck: Fix leaks in error paths
fn and/or array was not freed in some error paths.
[ Also make sure the array is NULL terminated before we free it in
get_dirlist(). --tytso]
e2fsck/profile.c +5 -2
@@ -276,6 +276,7 @@ static errcode_t get_dirlist(const char *dirname, char***ret_array) new_array =realloc (array,sizeof (char *) * (max+1 ));if (!new_array) { retval =ENOMEM ; +free (fn); goto errout; } array = new_array;
@@ -290,6 +291,8 @@ static errcode_t get_dirlist(const char *dirname, char***ret_array)closedir (dir);return 0 ; errout: +if (array) + array[num] =0 ;closedir (dir);free_list (array);return retval;
@@ -345,8 +348,8 @@ profile_init(const char **files, profile_t *ret_profile)* If all the files were not found, return the appropriate error. */ if (!profile->first_file) { -profile_release (profile); -return ENOENT ; + retval =ENOENT ; + goto errout; } }
Re: [PATCH 21/25] e2fsck: Fix leaks in error paths
Quoted 21 lines · Ted Ts'o
Quoted 3 lines · Eric Sandeen
fn and/or array was not freed in some error paths.
Signed-off-by Eric Sandeen <[email protected]>Applied, but I did make a two changes.
e2fsck/profile.c profile_init(const char **files, profile_t *ret_profile) · line 352
Quoted 8 lines
e2fsck/profile.c +1
-0@@ -345,6 +346,7 @@ profile_init(const char **files, profile_t *ret_profile)* If all the files were not found, return the appropriate error. */ if (!profile->first_file) { +free_list (array);profile_release (profile);return ENOENT ; }I changed this to be:
(quoted diff) +0
-0if (!profile->first_file) { retval = ENOENT;goto errout;
Quoted 4 lines
}
Which allows us to unify the error cleanup path and avoid duplicating code.
Makes sense, now why didn't I do that ... was thinking there was some issue but sure looks right now!
Quoted 62 lines
Also, I noticed another bug while I was validating your patch. If the
realloc()inget_dirlist()fails due to a ENOMEM, and we jump to errout, the array hasn't been null terminated yet. This could lead to lead a kernel oops or worse whenfree_list(array)tries to free the array.So I added:
errout:
e2fsck/profile.c +2
-0+if (array) + array[num] =0 ;to take care of this problem.
- Ted
commit
04bfa75f42a5adb3510551f4d153526d94be37fbAuthor: Eric Sandeen <[email protected]> Date: Fri Sep 16 15:49:36 2011 -0500e2fsck: Fix leaks in error paths
fn and/or array was not freed in some error paths.
[ Also make sure the array is NULL terminated before we free it in
get_dirlist(). --tytso]Signed-off-by Eric Sandeen <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>e2fsck/profile.c +5
-2@@ -276,6 +276,7 @@ static errcode_t get_dirlist(const char *dirname, char***ret_array) new_array =realloc (array,sizeof (char *) * (max+1 ));if (!new_array) { retval =ENOMEM ; +free (fn); goto errout; } array = new_array;@@ -290,6 +291,8 @@ static errcode_t get_dirlist(const char *dirname, char***ret_array)closedir (dir);return 0 ; errout: +if (array) + array[num] =0 ;closedir (dir);free_list (array);return retval;@@ -345,8 +348,8 @@ profile_init(const char **files, profile_t *ret_profile)* If all the files were not found, return the appropriate error. */ if (!profile->first_file) { -profile_release (profile); -return ENOENT ; + retval =ENOENT ; + goto errout; } }
PATCH 22/25 tune2fs: handle inode and/or block bitmap read failures in resize_inode()
In reply to Eric SandeenHandle these failures in resize_inode, and handle the propagated error in the caller.
--- misc/tune2fs.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-)
misc/tune2fs.c +13 -2
@@ -1589,8 +1589,16 @@ static int resize_inode(ext2_filsys fs, unsigned long new_size)int new_ino_blks_per_grp;ext2fs_block_bitmap bmap; -ext2fs_read_inode_bitmap (fs); -ext2fs_read_block_bitmap (fs); + retval =ext2fs_read_inode_bitmap (fs); +if (retval) { +fputs (_ ("Failed to read inode bitmap\n" ), stderr); +return retval; + } + retval =ext2fs_read_block_bitmap (fs); +if (retval) { +fputs (_ ("Failed to read blockbitmap\n" ), stderr); +return retval; + }INIT_LIST_HEAD (&blk_move_list);
@@ -1989,6 +1997,9 @@ retry_open:if (resize_inode (fs, new_inode_size) ==0 ) {printf (_ ("Setting inode size %lu\n" ), new_inode_size); + }else { +printf (_ ("Failed to change inode size\n" )); +exit (1 ); } }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 22/25] tune2fs: handle inode and/or block bitmap read failures in resize_inode()
Quoted 4 lines · Eric Sandeen
Handle these failures in
resize_inode, and handle the propagated error in the caller.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 23/25 e2fsprogs: Don't try to close an fd which is negative
In reply to Eric SandeenThese reflect either file descriptors which aren't tested for failure, or closures of fd's which may have failed.
In setup_tdb(), test for failure of mkstemp and return without trying to open the file (again).
In reserve_stdio_fds, rather than closing the "extra" fd == 3 due to the way the loop is written, just don't go that far by using while (fd <= 2).
In logsave, it forks and retries forever if open fails, but at least make coverity happy by explicitly not trying to close a negative file descriptor.
--- e2fsck/dirinfo.c | 4 ++++ e2fsck/unix.c | 11 ++++++----- misc/logsave.c | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-)
e2fsck/dirinfo.c +4 -0
@@ -62,6 +62,10 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)uuid_unparse (ctx->fs->super->s_uuid, uuid);sprintf (db->tdb_fn,"%s/%s-dirinfo-XXXXXX" , tdb_dir, uuid); fd =mkstemp (db->tdb_fn); +if (fd <0 ) { + db->tdb =NULL ; +return ; + } db->tdb =tdb_open (db->tdb_fn,0 ,TDB_CLEAR_IF_FIRST ,O_RDWR |O_CREAT |O_TRUNC ,0600 );close (fd);
e2fsck/unix.c +6 -5
@@ -542,14 +542,16 @@ static int e2fsck_update_progress(e2fsck_t ctx, int pass,#define PATH_SET "PATH=/sbin" +/* +* Make sure 0,1,2 file descriptors are open, so that we don't open +* the filesystem using the same file descriptor as stdout or stderr. +*/ static void reserve_stdio_fds (void ) { -int fd; +int fd =0 ; -while (1 ) { +while (fd <=2 ) { fd =open ("/dev/null" ,O_RDWR ); -if (fd >2 ) -break ;if (fd <0 ) { fprintf(stderr, _("ERROR: Couldn't open " "/dev/null (%s)\n"),
@@ -557,7 +559,6 @@ static void reserve_stdio_fds(void)break ; } } -close (fd); }#ifdef HAVE_SIGNAL_H
misc/logsave.c +2 -1
@@ -325,7 +325,8 @@ int main(int argc, char **argv)write_all (outfd, outbuf, outbufsize);free (outbuf); } -close (outfd); +if (outfd >=0 ) +close (outfd);exit (rc); }
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 23/25] e2fsprogs: Don't try to close an fd which is negative
Quoted 10 lines · Eric Sandeen
These reflect either file descriptors which aren't tested for failure, or closures of fd's which may have failed.
In
setup_tdb(), test for failure of mkstemp and return without trying to open the file (again).In
reserve_stdio_fds, rather than closing the "extra" fd == 3 due to the way the loop is written, just don't go that far by using while (fd <= 2).
Quoted 5 lines
In logsave, it forks and retries forever if open fails, but at least make coverity happy by explicitly not trying to close a negative file descriptor.
Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 24/25 e4defrag: Check error return of sysconf()
In reply to Eric SandeenIn theory sysconf() can fail, so check for an error return.
--- misc/e4defrag.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
misc/e4defrag.c +4 -1
@@ -399,13 +399,16 @@ static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),static int page_in_core (int fd,struct move_extent defrag_data,unsigned char **vec,unsigned int *page_num) { -long pagesize =sysconf (_SC_PAGESIZE); +long pagesize;void *page =NULL ;loff_t offset, end_offset, length;if (vec ==NULL || *vec !=NULL )return -1 ; + pagesize =sysconf (_SC_PAGESIZE); +if (pagesize <0 ) +return -1 ;/* In mmap, offset should be a multiple of the page size */ offset = (loff_t )defrag_data.orig_start * block_size; length = (loff_t )defrag_data.len * block_size;
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 24/25] e4defrag: Check error return of sysconf()
Quoted 3 lines · Eric Sandeen
In theory
sysconf()can fail, so check for an error return.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks.
- Ted
PATCH 25/25 mke2fs: free tdb_dir string if it came from the profile
In reply to Eric Sandeenif tdb_dir points to a string allocated from profile_get_string, it should be freed again before we exit.
--- misc/mke2fs.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-)
misc/mke2fs.c +9 -2
@@ -2024,18 +2024,21 @@ open_err_out:static int mke2fs_setup_tdb (const char *name,io_manager *io_ptr) {errcode_t retval =ENOMEM ; -char *tdb_dir, *tdb_file =NULL ; +char *tdb_dir =NULL , *tdb_file =NULL ;char *device_name, *tmp_name; +int free_tdb_dir =0 ;/* * Configuration via a conf file would be * nice */ tdb_dir =getenv ("E2FSPROGS_UNDO_DIR" ); -if (!tdb_dir) +if (!tdb_dir) {profile_get_string (profile,"defaults" ,"undo_dir" ,0 ,"/var/lib/e2fsprogs" , &tdb_dir); + free_tdb_dir =1 ; + }if (!strcmp (tdb_dir,"none" ) || (tdb_dir[0 ] ==0 ) ||access (tdb_dir,W_OK ))
@@ -2069,10 +2072,14 @@ static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)"using the command:\n" " e2undo %s %s\n\n"), tdb_file, name); +if (free_tdb_dir) +free (tdb_dir);free (tdb_file);return 0 ; errout: +if (free_tdb_dir) +free (tdb_dir);free (tdb_file);com_err (program_name, retval,_ ("while trying to setup undo file\n" ));
Signature · 1 line
1.7.4.1
Ted Ts'o
AppliedRe: [PATCH 25/25] mke2fs: free tdb_dir string if it came from the profile
Quoted 4 lines · Eric Sandeen
if
tdb_dirpoints to a string allocated fromprofile_get_string, it should be freed again before we exit.Signed-off-by Eric Sandeen <[email protected]>
Applied, thanks!
- Ted
Quoted 7 lines · Eric Sandeen
I ran Coverity over e2fsprogs this week, hoping to find any recent-ish flaws, but most of what it came up with seems longstanding. I guess that's a good sign for the recent work, anyway!
Some of these are just tidyups, some are bugfixes. It gets the defect count down to around 25 from 70 to start with, but I'll need to take a deep breath before tackling the rest.
Heh, and I should have pulled from github prior to doing all this. :(
I take back that comment about recent work, at least slightly.
Is the quota library really full of void functions which cannot return errors? My fault for not reviewing while it was on the list, sorry.
-Eric
Quoted 8 lines
Thanks,
-Eric--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 7 lines · Eric Sandeen
Heh, and I should have pulled from github prior to doing all this. :(
I take back that comment about recent work, at least slightly.
Is the quota library really full of void functions which cannot return errors? My fault for not reviewing while it was on the list, sorry.
Yeah, the quota library is going to need a lot of fixups before 1.42 ships. I don't consider the ABI (or the function naming, since it doesn't have any kind of namespace discpline) fixed in stone yet. Perhaps I shouldn't have integrated the patches right away but we'll get it fixed up before 1.42 goes out the door.
- Ted
Quoted 6 lines · Ted Ts'o
Quoted 5 lines · Eric Sandeen
Heh, and I should have pulled from github prior to doing all this. :(
I take back that comment about recent work, at least slightly.
Quoted 8 lines
Quoted 2 lines
Is the quota library really full of void functions which cannot return errors? My fault for not reviewing while it was on the list, sorry.
Yeah, the quota library is going to need a lot of fixups before 1.42 ships. I don't consider the ABI (or the function naming, since it doesn't have any kind of namespace discpline) fixed in stone yet. Perhaps I shouldn't have integrated the patches right away but we'll get it fixed up before 1.42 goes out the door.
sounds like a plan ... though too bad that's One More Thing which must be done prior to a 64-bit-capable release :(
by the way, any plans to put a new -WIP-tagged tarball anywhere?
Thanks,
-Eric
Quoted 5 lines
- Ted
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Eric Sandeen
sounds like a plan ... though too bad that's One More Thing which must be done prior to a 64-bit-capable release :(
by the way, any plans to put a new -WIP-tagged tarball anywhere?
Sorry, I hadn't gotten around to sending out an announcement yet. Since kernel.org is down, I've gone back to using the old sourceforge distribution method:
http://sourceforge.net/projects/e2fsprogs/files/e2fsprogs-TEST/1.42-WIP-0916/
As always, the tarball is GPG-signed, and the git tag is als GPG-signed, from these two sites:
git://github.com/tytso/e2fsprogs.git
git://e2fsprogs.git.sourceforge.net/gitroot/e2fsprogs/e2fsprogs
BTW, I already know that the 0916 release is broken on big-endian machines. A Debian developer filed a FTBFS bug in hours. That's fixed on the next branch that I'll be pushing out shortly, along with your patches.
- Ted
Quoted 19 lines · Ted Ts'o
Quoted 5 lines · Eric Sandeen
sounds like a plan ... though too bad that's One More Thing which must be done prior to a 64-bit-capable release :(
by the way, any plans to put a new -WIP-tagged tarball anywhere?
Sorry, I hadn't gotten around to sending out an announcement yet. Since kernel.org is down, I've gone back to using the old sourceforge distribution method:
http://sourceforge.net/projects/e2fsprogs/files/e2fsprogs-TEST/1.42-WIP-0916/
As always, the tarball is GPG-signed, and the git tag is als GPG-signed, from these two sites:
git://github.com/tytso/e2fsprogs.git
git://e2fsprogs.git.sourceforge.net/gitroot/e2fsprogs/e2fsprogs
Quoted 7 lines
BTW, I already know that the 0916 release is broken on big-endian machines. A Debian developer filed a FTBFS bug in hours. That's fixed on the next branch that I'll be pushing out shortly, along with your patches.
- Ted
Thanks, will that be another -WIP then?
As soon as there's a nice -WIP release I'll push a new one to rawhide as well.
-Eric