Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 1 | /// Many iterators have the property that the first argument is always bound |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 2 | /// to a real list element, never NULL. |
| 3 | //# False positives arise for some iterators that do not have this property, |
| 4 | //# or in cases when the loop cursor is reassigned. The latter should only |
| 5 | //# happen when the matched code is on the way to a loop exit (break, goto, |
| 6 | //# or return). |
Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 7 | /// |
| 8 | // Confidence: Moderate |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 9 | // Copyright: (C) 2010-2012 Nicolas Palix. GPLv2. |
| 10 | // Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2. |
| 11 | // Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2. |
Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 12 | // URL: http://coccinelle.lip6.fr/ |
| 13 | // Comments: |
Nicolas Palix | 93f1446 | 2013-06-20 13:10:56 +0200 | [diff] [blame] | 14 | // Options: --no-includes --include-headers |
Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 15 | |
| 16 | virtual patch |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 17 | virtual context |
| 18 | virtual org |
| 19 | virtual report |
Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 20 | |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 21 | @depends on patch@ |
Nicolas Palix | afc48a8 | 2010-08-24 17:39:02 +0200 | [diff] [blame] | 22 | iterator I; |
| 23 | expression x,E,E1,E2; |
| 24 | statement S,S1,S2; |
| 25 | @@ |
| 26 | |
| 27 | I(x,...) { <... |
| 28 | ( |
| 29 | - if (x == NULL && ...) S |
| 30 | | |
| 31 | - if (x != NULL || ...) |
| 32 | S |
| 33 | | |
| 34 | - (x == NULL) || |
| 35 | E |
| 36 | | |
| 37 | - (x != NULL) && |
| 38 | E |
| 39 | | |
| 40 | - (x == NULL && ...) ? E1 : |
| 41 | E2 |
| 42 | | |
| 43 | - (x != NULL || ...) ? |
| 44 | E1 |
| 45 | - : E2 |
| 46 | | |
| 47 | - if (x == NULL && ...) S1 else |
| 48 | S2 |
| 49 | | |
| 50 | - if (x != NULL || ...) |
| 51 | S1 |
| 52 | - else S2 |
| 53 | | |
| 54 | + BAD( |
| 55 | x == NULL |
| 56 | + ) |
| 57 | | |
| 58 | + BAD( |
| 59 | x != NULL |
| 60 | + ) |
| 61 | ) |
Julia Lawall | 29a36d4 | 2012-01-14 23:41:54 +0100 | [diff] [blame] | 62 | ...> } |
| 63 | |
| 64 | @r depends on !patch exists@ |
| 65 | iterator I; |
| 66 | expression x,E; |
| 67 | position p1,p2; |
| 68 | @@ |
| 69 | |
| 70 | *I@p1(x,...) |
| 71 | { ... when != x = E |
| 72 | ( |
| 73 | * x@p2 == NULL |
| 74 | | |
| 75 | * x@p2 != NULL |
| 76 | ) |
| 77 | ... when any |
| 78 | } |
| 79 | |
| 80 | @script:python depends on org@ |
| 81 | p1 << r.p1; |
| 82 | p2 << r.p2; |
| 83 | @@ |
| 84 | |
| 85 | cocci.print_main("iterator-bound variable",p1) |
| 86 | cocci.print_secs("useless NULL test",p2) |
| 87 | |
| 88 | @script:python depends on report@ |
| 89 | p1 << r.p1; |
| 90 | p2 << r.p2; |
| 91 | @@ |
| 92 | |
| 93 | msg = "ERROR: iterator variable bound on line %s cannot be NULL" % (p1[0].line) |
| 94 | coccilib.report.print_report(p2[0], msg) |