Skip to contentSkip to conversation

coccinelle: Add patch for replacing open-coded PTR_RET

PATCH In review Lars-Peter Clausen 2 participants 8 messages Dec 19, 2011 – Dec 23, 2011 Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 coccinelle: Add patch for replacing open-coded PTR_RET

The PTR_RET function returns the error value of its parameter if it is an ERR_PTR, otherwise it returns 0. This patch adds a semantic patch which finds open-coded instances of this check and replaces them.

---
 scripts/coccinelle/api/ptr_ret.cocci |   55 ++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100644 scripts/coccinelle/api/ptr_ret.cocci
scripts/coccinelle/api/ptr_ret.cocci new file +55 -0
new file mode 100644
index 0000000..dfd9b1f
@@ -0,0 +1,55 @@
+///
+/// Use PTR_RET function instead of open coding it
+///
+// Confidence: High
+// Options:
+//
+// Keywords: IS_ERR, PTR_ERR, PTR_RET
+// Version min: 2.6.39
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+@depends on context@
+expression x;
+@@
+*if (IS_ERR(x))
+*	return PTR_ERR(x);
+*return 0;
+
+@depends on patch@
+expression x;
+@@
+-if (IS_ERR(x))
+-	return PTR_ERR(x);
+-return 0;
++return PTR_RET(x);
+
+@r depends on org || report@
+expression x;
+position p;
+@@
+
+ if (IS_ERR@p(x))
+	return PTR_ERR(x);
+ return 0;
+
+@script:python depends on org@
+p << r.p;
+x << r.x;
+@@
+
+msg="WARNING PTR_RET can be used with %s" % (x)
+msg_safe=msg.replace("[","@(").replace("]",")")
+coccilib.org.print_todo(p[0], msg_safe)
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+@@
+
+msg="WARNING: PTR_RET can be used with %s" % (x)
+coccilib.report.print_report(p[0], msg)
Signature · 1 line
1.7.7.3
#1 lore raw

PATCH 2/2 coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

The IS_ERR_OR_NULL function returns true if the passed parameter is either a ERR_PTR or NULL. This patch adds a semantic patch which finds open-coded instances of this check and replaces them.

---
 scripts/coccinelle/api/is_err_or_null.cocci |   98 +++++++++++++++++++++++++++
 1 files changed, 98 insertions(+), 0 deletions(-)
 create mode 100644 scripts/coccinelle/api/is_err_or_null.cocci
scripts/coccinelle/api/is_err_or_null.cocci new file +98 -0
new file mode 100644
index 0000000..04afd1e
@@ -0,0 +1,98 @@
+///
+/// Use IS_ERR_OR_NULL function instead of open coding it
+///
+// Confidence: High
+// Options:
+//
+// Keywords: IS_ERR, IS_ERR_OR_NULL
+// Version min: 2.6.33
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+// Since the logical or operator by itself is not commutative we need rules for
+// both cases as well as for their De Morgan equivalents.
+
+@depends on context@
+expression x;
+@@
+*(IS_ERR(x) || !x)
+
+@depends on context@
+expression x;
+@@
+*(!x || IS_ERR(x))
+
+@depends on context@
+expression x;
+@@
+*(!IS_ERR(x) && x)
+
+@depends on context@
+expression x;
+@@
+*(x && !IS_ERR(x))
+
+@depends on patch@
+expression x;
+@@
+-(IS_ERR(x) || !x)
++IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(!x || IS_ERR(x))
++IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(!IS_ERR(x) && x)
++ !IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(x && !IS_ERR(x))
++ !IS_ERR_OR_NULL(x)
+
+@r depends on org || report@
+expression x;
+position p;
+statement S;
+@@
+// We'll probably miss some some cases with this, but the if gives us an anchor
+// and we do not have to write a indivual rule for each case. patch and context
+// mode will report all cases
+ if(@p
+(
+ IS_ERR(x) || !x
+|
+ !x || IS_ERR(x)
+|
+ !IS_ERR(x) && x
+|
+ x && !IS_ERR(x)
+)
+ ) S
+
+@script:python depends on org@
+p << r.p;
+x << r.x;
+@@
+
+msg="IS_ERR_OR_NULL can be used with %s" % (x)
+msg_safe=msg.replace("[","@(").replace("]",")")
+coccilib.org.print_todo(p[0], msg_safe)
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+@@
+
+msg="IS_ERR_OR_NULL can be used with %s" % (x)
+coccilib.report.print_report(p[0], msg)
Signature · 1 line
1.7.7.3
#2 lore raw

