Skip to contentSkip to conversation

clvmd.c: Remove redundant if-before-free tests.

PATCH New Jim Meyering 2 participants 3 messages Feb 14, 2008 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clvmd.c: Remove redundant if-before-free tests.

Since "free (NULL);" is universally safe these days, I propose to remove all of lvm's redundant "if-before-free" tests. There are only 5. If no one objects, I'll commit it tonight.

In case you need more justification than "POSIX requires it", (understandable, that :-), the following was enough for Wine development a year and a half ago:

    http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html

Sure, the extra test is not a big deal, but removing it does make the code slightly smaller, and maybe even more readable (people won't wonder about the test of such a pointer, when they know that free can handle a NULL pointer).

The changes in the patch below were created by running this command after a fresh "clone":

  find . -name '*.c' -print0 | xargs -0 perl -0x3b -pi -e \
     's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\(\s*\1\s*\))/$2/s'
daemons/clvmd/clvmd.c +5 -10
index 574010f..14ee1c6 100644
@@ -903,8 +903,7 @@ static int read_from_local_sock(struct local_client *thisfd)
 		}
 
 		/* Free the command buffer */
-		if (thisfd->bits.localsock.cmd)
-			free(thisfd->bits.localsock.cmd);
+		free(thisfd->bits.localsock.cmd);
 
 		/* Clear out the cross-link */
 		if (thisfd->bits.localsock.pipe_client != NULL)
@@ -939,8 +938,7 @@ static int read_from_local_sock(struct local_client *thisfd)
 		}
 
 		/* Free any old buffer space */
-		if (thisfd->bits.localsock.cmd)
-			free(thisfd->bits.localsock.cmd);
+		free(thisfd->bits.localsock.cmd);
 
 		/* See if we have the whole message */
 		argslen =
@@ -1547,8 +1545,7 @@ static void send_local_reply(struct local_client *client, int status, int fd)
 		}
 		thisreply = thisreply->next;
 
-		if (tempreply->replymsg)
-			free(tempreply->replymsg);
+		free(tempreply->replymsg);
 		free(tempreply);
 	}
 
@@ -1579,8 +1576,7 @@ static void free_reply(struct local_client *client)
 
 		thisreply = thisreply->next;
 
-		if (tempreply->replymsg)
-			free(tempreply->replymsg);
+		free(tempreply->replymsg);
 		free(tempreply);
 	}
 	client->bits.localsock.replies = NULL;
@@ -1732,8 +1728,7 @@ static __attribute__ ((noreturn)) void *lvm_thread_fn(void *arg)
 			pthread_mutex_unlock(&lvm_thread_mutex);
 
 			process_work_item(cmd);
-			if (cmd->msg)
-				free(cmd->msg);
+			free(cmd->msg);
 			free(cmd);
 
 			pthread_mutex_lock(&lvm_thread_mutex);

--
1.5.4.1.98.gf3293

#1 lore raw

PATCH 1/1 Remove redundant if-before-free tests.

[as I just wrote to lvm-devel:
http://www.redhat.com/archives/lvm-devel/2008-February/msg00017.html ]

Quoted 8 lines · Jim Meyering

Since "free (NULL);" is universally safe these days, I propose to remove all of lvm's redundant "if-before-free" tests. If no one objects, I'll commit it tonight.

In case you need more justification than "POSIX requires it", (understandable, that :-), the following was enough for Wine development a year and a half ago:

Quoted 6 lines
    http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html

Sure, the extra test is not a big deal, but removing it does make the code slightly smaller, and maybe even more readable (people won't wonder about the test of such a pointer, when they know that free can handle a NULL pointer).

Same for dm, but here there are only 3:

---
 dmeventd/old/dmevent.c               |    6 ++----
 multilog/pthread_lock/pthread_lock.c |    3 +--
 2 files changed, 3 insertions(+), 6 deletions(-)
dmeventd/old/dmevent.c +2 -4
index 6e9eb46..5d80410 100644
@@ -220,10 +220,8 @@ int main(int argc, char **argv)
    out:
 	multilog_del_type(standard);
 
-	if (device)
-		free(device);
-	if (dso_name)
-		free(dso_name);
+	free(device);
+	free(dso_name);
 
 	exit(ret);
 }
multilog/pthread_lock/pthread_lock.c +1 -2
index b782301..c52050b 100644
@@ -62,7 +62,6 @@ int unlock_fn(void *handle)
 void destroy_locking(void *handle)
 {
         pthread_mutex_t *mutex = (pthread_mutex_t *)handle;
-	if(mutex)
-		free(mutex);
+	free(mutex);
 }
 

--
1.5.4.1.98.gf3293

#2 lore raw

Re: [lvm-devel] [PATCH] Remove redundant if-before-free tests.

Quoted 1 line · Jim Meyering

Same for dm, but here there are only 3:

2 of which are in obsolete code - don't change those (the dir will get dropped when we combine the trees); the other of which is in unfinished & unreviewed code (yes, change that one)

Alasdair

Signature · 1 line
[email protected]