Linux Audio

Check our new training course

Loading...
v4.17
 
 1///Find conditions where if and else branch are functionally
 2// identical.
 3//
 4// There can be false positives in cases where the positional
 5// information is used (as with lockdep) or where the identity
 6// is a placeholder for not yet handled cases.
 7// Unfortunately there also seems to be a tendency to use
 8// the last if else/else as a "default behavior" - which some
 9// might consider a legitimate coding pattern. From discussion
10// on kernelnewbies though it seems that this is not really an
11// accepted pattern and if at all it would need to be commented
12//
13// In the Linux kernel it does not seem to actually report
14// false positives except for those that were documented as
15// being intentional.
16// the two known cases are:
17//   arch/sh/kernel/traps_64.c:read_opcode()
18//        } else if ((pc & 1) == 0) {
19//              /* SHcompact */
20//              /* TODO : provide handling for this.  We don't really support
21//                 user-mode SHcompact yet, and for a kernel fault, this would
22//                 have to come from a module built for SHcompact.  */
23//              return -EFAULT;
24//      } else {
25//              /* misaligned */
26//              return -EFAULT;
27//      }
28//   fs/kernfs/file.c:kernfs_fop_open()
29//       * Both paths of the branch look the same.  They're supposed to
30//       * look that way and give @of->mutex different static lockdep keys.
31//       */
32//      if (has_mmap)
33//              mutex_init(&of->mutex);
34//      else
35//              mutex_init(&of->mutex);
36//
37// All other cases look like bugs or at least lack of documentation
38//
39// Confidence: Moderate
40// Copyright: (C) 2016 Nicholas Mc Guire, OSADL.  GPLv2.
41// Comments:
42// Options: --no-includes --include-headers
43
44virtual org
45virtual report
46
47@cond@
48statement S1;
49position p;
50@@
51
52* if@p (...) S1 else S1
53
54@script:python depends on org@
55p << cond.p;
56@@
57
58cocci.print_main("WARNING: possible condition with no effect (if == else)",p)
59
60@script:python depends on report@
61p << cond.p;
62@@
63
64coccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)")
v6.8
 1// SPDX-License-Identifier: GPL-2.0-only
 2///Find conditions where if and else branch are functionally
 3// identical.
 4//
 5// There can be false positives in cases where the positional
 6// information is used (as with lockdep) or where the identity
 7// is a placeholder for not yet handled cases.
 8// Unfortunately there also seems to be a tendency to use
 9// the last if else/else as a "default behavior" - which some
10// might consider a legitimate coding pattern. From discussion
11// on kernelnewbies though it seems that this is not really an
12// accepted pattern and if at all it would need to be commented
13//
14// In the Linux kernel it does not seem to actually report
15// false positives except for those that were documented as
16// being intentional.
17// the two known cases are:
18//   arch/sh/kernel/traps_64.c:read_opcode()
19//        } else if ((pc & 1) == 0) {
20//              /* SHcompact */
21//              /* TODO : provide handling for this.  We don't really support
22//                 user-mode SHcompact yet, and for a kernel fault, this would
23//                 have to come from a module built for SHcompact.  */
24//              return -EFAULT;
25//      } else {
26//              /* misaligned */
27//              return -EFAULT;
28//      }
29//   fs/kernfs/file.c:kernfs_fop_open()
30//       * Both paths of the branch look the same.  They're supposed to
31//       * look that way and give @of->mutex different static lockdep keys.
32//       */
33//      if (has_mmap)
34//              mutex_init(&of->mutex);
35//      else
36//              mutex_init(&of->mutex);
37//
38// All other cases look like bugs or at least lack of documentation
39//
40// Confidence: Moderate
41// Copyright: (C) 2016 Nicholas Mc Guire, OSADL.
42// Comments:
43// Options: --no-includes --include-headers
44
45virtual org
46virtual report
47
48@cond@
49statement S1;
50position p;
51@@
52
53* if@p (...) S1 else S1
54
55@script:python depends on org@
56p << cond.p;
57@@
58
59cocci.print_main("WARNING: possible condition with no effect (if == else)",p)
60
61@script:python depends on report@
62p << cond.p;
63@@
64
65coccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)")