Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 ///Find conditions where if and else branch are functionally
0003 // identical.
0004 //
0005 // There can be false positives in cases where the positional
0006 // information is used (as with lockdep) or where the identity
0007 // is a placeholder for not yet handled cases.
0008 // Unfortunately there also seems to be a tendency to use
0009 // the last if else/else as a "default behavior" - which some
0010 // might consider a legitimate coding pattern. From discussion
0011 // on kernelnewbies though it seems that this is not really an
0012 // accepted pattern and if at all it would need to be commented
0013 //
0014 // In the Linux kernel it does not seem to actually report
0015 // false positives except for those that were documented as
0016 // being intentional.
0017 // the two known cases are:
0018 //   arch/sh/kernel/traps_64.c:read_opcode()
0019 //        } else if ((pc & 1) == 0) {
0020 //              /* SHcompact */
0021 //              /* TODO : provide handling for this.  We don't really support
0022 //                 user-mode SHcompact yet, and for a kernel fault, this would
0023 //                 have to come from a module built for SHcompact.  */
0024 //              return -EFAULT;
0025 //      } else {
0026 //              /* misaligned */
0027 //              return -EFAULT;
0028 //      }
0029 //   fs/kernfs/file.c:kernfs_fop_open()
0030 //       * Both paths of the branch look the same.  They're supposed to
0031 //       * look that way and give @of->mutex different static lockdep keys.
0032 //       */
0033 //      if (has_mmap)
0034 //              mutex_init(&of->mutex);
0035 //      else
0036 //              mutex_init(&of->mutex);
0037 //
0038 // All other cases look like bugs or at least lack of documentation
0039 //
0040 // Confidence: Moderate
0041 // Copyright: (C) 2016 Nicholas Mc Guire, OSADL.
0042 // Comments:
0043 // Options: --no-includes --include-headers
0044 
0045 virtual org
0046 virtual report
0047 
0048 @cond@
0049 statement S1;
0050 position p;
0051 @@
0052 
0053 * if@p (...) S1 else S1
0054 
0055 @script:python depends on org@
0056 p << cond.p;
0057 @@
0058 
0059 cocci.print_main("WARNING: possible condition with no effect (if == else)",p)
0060 
0061 @script:python depends on report@
0062 p << cond.p;
0063 @@
0064 
0065 coccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)")