NULL-checks before free()

Michael McConville mmcco at mykolab.com
Thu Dec 10 00:35:48 UTC 2015


I thought I'd send a few quick cleanup diffs if people are interested.
I'm an OpenBSD dev and we include Unbound in our base system, so these
are just things I've noticed over the past few months.

Here's the first one. POSIX specifies that free() is NULL-safe. All
modern and almost all prehistoric platforms conform to this, and most
codebases already assume it.


Index: testcode/replay.c
===================================================================
--- testcode/replay.c	(revision 3578)
+++ testcode/replay.c	(working copy)
@@ -499,8 +499,7 @@
 	struct replay_range* rng, *rngn;
 	if(!scen)
 		return;
-	if(scen->title)
-		free(scen->title);
+	free(scen->title);
 	mom = scen->mom_first;
 	while(mom) {
 		momn = mom->mom_next;
Index: util/data/packed_rrset.c
===================================================================
--- util/data/packed_rrset.c	(revision 3578)
+++ util/data/packed_rrset.c	(working copy)
@@ -57,11 +57,9 @@
 {
 	if(!pkey)
 		return;
-	if(pkey->entry.data)
-		free(pkey->entry.data);
+	free(pkey->entry.data);
 	pkey->entry.data = NULL;
-	if(pkey->rk.dname)
-		free(pkey->rk.dname);
+	free(pkey->rk.dname);
 	pkey->rk.dname = NULL;
 	pkey->id = 0;
 	alloc_special_release(alloc, pkey);
Index: util/mini_event.c
===================================================================
--- util/mini_event.c	(revision 3578)
+++ util/mini_event.c	(working copy)
@@ -261,12 +261,9 @@
 {
 	if(!base)
 		return;
-	if(base->times)
-		free(base->times);
-	if(base->fds)
-		free(base->fds);
-	if(base->signals)
-		free(base->signals);
+	free(base->times);
+	free(base->fds);
+	free(base->signals);
 	free(base);
 }
 
Index: util/random.c
===================================================================
--- util/random.c	(revision 3578)
+++ util/random.c	(working copy)
@@ -228,7 +228,6 @@
 void 
 ub_randfree(struct ub_randstate* s)
 {
-	if(s)
-		free(s);
+	free(s);
 	/* user app must do RAND_cleanup(); */
 }
Index: util/tube.c
===================================================================
--- util/tube.c	(revision 3578)
+++ util/tube.c	(working copy)
@@ -118,10 +118,8 @@
 		comm_point_delete(tube->listen_com);
 		tube->listen_com = NULL;
 	}
-	if(tube->cmd_msg) {
-		free(tube->cmd_msg);
-		tube->cmd_msg = NULL;
-	}
+	free(tube->cmd_msg);
+	tube->cmd_msg = NULL;
 }
 
 void tube_remove_bg_write(struct tube* tube)
Index: util/winsock_event.c
===================================================================
--- util/winsock_event.c	(revision 3578)
+++ util/winsock_event.c	(working copy)
@@ -459,12 +459,9 @@
 	verbose(VERB_CLIENT, "winsock_event event_base_free");
         if(!base)
                 return;
-	if(base->items)
-		free(base->items);
-        if(base->times)
-                free(base->times);
-        if(base->signals)
-                free(base->signals);
+	free(base->items);
+        free(base->times);
+        free(base->signals);
         free(base);
 }
 



More information about the Unbound-users mailing list