summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 22c430a)
raw | patch | inline | side by side (parent: 22c430a)
author | Jim Meyering <meyering@redhat.com> | |
Thu, 31 Jan 2008 17:26:32 +0000 (18:26 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Fri, 22 Feb 2008 22:14:40 +0000 (14:14 -0800) |
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>
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>
17 files changed:
builtin-blame.c | patch | blob | history | |
builtin-branch.c | patch | blob | history | |
builtin-fast-export.c | patch | blob | history | |
builtin-http-fetch.c | patch | blob | history | |
builtin-pack-objects.c | patch | blob | history | |
builtin-revert.c | patch | blob | history | |
connect.c | patch | blob | history | |
diff.c | patch | blob | history | |
dir.c | patch | blob | history | |
http-push.c | patch | blob | history | |
imap-send.c | patch | blob | history | |
interpolate.c | patch | blob | history | |
pretty.c | patch | blob | history | |
remote.c | patch | blob | history | |
setup.c | patch | blob | history | |
sha1_name.c | patch | blob | history | |
xdiff-interface.c | patch | blob | history |
diff --git a/builtin-blame.c b/builtin-blame.c
index 59d7237f21f406b71aabc66ea7997faf98981869..bfd562d7d2b897ad4ab3d35dda33701e315b7d5d 100644 (file)
--- a/builtin-blame.c
+++ b/builtin-blame.c
static void origin_decref(struct origin *o)
{
if (o && --o->refcnt <= 0) {
- if (o->file.ptr)
- free(o->file.ptr);
+ free(o->file.ptr);
free(o);
}
}
diff --git a/builtin-branch.c b/builtin-branch.c
index 9edf2eb8162eb7214e1b4d76dca8949c63e72847..79177007e617ef2b77c5e39dbdedbf9c668cbe25 100644 (file)
--- a/builtin-branch.c
+++ b/builtin-branch.c
continue;
}
- if (name)
- free(name);
+ free(name);
name = xstrdup(mkpath(fmt, argv[i]));
if (!resolve_ref(name, sha1, 1, NULL)) {
}
}
- if (name)
- free(name);
+ free(name);
return(ret);
}
if (write_ref_sha1(lock, sha1, msg) < 0)
die("Failed to write ref: %s.", strerror(errno));
- if (real_ref)
- free(real_ref);
+ free(real_ref);
}
static void rename_branch(const char *oldname, const char *newname, int force)
diff --git a/builtin-fast-export.c b/builtin-fast-export.c
index f741df522014b9495f18cf13c2e01382da71c41d..49b54de0541b4537ac8da51018fa7f5a7276b71e 100755 (executable)
--- a/builtin-fast-export.c
+++ b/builtin-fast-export.c
? strlen(reencoded) : message
? strlen(message) : 0),
reencoded ? reencoded : message ? message : "");
- if (reencoded)
- free(reencoded);
+ free(reencoded);
for (i = 0, p = commit->parents; p; p = p->next) {
int mark = get_object_mark(&p->item->object);
diff --git a/builtin-http-fetch.c b/builtin-http-fetch.c
index 7f450c61d95945862fc44bec99859a229269b224..299093ff9134f517e53b5105ec356e6575df6214 100644 (file)
--- a/builtin-http-fetch.c
+++ b/builtin-http-fetch.c
walker_free(walker);
- if (rewritten_url)
- free(rewritten_url);
+ free(rewritten_url);
return rc;
}
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index d2bb12e574fb8e18f9b1e3d241f73f732ac740d3..7dff6536dea3b725a95fcd17b001c7297b251151 100644 (file)
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
* 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);
+ free(trg_entry->delta_data);
cache_lock();
if (trg_entry->delta_data) {
delta_cache_size -= trg_entry->delta_size;
diff --git a/builtin-revert.c b/builtin-revert.c
index e219859f9b04dc94a2444346541e86503085fdea..b6dee6a56c21be816725a4366f448dac98e62ae6 100644 (file)
--- a/builtin-revert.c
+++ b/builtin-revert.c
else
return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
}
- if (reencoded_message)
- free(reencoded_message);
+ free(reencoded_message);
return 0;
}
diff --git a/connect.c b/connect.c
index 5ac357278464324d82ac6912c1b7ad84d42d0743..d12b105970bca82017998cae52f5807629736bb9 100644 (file)
--- a/connect.c
+++ b/connect.c
name_len = strlen(name);
if (len != name_len + 41) {
- if (server_capabilities)
- free(server_capabilities);
+ free(server_capabilities);
server_capabilities = xstrdup(name + name_len + 1);
}
index 699b21f4e347b38498e23ad5258a94cf4289bae9..f08e663b865ca545b871b153160104d4a3eb329e 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -118,8 +118,7 @@ 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);
+ free(pp->pattern);
pp->pattern = xstrdup(value);
return 0;
}
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->minus.text.ptr);
+ free (ecbdata->diff_words->plus.text.ptr);
free(ecbdata->diff_words);
ecbdata->diff_words = NULL;
}
index 1f507daff2c278a70da768e3754a68891b946973..edc458e020772a7ab704e9cf69786d3aa641bcd4 100644 (file)
--- a/dir.c
+++ b/dir.c
static void free_simplify(struct path_simplify *simplify)
{
- if (simplify)
- free(simplify);
+ free(simplify);
}
int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
diff --git a/http-push.c b/http-push.c
index 0beb7406c34a19403062a6a2e2aeeee35566a45f..406270f6f43d02eea1210b0363816363ed5a5816 100644 (file)
--- a/http-push.c
+++ b/http-push.c
close(request->local_fileno);
if (request->local_stream)
fclose(request->local_stream);
- if (request->url != NULL)
- free(request->url);
+ free(request->url);
free(request);
}
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(lock->token);
+ free(lock->owner);
free(url);
free(lock);
lock = NULL;
prev->next = prev->next->next;
}
- if (lock->owner != NULL)
- free(lock->owner);
+ free(lock->owner);
free(lock->url);
free(lock->token);
free(lock);
@@ -2035,8 +2031,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
}
free(url);
- if (*symref != NULL)
- free(*symref);
+ free(*symref);
*symref = NULL;
hashclr(sha1);
}
cleanup:
- if (rewritten_url)
- free(rewritten_url);
+ free(rewritten_url);
if (info_ref_lock)
unlock_remote(info_ref_lock);
free(remote);
diff --git a/imap-send.c b/imap-send.c
index 9025d9aa3ef37b1a1ce4ae5d4b447e3b0918cb7e..10cce15a427646a1281afa5197f40def39151154 100644 (file)
--- a/imap-send.c
+++ b/imap-send.c
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;
}
normal:
if (cmdp->cb.done)
cmdp->cb.done( ctx, cmdp, resp );
- if (cmdp->cb.data)
- free( cmdp->cb.data );
+ free( cmdp->cb.data );
free( cmdp->cmd );
free( cmdp );
if (!tcmd || tcmd == cmdp)
diff --git a/interpolate.c b/interpolate.c
index 6ef53f246511a1943e375d5d5913a4ec52e2c663..7f03bd99c5b66afa6cc7fa11a2430301a3042656 100644 (file)
--- a/interpolate.c
+++ b/interpolate.c
char *oldval = table[slot].value;
char *newval = NULL;
- if (oldval)
- free(oldval);
+ free(oldval);
if (value)
newval = xstrdup(value);
diff --git a/pretty.c b/pretty.c
index 997f5837d521ded5f158d12abc0c025f74e83341..4bf3be2dd9552b1881ec863835fa2cd28e008090 100644 (file)
--- a/pretty.c
+++ b/pretty.c
if (*arg == '=')
arg++;
if (!prefixcmp(arg, "format:")) {
- if (user_format)
- free(user_format);
+ free(user_format);
user_format = xstrdup(arg + 7);
return CMIT_FMT_USERFORMAT;
}
diff --git a/remote.c b/remote.c
index 6b56473f5bb7d8ab2226ed83805f30c9e21ba773..ae1ef5708ccb9031cc240516a32695b2c846ed4e 100644 (file)
--- a/remote.c
+++ b/remote.c
struct ref *next;
while (ref) {
next = ref->next;
- if (ref->peer_ref)
- free(ref->peer_ref);
+ free(ref->peer_ref);
free(ref);
ref = next;
}
index dc247a84c49709d2bff00440e5ac976df83acd2e..89c81e54e6d25d7ba2bec8831621283f32fd3108 100644 (file)
--- a/setup.c
+++ b/setup.c
} else if (strcmp(var, "core.worktree") == 0) {
if (!value)
return config_error_nonbool(var);
- if (git_work_tree_cfg)
- free(git_work_tree_cfg);
+ free(git_work_tree_cfg);
git_work_tree_cfg = xstrdup(value);
inside_work_tree = -1;
}
diff --git a/sha1_name.c b/sha1_name.c
index c2805e736b3086a0e33271a4b5c73cea7d2c55cd..9d088cc2caa24ad79b927b9078771e4f0fb22a69 100644 (file)
--- a/sha1_name.c
+++ b/sha1_name.c
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
if (!parse_object(commit->object.sha1))
continue;
- if (temp_commit_buffer)
- free(temp_commit_buffer);
+ free(temp_commit_buffer);
if (commit->buffer)
p = commit->buffer;
else {
break;
}
}
- if (temp_commit_buffer)
- free(temp_commit_buffer);
+ free(temp_commit_buffer);
free_commit_list(list);
for (l = backup; l; l = l->next)
clear_commit_marks(l->item, ONELINE_SEEN);
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 4b8e5cca804198b0e227454a585fa025281bbe2d..bba236428adb0b34421b9f1b5a3a1728911ee406 100644 (file)
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
expression = value;
if (regcomp(®->re, expression, 0))
die("Invalid regexp to look for hunk header: %s", expression);
- if (buffer)
- free(buffer);
+ free(buffer);
value = ep + 1;
}
}