remove useless if-before-free tests
PATCH 1/1 remove useless if-before-free tests
Hello,
I noticed that the strdup-leak-fixing patch I just posted has a prerequisite, otherwise it won't apply. Apply the following patch first:
(if that's a problem, I can rework it)
In case you're wondering about whether this change is safe from a portability standpoint, fear not. This has been beaten to death in other forums. Here are a few threads:
http://thread.gmane.org/gmane.comp.version-control.git/74187
http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/12712
http://thread.gmane.org/gmane.emacs.devel/98144
http://thread.gmane.org/gmane.comp.lib.glibc.alpha/13092
There has been debate about whether it's a good idea from a performance standpoint, too, but imho you'll have a hard time finding an instance where this sort of change induces a measurable performance penalty. If you do, please let me know.
As for whether I made mistakes while making these changes, that is always possible. However, all but four hunks were changed automatically by running this:
mkid; useless-if-before-free -l $(lid -knone free) \
| xargs -0 perl -0x3b -pi -e \
's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\((?:\s*\([^)]+\))?\s*\1\s*\))/$2/s'
The remaining four were spotted by running this:
git ls-files -z |xargs -0 useless-if-before-free
and then I manually made changes like this (all in nt_io.c):
- if(NULL != io->name)
- {
- free(io->name);
- }
-
+ free(io->name);
Quoted 1 line
From
532bf81f7a60a750ea22419f711e78f97ea91a11Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email protected]>
Date: Mon, 23 Feb 2009 17:52:01 +0100
Subject: [PATCH] remove useless if-before-free tests
--- debugfs/htree.c | 3 +-- debugfs/icheck.c | 3 +-- e2fsck/argv_parse.c | 5 ++--- e2fsck/dirinfo.c | 3 +-- e2fsck/profile.c | 9 +++------ e2fsck/rehash.c | 12 ++++-------- intl/bindtextdom.c | 3 +-- intl/loadmsgcat.c | 6 ++---- intl/localcharset.c | 3 +-- intl/printf-parse.c | 6 ++---- intl/vasnprintf.c | 9 +++------ lib/blkid/cache.c | 3 +-- lib/blkid/dev.c | 15 +++++---------- lib/blkid/devno.c | 3 +-- lib/blkid/probe.c | 6 ++---- lib/blkid/resolve.c | 6 ++---- lib/blkid/save.c | 3 +-- lib/blkid/tag.c | 9 +++------ lib/ext2fs/dosio.c | 15 +++++---------- lib/ext2fs/extent.c | 3 +-- lib/ext2fs/initialize.c | 3 +-- lib/ext2fs/nt_io.c | 25 ++++--------------------- lib/ss/execute_cmd.c | 3 +-- misc/badblocks.c | 3 +-- misc/blkid.c | 6 ++---- misc/e2initrd_helper.c | 3 +-- misc/fsck.c | 12 ++++-------- misc/mke2fs.c | 12 ++++-------- misc/tune2fs.c | 3 +-- resize/resize2fs.c | 3 +-- util/subst.c | 6 ++---- 31 files changed, 64 insertions(+), 140 deletions(-)
debugfs/htree.c +1 -2
@@ -275,8 +275,7 @@ void do_htree_dump(int argc, char *argv[]) rootnode->indirect_levels); errout: -if (buf) -free (buf); +free (buf);close_pager (pager); }
debugfs/icheck.c +1 -2
@@ -161,8 +161,7 @@ void do_icheck(int argc, char **argv) error_out:free (bw.barray); -if (block_buf) -free (block_buf); +free (block_buf);if (scan)ext2fs_close_inode_scan (scan);return ;
e2fsck/argv_parse.c +2 -3
@@ -66,7 +66,7 @@ int argv_parse(char *in_buf, int *ret_argc, char ***ret_argv) new_argv =realloc (argv, (max_argc+1 )*sizeof (char *));if (!new_argv) { -if (argv)free (argv); +free (argv);free (buf);return -1 ; }
@@ -126,8 +126,7 @@ int argv_parse(char *in_buf, int *ret_argc, char ***ret_argv)void argv_free (char **argv) { -if (*argv) -free (*argv); +free (*argv);free (argv); }
e2fsck/dirinfo.c +1 -2
@@ -326,8 +326,7 @@ extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx) extern void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)), struct dir_info_iter *iter) { -if (iter->tdb_iter.dptr) -free (iter->tdb_iter.dptr); +free (iter->tdb_iter.dptr);ext2fs_free_mem (&iter); }
e2fsck/profile.c +3 -6
@@ -603,8 +603,7 @@ void profile_free_file(prf_file_t prf) {if (prf->root)profile_free_node (prf->root); -if (prf->filespec) -free (prf->filespec); +free (prf->filespec);free (prf); }
@@ -1049,10 +1048,8 @@ void profile_free_node(struct profile_node *node)if (node->magic !=PROF_MAGIC_NODE )return ; -if (node->name) -free (node->name); -if (node->value) -free (node->value); +free (node->name); +free (node->value);for (child=node->first_child; child; child = next) { next = child->next;
e2fsck/rehash.c +4 -8
@@ -247,10 +247,8 @@ static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,static void free_out_dir (struct out_dir *outdir) { -if (outdir->buf) -free (outdir->buf); -if (outdir->hashes) -free (outdir->hashes); +free (outdir->buf); +free (outdir->hashes); outdir->max =0 ; outdir->num =0 ; }
@@ -768,10 +766,8 @@ resort: goto errout; errout: -if (dir_buf) -free (dir_buf); -if (fd.harray) -free (fd.harray); +free (dir_buf); +free (fd.harray);free_out_dir (&outdir);return retval;
intl/bindtextdom.c +1 -2
@@ -197,8 +197,7 @@ set_binding_values (const char *domainname,if (__builtin_expect (result !=NULL ,1 )) { -if (binding->codeset !=NULL ) -free (binding->codeset); +free (binding->codeset); binding->codeset = result; binding->codeset_cntr++;
intl/loadmsgcat.c +2 -4
@@ -1372,8 +1372,7 @@ _nl_load_domain (struct loaded_l10nfile *domain_file,/* This is an invalid revision. */ invalid:/* This is an invalid .mo file. */ -if (domain->malloced) -free (domain->malloced); +free (domain->malloced);#ifdef HAVE_MMAP if (use_mmap)munmap ((void *) data, size);
@@ -1405,8 +1404,7 @@ _nl_unload_domain (struct loaded_domain *domain)_nl_free_domain_conv (domain); -if (domain->malloced) -free (domain->malloced); +free (domain->malloced);# ifdef _POSIX_MAPPED_FILESif (domain->use_mmap)
intl/localcharset.c +1 -2
@@ -199,8 +199,7 @@ get_charset_aliases () } } -if (file_name !=NULL ) -free (file_name); +free (file_name);#else
intl/printf-parse.c +2 -4
@@ -524,10 +524,8 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)return 0 ; error: -if (a->arg) -free (a->arg); -if (d->dir) -free (d->dir); +free (a->arg); +free (d->dir);return -1 ; }
intl/vasnprintf.c +3 -6
@@ -801,8 +801,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar {if (!(result == resultbuf || result ==NULL ))free (result); -if (buf_malloced !=NULL ) -free (buf_malloced); +free (buf_malloced);CLEANUP (); errno =EINVAL ;return NULL ;
@@ -860,8 +859,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar result = memory; } -if (buf_malloced !=NULL ) -free (buf_malloced); +free (buf_malloced);CLEANUP (); *lengthp = length;return result;
@@ -869,8 +867,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar out_of_memory:if (!(result == resultbuf || result ==NULL ))free (result); -if (buf_malloced !=NULL ) -free (buf_malloced); +free (buf_malloced); out_of_memory_1:CLEANUP (); errno =ENOMEM ;
lib/blkid/cache.c +1 -2
@@ -146,8 +146,7 @@ void blkid_put_cache(blkid_cache cache) }blkid_free_tag (tag); } -if (cache->bic_filename) -free (cache->bic_filename); +free (cache->bic_filename);free (cache); }
lib/blkid/dev.c +5 -10
@@ -45,8 +45,7 @@ void blkid_free_dev(blkid_dev dev) bit_tags);blkid_free_tag (tag); } -if (dev->bid_name) -free (dev->bid_name); +free (dev->bid_name);free (dev); }
@@ -137,18 +136,14 @@ extern int blkid_dev_set_search(blkid_dev_iterate iter, new_type =malloc (strlen (search_type)+1 ); new_value =malloc (strlen (search_value)+1 );if (!new_type || !new_value) { -if (new_type) -free (new_type); -if (new_value) -free (new_value); +free (new_type); +free (new_value);return -1 ; }strcpy (new_type, search_type);strcpy (new_value, search_value); -if (iter->search_type) -free (iter->search_type); -if (iter->search_value) -free (iter->search_value); +free (iter->search_type); +free (iter->search_value); iter->search_type = new_type; iter->search_value = new_value;return 0 ;
lib/blkid/devno.c +1 -2
@@ -222,8 +222,7 @@ int main(int argc, char** argv) }printf ("Looking for device 0x%04llx\n" , (long long )devno); devname =blkid_devno_to_devname (devno); -if (devname) -free (devname); +free (devname);return 0 ; }#endif
lib/blkid/probe.c +2 -4
@@ -1528,10 +1528,8 @@ found_type: dev->bid_name, (long long)st.st_rdev, type)); } -if (probe.sbbuf) -free (probe.sbbuf); -if (probe.buf) -free (probe.buf); +free (probe.sbbuf); +free (probe.buf);if (probe.fd >=0 )close (probe.fd);
lib/blkid/resolve.c +2 -4
@@ -97,10 +97,8 @@ char *blkid_get_devname(blkid_cache cache, const char *token, ret =blkid_strdup (blkid_dev_devname (dev)); out: -if (t) -free (t); -if (v) -free (v); +free (t); +free (v);if (!cache) {blkid_put_cache (c); }
lib/blkid/save.c +1 -2
@@ -153,8 +153,7 @@ int blkid_flush_cache(blkid_cache cache) } errout: -if (tmp) -free (tmp); +free (tmp);return ret; }
lib/blkid/tag.c +3 -6
@@ -54,10 +54,8 @@ void blkid_free_tag(blkid_tag tag)list_del (&tag->bit_tags);/* list of tags for this device */ list_del (&tag->bit_names);/* list of tags with this type */ -if (tag->bit_name) -free (tag->bit_name); -if (tag->bit_val) -free (tag->bit_val); +free (tag->bit_name); +free (tag->bit_val);free (tag); }
@@ -206,8 +204,7 @@ int blkid_set_tag(blkid_dev dev, const char *name, errout:if (t)blkid_free_tag (t); -else if (val) -free (val); +else free (val);if (head)blkid_free_tag (head);return -BLKID_ERR_MEM ;
lib/ext2fs/dosio.c +5 -10
@@ -278,8 +278,7 @@ static errcode_t dos_open(const char *dev, int flags, io_channel *channel)if (!HW_OK ()) { _dio_error =ERR_HARDWARE ; -if (part) -free (part); +free (part);return EFAULT ; }
@@ -298,8 +297,7 @@ static errcode_t dos_open(const char *dev, int flags, io_channel *channel)if (!HW_OK ()) { _dio_error =ERR_HARDWARE ; -if (part) -free (part); +free (part);return EFAULT ; }
@@ -310,8 +308,7 @@ static errcode_t dos_open(const char *dev, int flags, io_channel *channel) { _dio_error = part->pno ==0xFE ?ERR_EMPTYPART : part->pno ==0xFD ?ERR_LINUXSWAP :ERR_NOTEXT2FS ; -if (part) -free (part); +free (part);return ENODEV ; }
@@ -352,10 +349,8 @@ static errcode_t dos_open(const char *dev, int flags, io_channel *channel)static errcode_t dos_close (io_channel channel) { -if (channel->name) -free (channel->name); -if (channel) -free (channel); +free (channel->name); +free (channel);return 0 ; }
lib/ext2fs/extent.c +1 -2
@@ -1005,8 +1005,7 @@ static errcode_t extent_node_split(ext2_extent_handle_t handle) done:if (newpath)ext2fs_free_mem (&newpath); -if (block_buf) -free (block_buf); +free (block_buf);return retval; }
lib/ext2fs/initialize.c +1 -2
@@ -437,8 +437,7 @@ ipg_retry: *ret_fs = fs;return 0 ; cleanup: -if (buf) -free (buf); +free (buf);ext2fs_free (fs);return retval; }
lib/ext2fs/nt_io.c +4 -21
@@ -1191,11 +1191,7 @@ nt_open(const char *name, int flags, io_channel *channel)if (NULL != io) { -if (NULL != io->name) - { -free (io->name); - } - +free (io->name);free (io); }
@@ -1207,11 +1203,7 @@ nt_open(const char *name, int flags, io_channel *channel)_CloseDisk (NtData->Handle); } -if (NULL != NtData->Buffer) - { -free (NtData->Buffer); - } - +free (NtData->Buffer);free (NtData); } }
@@ -1245,12 +1237,7 @@ nt_close(io_channel channel)return 0 ; } -if (NULL != channel->name) - { -free (channel->name); - } - - +free (channel->name);free (channel);if (NULL != NtData)
@@ -1262,11 +1249,7 @@ nt_close(io_channel channel)_CloseDisk (NtData->Handle); } -if (NULL != NtData->Buffer) - { -free (NtData->Buffer); - } - +free (NtData->Buffer);free (NtData); }
lib/ss/execute_cmd.c +1 -2
@@ -221,8 +221,7 @@ int ss_execute_line (sci_idx, line_ptr)/* parse it */ argv =ss_parse (sci_idx, line_ptr, &argc);if (argc ==0 ) { -if (argv) -free (argv); +free (argv);return 0 ; }
misc/badblocks.c +1 -2
@@ -1237,8 +1237,7 @@ int main (int argc, char ** argv)close (dev);if (out != stdout)fclose (out); -if (t_patts) -free (t_patts); +free (t_patts);return 0 ; }
misc/blkid.c +2 -4
@@ -420,10 +420,8 @@ int main(int argc, char **argv) } exit: -if (search_type) -free (search_type); -if (search_value) -free (search_value); +free (search_type); +free (search_value);blkid_put_cache (cache);return err; }
misc/e2initrd_helper.c +1 -2
@@ -274,8 +274,7 @@ static int parse_fstab_line(char *line, struct fs_info *fs) fs->flags =0 ; fs->next =NULL ; -if (dev) -free (dev); +free (dev);return 0 ; }
misc/fsck.c +4 -8
@@ -233,12 +233,9 @@ static void parse_escape(char *word)static void free_instance (struct fsck_instance *i) { -if (i->prog) -free (i->prog); -if (i->device) -free (i->device); -if (i->base_device) -free (i->base_device); +free (i->prog); +free (i->device); +free (i->base_device);free (i);return ; }
@@ -310,8 +307,7 @@ static int parse_fstab_line(char *line, struct fs_info **ret_fs) fs =create_fs_device (device, mntpnt, type ? type :"auto" , opts, freq ?atoi (freq) :-1 , passno ?atoi (passno) :-1 ); -if (dev) -free (dev); +free (dev);if (!fs)return -1 ;
misc/mke2fs.c +4 -8
@@ -1021,8 +1021,7 @@ static char **parse_fs_type(const char *fs_type, } }free (parse_str); -if (profile_type) -free (profile_type); +free (profile_type);if (is_hurd)push_string (&list,"hurd" );return (list.list);
@@ -1499,16 +1498,14 @@ static void PRS(int argc, char *argv[]) "features", "", &tmp);if (tmp && *tmp)edit_feature (tmp, &fs_param.s_feature_compat); -if (tmp) -free (tmp); +free (tmp); } tmp =get_string_from_profile (fs_types,"default_features" ,"" ); }edit_feature (fs_features ? fs_features : tmp, &fs_param.s_feature_compat); -if (tmp) -free (tmp); +free (tmp);if (fs_param.s_feature_incompat &EXT3_FEATURE_INCOMPAT_JOURNAL_DEV ) { fs_types[0 ] =strdup ("journal" );
@@ -1622,8 +1619,7 @@ static void PRS(int argc, char *argv[])profile_get_string (profile,"fs_types" , *cpp,"options" ,"" , &tmp);if (tmp && *tmp)parse_extended_opts (&fs_param, tmp); -if (tmp) -free (tmp); +free (tmp); }if (extended_opts)
misc/tune2fs.c +1 -2
@@ -529,8 +529,7 @@ static void add_journal(ext2_filsys fs)return ; err: -if (journal_device) -free (journal_device); +free (journal_device);exit (1 ); }
resize/resize2fs.c +1 -2
@@ -1359,8 +1359,7 @@ errout:ext2fs_close_inode_scan (scan);if (block_buf)ext2fs_free_mem (&block_buf); -if (inode) -free (inode); +free (inode);return retval; }
util/subst.c +2 -4
@@ -54,10 +54,8 @@ static int add_subst(char *name, char *value)return 0 ; fail:if (ent) { -if (ent->name) -free (ent->name); -if (ent->value) -free (ent->value); +free (ent->name); +free (ent->value);free (ent); }return retval;
--
1.6.2.rc1.266.g4bdf
Theodore Tso
AppliedThanks, applied.
- Ted