Re: [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

For this one, I made quite a number of changes, some of which required fixing a bug in Coccinelle... I will try to release the bug fix shortly. In general, though, I have added ... between the two tests. I also got rid of the focus on ifs in the org and report rules.

julia

virtual context
virtual patch
virtual org
virtual report

// Since the logical or operator by itself is not commutative we need rules for // both cases as well as for their De Morgan equivalents.

@depends on context@
expression x;
@@
* IS_ERR(x)
|| ... ||
* x == NULL

@depends on context@
expression x;
@@
* x == NULL
|| ... ||
* IS_ERR(x)

@depends on context@
expression x;
@@
* !IS_ERR(x)
&& ... &&
* x != NULL

@depends on context@
expression x;
@@
* x != NULL
&& ... &&
* !IS_ERR(x)

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

  || ...
- || x == NULL

@depends on patch@
expression x;
@@
- x == NULL
+ IS_ERR_OR_NULL(x)

  || ...
- || IS_ERR(x)

@depends on patch@
expression x;
@@
- !IS_ERR(x)
+ !IS_ERR_OR_NULL(x)
&& ...
- && x != NULL

@depends on patch@
expression x;
@@
- x != NULL
+ !IS_ERR_OR_NULL(x)
&& ...
- && !IS_ERR(x)

@r depends on org || report@
expression x;
position p;
@@
(
IS_ERR@p(x) || ... || x == NULL
|
x == NULL || ... || IS_ERR@p(x)
|
!IS_ERR@p(x) && ... && x != NULL
|
x != NULL && ... && !IS_ERR@p(x)
)

@script:python depends on org@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)

@script:python depends on report@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
coccilib.report.print_report(p[0], msg)

#3 lore raw

Re: [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

Quoted 5 lines · Julia Lawall

For this one, I made quite a number of changes, some of which required fixing a bug in Coccinelle... I will try to release the bug fix shortly. In general, though, I have added ... between the two tests. I also got rid of the focus on ifs in the org and report rules.

Ok, thanks. Was the removing of the ifs in related to the Coccinelle bug? I always get an error if I start a match with a conditional.

Quoted 101 lines

julia

virtual context
virtual patch
virtual org
virtual report

// Since the logical or operator by itself is not commutative we need rules for
// both cases as well as for their De Morgan equivalents.

@depends on context@
expression x;
@@
* IS_ERR(x)
|| ... ||
* x == NULL

@depends on context@
expression x;
@@
* x == NULL
|| ... ||
* IS_ERR(x)

@depends on context@
expression x;
@@
* !IS_ERR(x)
&& ... &&
* x != NULL

@depends on context@
expression x;
@@
* x != NULL
&& ... &&
* !IS_ERR(x)

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

  || ...
- || x == NULL

@depends on patch@
expression x;
@@
- x == NULL
+ IS_ERR_OR_NULL(x)

  || ...
- || IS_ERR(x)

@depends on patch@
expression x;
@@
- !IS_ERR(x)
+ !IS_ERR_OR_NULL(x)
&& ...
- && x != NULL

@depends on patch@
expression x;
@@
- x != NULL
+ !IS_ERR_OR_NULL(x)
&& ...
- && !IS_ERR(x)

@r depends on org || report@
expression x;
position p;
@@
(
IS_ERR@p(x) || ... || x == NULL
|
x == NULL || ... || IS_ERR@p(x)
|
!IS_ERR@p(x) && ... && x != NULL
|
x != NULL && ... && !IS_ERR@p(x)
)

@script:python depends on org@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)

@script:python depends on report@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
coccilib.report.print_report(p[0], msg)

#4 lore raw

