Skip to contentSkip to conversation

remove unnecessary NULL check before free(3)

PATCH New René Scharfe 1 participant 1 message Oct 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remove unnecessary NULL check before free(3)

free(3) handles NULL pointers just fine. Add a semantic patch for removing unnecessary NULL checks before calling this function, and apply it on the code base.

---
 contrib/coccinelle/free.cocci | 5 +++++
 parse-options-cb.c            | 3 +--
 2 files changed, 6 insertions(+), 2 deletions(-)
 create mode 100644 contrib/coccinelle/free.cocci
contrib/coccinelle/free.cocci new file +5 -0
new file mode 100644
index 0000000..e282131
@@ -0,0 +1,5 @@
+@@
+expression E;
+@@
+- if (E)
+  free(E);
parse-options-cb.c +1 -2
index b5d9209..b7d8f7d 100644
@@ -211,8 +211,7 @@ int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
 	if (recreate_opt(&sb, opt, arg, unset) < 0)
 		return -1;
 
-	if (*opt_value)
-		free(*opt_value);
+	free(*opt_value);
 
 	*opt_value = strbuf_detach(&sb, NULL);
 
Signature · 1 line
2.10.0