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:
Jim Meyering
2008-01-31 18:26:32 +01:00
committed by Junio C Hamano
parent 22c430ad84
commit 8e0f70033b
17 changed files with 28 additions and 55 deletions

View File

@ -664,8 +664,7 @@ 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->url);
free(request);
}
@ -1283,10 +1282,8 @@ 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(lock->token);
free(lock->owner);
free(url);
free(lock);
lock = NULL;
@ -1344,8 +1341,7 @@ static int unlock_remote(struct remote_lock *lock)
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);
@ -2435,8 +2430,7 @@ int main(int argc, char **argv)
}
cleanup:
if (rewritten_url)
free(rewritten_url);
free(rewritten_url);
if (info_ref_lock)
unlock_remote(info_ref_lock);
free(remote);