Re: [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

Quoted 9 lines · Lars-Peter Clausen
Quoted 5 lines · Julia Lawall

For this one, I made quite a number of changes, some of which required fixing a bug in Coccinelle... I will try to release the bug fix shortly. In general, though, I have added ... between the two tests. I also got rid of the focus on ifs in the org and report rules.

Ok, thanks. Was the removing of the ifs in related to the Coccinelle bug? I always get an error if I start a match with a conditional.

Not at all. The bug was that a patterm like:

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

   || ...
 - || x == NULL

thought that it should be applied in two ways, giving an error about a double transformation of the IS_ERR call.

Could you reproduce the example that caused problems?

thanks,
julia

Quoted 103 lines
Quoted 101 lines

julia

virtual context
virtual patch
virtual org
virtual report

// Since the logical or operator by itself is not commutative we need rules for
// both cases as well as for their De Morgan equivalents.

@depends on context@
expression x;
@@
* IS_ERR(x)
|| ... ||
* x == NULL

@depends on context@
expression x;
@@
* x == NULL
|| ... ||
* IS_ERR(x)

@depends on context@
expression x;
@@
* !IS_ERR(x)
&& ... &&
* x != NULL

@depends on context@
expression x;
@@
* x != NULL
&& ... &&
* !IS_ERR(x)

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

  || ...
- || x == NULL

@depends on patch@
expression x;
@@
- x == NULL
+ IS_ERR_OR_NULL(x)

  || ...
- || IS_ERR(x)

@depends on patch@
expression x;
@@
- !IS_ERR(x)
+ !IS_ERR_OR_NULL(x)
&& ...
- && x != NULL

@depends on patch@
expression x;
@@
- x != NULL
+ !IS_ERR_OR_NULL(x)
&& ...
- && !IS_ERR(x)

@r depends on org || report@
expression x;
position p;
@@
(
IS_ERR@p(x) || ... || x == NULL
|
x == NULL || ... || IS_ERR@p(x)
|
!IS_ERR@p(x) && ... && x != NULL
|
x != NULL && ... && !IS_ERR@p(x)
)

@script:python depends on org@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
msg_safe=msg.replace("[","@(").replace("]",")")
coccilib.org.print_todo(p[0], msg_safe)

@script:python depends on report@
p << r.p;
x << r.x;
@@

msg="IS_ERR_OR_NULL can be used with %s" % (x)
coccilib.report.print_report(p[0], msg)

#5 lore raw

Re: [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

Quoted 20 lines · Julia Lawall
Quoted 10 lines · Lars-Peter Clausen
Quoted 5 lines · Julia Lawall

For this one, I made quite a number of changes, some of which required fixing a bug in Coccinelle... I will try to release the bug fix shortly. In general, though, I have added ... between the two tests. I also got rid of the focus on ifs in the org and report rules.

Ok, thanks. Was the removing of the ifs in related to the Coccinelle bug? I
always get an error if I start a match with a conditional.

Not at all. The bug was that a patterm like:

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

Quoted 7 lines
   || ...
 - || x == NULL

thought that it should be applied in two ways, giving an error about a double transformation of the IS_ERR call.

Could you reproduce the example that caused problems?

Hm, I can reproduce it anymore, even with my original patch, must have been caused by some other error I had in the semantic patch at that time.

- Lars

#6 lore raw

Re: [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL

Quoted 24 lines · Lars-Peter Clausen
Quoted 23 lines · Julia Lawall
Quoted 10 lines · Lars-Peter Clausen
Quoted 5 lines · Julia Lawall

For this one, I made quite a number of changes, some of which required fixing a bug in Coccinelle... I will try to release the bug fix shortly. In general, though, I have added ... between the two tests. I also got rid of the focus on ifs in the org and report rules.

Ok, thanks. Was the removing of the ifs in related to the Coccinelle bug? I
always get an error if I start a match with a conditional.

Not at all. The bug was that a patterm like:

@depends on patch@
expression x;
@@
- IS_ERR(x)
+ IS_ERR_OR_NULL(x)

   || ...
 - || x == NULL
Quoted 7 lines
Quoted 4 lines

thought that it should be applied in two ways, giving an error about a double transformation of the IS_ERR call.

Could you reproduce the example that caused problems?

Hm, I can reproduce it anymore, even with my original patch, must have been caused by some other error I had in the semantic patch at that time.

OK, should do you want to resubmit the semantic patch that I sent you? The version of Coccinelle you have does not perfectly support it, but it should just cause failures on a few files. The problem is only with the patch case, when there is the possibility to mak two calls to IS_ERR_OR_NULL in a single test expression, which doesn't happen very often.

julia

Acked-by on v1 1/2
#7 lore raw
In reply to Lars-Peter Clausen
Quoted 5 lines · Lars-Peter Clausen

The PTR_RET function returns the error value of its parameter if it is an ERR_PTR, otherwise it returns 0. This patch adds a semantic patch which finds open-coded instances of this check and replaces them.

Quoted 76 lines

---
scripts/coccinelle/api/ptr_ret.cocci | 55 ++++++++++++++++++++++++++++++++++

1 files changed, 55 insertions(+), 0 deletions(-)

create mode 100644 scripts/coccinelle/api/ptr_ret.cocci

scripts/coccinelle/api/ptr_ret.cocci new file +55 -0
new file mode 100644
index 0000000..dfd9b1f
@@ -0,0 +1,55 @@
+///
+/// Use PTR_RET function instead of open coding it
+///
+// Confidence: High
+// Options:
+//
+// Keywords: IS_ERR, PTR_ERR, PTR_RET
+// Version min: 2.6.39
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+@depends on context@
+expression x;
+@@
+*if (IS_ERR(x))
+*	return PTR_ERR(x);
+*return 0;
+
+@depends on patch@
+expression x;
+@@
+-if (IS_ERR(x))
+-	return PTR_ERR(x);
+-return 0;
++return PTR_RET(x);
+
+@r depends on org || report@
+expression x;
+position p;
+@@
+
+ if (IS_ERR@p(x))
+	return PTR_ERR(x);
+ return 0;
+
+@script:python depends on org@
+p << r.p;
+x << r.x;
+@@
+
+msg="WARNING PTR_RET can be used with %s" % (x)
+msg_safe=msg.replace("[","@(").replace("]",")")
+coccilib.org.print_todo(p[0], msg_safe)
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+@@
+
+msg="WARNING: PTR_RET can be used with %s" % (x)
+coccilib.report.print_report(p[0], msg)
Signature · 8 lines
1.7.7.3


_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)