[v2,02/83] devtools/cocci: add script to fix unnecessary null checks

Message ID 20220124174719.14417-3-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series remove unnecessary null checks |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Jan. 24, 2022, 5:45 p.m. UTC
  This script is based on the idea of the nullfree script
in the Linux kernel. It finds cases where a check for null
pointer is done, but is unnecessary because the function
already handles NULL pointer.

Basic example:
       if (x->buf)
           rte_free(x->buf);
can be reduced to:
       rte_free(x->buf);

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 devtools/cocci/nullfree.cocci | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 devtools/cocci/nullfree.cocci
  

Comments

Thomas Monjalon Jan. 28, 2022, 9:48 p.m. UTC | #1
24/01/2022 18:45, Stephen Hemminger:
> This script is based on the idea of the nullfree script
> in the Linux kernel. It finds cases where a check for null
> pointer is done, but is unnecessary because the function
> already handles NULL pointer.
> 
> Basic example:
>        if (x->buf)
>            rte_free(x->buf);
> can be reduced to:
>        rte_free(x->buf);
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

Good to have, thank you.
  

Patch

diff --git a/devtools/cocci/nullfree.cocci b/devtools/cocci/nullfree.cocci
new file mode 100644
index 000000000000..363b6149ac28
--- /dev/null
+++ b/devtools/cocci/nullfree.cocci
@@ -0,0 +1,33 @@ 
+//
+// Remove unnecessary NULL pointer checks before free functions
+// All these functions work like libc free which allows
+// free(NULL) as a no-op.
+//
+@@
+expression E;
+@@
+(
+- if (E != NULL) free(E);
++ free(E);
+|
+- if (E != NULL) rte_bitmap_free(E);
++ rte_bitmap_free(E);
+|
+- if (E != NULL) rte_free(E);
++ rte_free(E);
+|
+- if (E != NULL) rte_hash_free(E);
++ rte_hash_free(E);
+|
+- if (E != NULL) rte_ring_free(E);
++ rte_ring_free(E);
+|
+- if (E != NULL) rte_pktmbuf_free(E);
++ rte_pktmbuf_free(E);
+|
+- if (E != NULL) rte_mempool_free(E);
++ rte_mempool_free(E);
+|
+- if (E != NULL) rte_kvargs_free(E);
++ rte_kvargs_free(E);
+)