Avoid unnecessary "if-before-free" tests.
This change removes all obvious useless if-before-free tests. E.g., it replaces code like this: if (some_expression) free (some_expression); with the now-equivalent: free (some_expression); It is equivalent not just because POSIX has required free(NULL) to work for a long time, but simply because it has worked for so long that no reasonable porting target fails the test. Here's some evidence from nearly 1.5 years ago: http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html FYI, the change below was prepared by running the following: git ls-files -z | xargs -0 \ perl -0x3b -pi -e \ 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\(\s*\1\s*\))/$2/s' Note however, that it doesn't handle brace-enclosed blocks like "if (x) { free (x); }". But that's ok, since there were none like that in git sources. Beware: if you do use the above snippet, note that it can produce syntactically invalid C code. That happens when the affected "if"-statement has a matching "else". E.g., it would transform this if (x) free (x); else foo (); into this: free (x); else foo (); There were none of those here, either. If you're interested in automating detection of the useless tests, you might like the useless-if-before-free script in gnulib: [it *does* detect brace-enclosed free statements, and has a --name=S option to make it detect free-like functions with different names] http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=build-aux/useless-if-before-free Addendum: Remove one more (in imap-send.c), spotted by Jean-Luc Herren <jlh@gmx.ch>. Signed-off-by: Jim Meyering <meyering@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
22c430ad84
commit
8e0f70033b
@ -123,7 +123,6 @@ static inline struct origin *origin_incref(struct origin *o)
|
||||
static void origin_decref(struct origin *o)
|
||||
{
|
||||
if (o && --o->refcnt <= 0) {
|
||||
if (o->file.ptr)
|
||||
free(o->file.ptr);
|
||||
free(o);
|
||||
}
|
||||
|
@ -126,7 +126,6 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (name)
|
||||
free(name);
|
||||
|
||||
name = xstrdup(mkpath(fmt, argv[i]));
|
||||
@ -172,7 +171,6 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
|
||||
}
|
||||
}
|
||||
|
||||
if (name)
|
||||
free(name);
|
||||
|
||||
return(ret);
|
||||
@ -490,7 +488,6 @@ static void create_branch(const char *name, const char *start_name,
|
||||
if (write_ref_sha1(lock, sha1, msg) < 0)
|
||||
die("Failed to write ref: %s.", strerror(errno));
|
||||
|
||||
if (real_ref)
|
||||
free(real_ref);
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,6 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
|
||||
? strlen(reencoded) : message
|
||||
? strlen(message) : 0),
|
||||
reencoded ? reencoded : message ? message : "");
|
||||
if (reencoded)
|
||||
free(reencoded);
|
||||
|
||||
for (i = 0, p = commit->parents; p; p = p->next) {
|
||||
|
@ -80,7 +80,6 @@ int cmd_http_fetch(int argc, const char **argv, const char *prefix)
|
||||
|
||||
walker_free(walker);
|
||||
|
||||
if (rewritten_url)
|
||||
free(rewritten_url);
|
||||
|
||||
return rc;
|
||||
|
@ -1428,7 +1428,6 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
|
||||
* accounting lock. Compiler will optimize the strangeness
|
||||
* away when THREADED_DELTA_SEARCH is not defined.
|
||||
*/
|
||||
if (trg_entry->delta_data)
|
||||
free(trg_entry->delta_data);
|
||||
cache_lock();
|
||||
if (trg_entry->delta_data) {
|
||||
|
@ -397,7 +397,6 @@ static int revert_or_cherry_pick(int argc, const char **argv)
|
||||
else
|
||||
return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
|
||||
}
|
||||
if (reencoded_message)
|
||||
free(reencoded_message);
|
||||
|
||||
return 0;
|
||||
|
@ -68,7 +68,6 @@ struct ref **get_remote_heads(int in, struct ref **list,
|
||||
|
||||
name_len = strlen(name);
|
||||
if (len != name_len + 41) {
|
||||
if (server_capabilities)
|
||||
free(server_capabilities);
|
||||
server_capabilities = xstrdup(name + name_len + 1);
|
||||
}
|
||||
|
3
diff.c
3
diff.c
@ -118,7 +118,6 @@ static int parse_funcname_pattern(const char *var, const char *ep, const char *v
|
||||
pp->next = funcname_pattern_list;
|
||||
funcname_pattern_list = pp;
|
||||
}
|
||||
if (pp->pattern)
|
||||
free(pp->pattern);
|
||||
pp->pattern = xstrdup(value);
|
||||
return 0;
|
||||
@ -492,9 +491,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
|
||||
ecbdata->diff_words->plus.text.size)
|
||||
diff_words_show(ecbdata->diff_words);
|
||||
|
||||
if (ecbdata->diff_words->minus.text.ptr)
|
||||
free (ecbdata->diff_words->minus.text.ptr);
|
||||
if (ecbdata->diff_words->plus.text.ptr)
|
||||
free (ecbdata->diff_words->plus.text.ptr);
|
||||
free(ecbdata->diff_words);
|
||||
ecbdata->diff_words = NULL;
|
||||
|
1
dir.c
1
dir.c
@ -704,7 +704,6 @@ static struct path_simplify *create_simplify(const char **pathspec)
|
||||
|
||||
static void free_simplify(struct path_simplify *simplify)
|
||||
{
|
||||
if (simplify)
|
||||
free(simplify);
|
||||
}
|
||||
|
||||
|
@ -664,7 +664,6 @@ static void release_request(struct transfer_request *request)
|
||||
close(request->local_fileno);
|
||||
if (request->local_stream)
|
||||
fclose(request->local_stream);
|
||||
if (request->url != NULL)
|
||||
free(request->url);
|
||||
free(request);
|
||||
}
|
||||
@ -1283,9 +1282,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
|
||||
strbuf_release(&in_buffer);
|
||||
|
||||
if (lock->token == NULL || lock->timeout <= 0) {
|
||||
if (lock->token != NULL)
|
||||
free(lock->token);
|
||||
if (lock->owner != NULL)
|
||||
free(lock->owner);
|
||||
free(url);
|
||||
free(lock);
|
||||
@ -1344,7 +1341,6 @@ static int unlock_remote(struct remote_lock *lock)
|
||||
prev->next = prev->next->next;
|
||||
}
|
||||
|
||||
if (lock->owner != NULL)
|
||||
free(lock->owner);
|
||||
free(lock->url);
|
||||
free(lock->token);
|
||||
@ -2035,7 +2031,6 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
|
||||
}
|
||||
free(url);
|
||||
|
||||
if (*symref != NULL)
|
||||
free(*symref);
|
||||
*symref = NULL;
|
||||
hashclr(sha1);
|
||||
@ -2435,7 +2430,6 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (rewritten_url)
|
||||
free(rewritten_url);
|
||||
if (info_ref_lock)
|
||||
unlock_remote(info_ref_lock);
|
||||
|
@ -472,7 +472,7 @@ v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
|
||||
if (socket_write( &imap->buf.sock, buf, bufl ) != bufl) {
|
||||
free( cmd->cmd );
|
||||
free( cmd );
|
||||
if (cb && cb->data)
|
||||
if (cb)
|
||||
free( cb->data );
|
||||
return NULL;
|
||||
}
|
||||
@ -858,7 +858,6 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
|
||||
normal:
|
||||
if (cmdp->cb.done)
|
||||
cmdp->cb.done( ctx, cmdp, resp );
|
||||
if (cmdp->cb.data)
|
||||
free( cmdp->cb.data );
|
||||
free( cmdp->cmd );
|
||||
free( cmdp );
|
||||
|
@ -11,7 +11,6 @@ void interp_set_entry(struct interp *table, int slot, const char *value)
|
||||
char *oldval = table[slot].value;
|
||||
char *newval = NULL;
|
||||
|
||||
if (oldval)
|
||||
free(oldval);
|
||||
|
||||
if (value)
|
||||
|
1
pretty.c
1
pretty.c
@ -30,7 +30,6 @@ enum cmit_fmt get_commit_format(const char *arg)
|
||||
if (*arg == '=')
|
||||
arg++;
|
||||
if (!prefixcmp(arg, "format:")) {
|
||||
if (user_format)
|
||||
free(user_format);
|
||||
user_format = xstrdup(arg + 7);
|
||||
return CMIT_FMT_USERFORMAT;
|
||||
|
1
remote.c
1
remote.c
@ -506,7 +506,6 @@ void free_refs(struct ref *ref)
|
||||
struct ref *next;
|
||||
while (ref) {
|
||||
next = ref->next;
|
||||
if (ref->peer_ref)
|
||||
free(ref->peer_ref);
|
||||
free(ref);
|
||||
ref = next;
|
||||
|
1
setup.c
1
setup.c
@ -448,7 +448,6 @@ int check_repository_format_version(const char *var, const char *value)
|
||||
} else if (strcmp(var, "core.worktree") == 0) {
|
||||
if (!value)
|
||||
return config_error_nonbool(var);
|
||||
if (git_work_tree_cfg)
|
||||
free(git_work_tree_cfg);
|
||||
git_work_tree_cfg = xstrdup(value);
|
||||
inside_work_tree = -1;
|
||||
|
@ -625,7 +625,6 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
|
||||
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
|
||||
if (!parse_object(commit->object.sha1))
|
||||
continue;
|
||||
if (temp_commit_buffer)
|
||||
free(temp_commit_buffer);
|
||||
if (commit->buffer)
|
||||
p = commit->buffer;
|
||||
@ -643,7 +642,6 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (temp_commit_buffer)
|
||||
free(temp_commit_buffer);
|
||||
free_commit_list(list);
|
||||
for (l = backup; l; l = l->next)
|
||||
|
@ -233,7 +233,6 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value)
|
||||
expression = value;
|
||||
if (regcomp(®->re, expression, 0))
|
||||
die("Invalid regexp to look for hunk header: %s", expression);
|
||||
if (buffer)
|
||||
free(buffer);
|
||||
value = ep + 1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user