[Cocci] Remove unnecessary null pointer checks?

SF Markus Elfring elfring at users.sourceforge.net
Wed Feb 26 12:30:55 CET 2014


> But it is not surprising that a regular expression with over 5000 options 
> exceeds its capacity.

A source code search with the following pattern found 893 functions which check
their single parameter.

@safety_check@
identifier work, input;
type data_type;
position pos;
statement is, es;
@@
 void work at pos(data_type input)
 {
  ... when any
( if (input) is else es
| if (likely(input)) is else es
)
  ... when any
 }


Such a name list could be integrated into the following pattern variant.

@Delete_unnecessary_checks@
expression x;
identifier release =~ "^(?x)
(?:
   (?:kz?|slob_)free
|
   (?:
      # alternation/disjunction of 893 strings ...
   )
)$";
@@
-if (x)
    release(x);


I get an interesting result for example. Do you find it useful?

elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt --sp-file
delete_unnecessary_checks3.cocci /usr/src/linux-stable/fs/btrfs/inode.c
init_defs_builtins: /usr/local/share/coccinelle/standard.h
HANDLING: /usr/src/linux-stable/fs/btrfs/inode.c
diff =
--- /usr/src/linux-stable/fs/btrfs/inode.c
+++ /tmp/cocci-output-5030-250c49-inode.c
@@ -819,8 +819,7 @@ static u64 get_extent_allocation_hint(st
                        em = search_extent_mapping(em_tree, 0, 0);
                        if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
                                alloc_hint = em->block_start;
-                       if (em)
-                               free_extent_map(em);
+                       free_extent_map(em);
                } else {
                        alloc_hint = em->block_start;
                        free_extent_map(em);
@@ -5137,8 +5136,7 @@ static int btrfs_dentry_delete(const str

 static void btrfs_dentry_release(struct dentry *dentry)
 {
-       if (dentry->d_fsdata)
-               kfree(dentry->d_fsdata);
+       kfree(dentry->d_fsdata);
 }

 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
@@ -6362,8 +6360,7 @@ out:

        trace_btrfs_get_extent(root, em);

-       if (path)
-               btrfs_free_path(path);
+       btrfs_free_path(path);
        if (trans) {
                ret = btrfs_end_transaction(trans, root);
                if (!err)


Does the following pattern variant show also around 70 update candidates on your
software development system?

@is_unnecessary_check@
expression data;
identifier work;
identifier release =~ "^(?x)
(?:
   # Big regular expression for a metavariable constraint ...
)$";
position pos;
type t;
@@
 t work at pos(...)
 {
  ... when any
( if (data) release(data);
| if (likely(data)) release(data);
)
  ... when any
 }

elfring at Sonne:~/Projekte/Coccinelle/janitor> date && spatch.opt --sp-file
list_functions_with_unnecessary_checks1.cocci --dir /usr/src/linux-stable >
list_functions_with_unnecessary_checks1.txt 2>
list_functions_with_unnecessary_checks1-errors.txt && date
Mi 26. Feb 11:38:14 CET 2014
Mi 26. Feb 12:13:47 CET 2014


The log file contains messages like 'EXN:Invalid_argument("equal: abstract
value")'. Do they need further considerations?


Is this intermediate source code analysis result good enough to expand the
constructive discussion to a mailing list like "kernel janitors"?

Regards,
Markus


More information about the Cocci mailing list