matching variations of function result check
I am trying to write a cocci script as below:
@@
identifier NDEV;
int RES;
@@
RES = register_netdevice(NDEV);
if (RES) {
<...
- free_netdev(NDEV);
+ foo(NDEV);
+ free_netdev(NDEV);
...>}
However, I want to catch more variations, ie.
if (RES < 0) if (RES != 0) if (register_netdevice(NDEV) < 0)
and so on. To boot the function call may either be register_netdevice or register_netdev.
Now I can create rules for all variations, but I was wondering if I could do things smarter. The grammar documentation [1] always confuses me mixing it all with C grammar.
Regards,
Arend
Hi Arend,
Check these examples of using '... when':
https://github.com/coccinelle/coccinellery/search?utf8=%E2%9C%93&q=when&type=
I guess this can be helpful
Quoted 35 lines · Arend van Spriel
I am trying to write a cocci script as below:
@@
identifier NDEV;
int RES;
@@RES =
register_netdevice(NDEV);if (RES) { <...(quoted diff) +2
-1- free_netdev(NDEV); + foo(NDEV); + free_netdev(NDEV); ...> }However, I want to catch more variations, ie.
if (RES < 0) if (RES != 0) if (register_netdevice(NDEV) < 0)and so on. To boot the function call may either be
register_netdeviceorregister_netdev.Now I can create rules for all variations, but I was wondering if I could do things smarter. The grammar documentation [1] always confuses me mixing it all with C grammar.
Regards,
Arend
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
Signature · 1 line
Peter
Quoted 4 lines · Peter Senna Tschudin
Hi Arend,
Check these examples of using '... when':
Quoted 21 lines
https://github.com/coccinelle/coccinellery/search?utf8=%E2%9C%93&q=when&type=
I guess this can be helpful
Quoted 13 lines · Arend van Spriel
I am trying to write a cocci script as below:
@@
identifier NDEV;
int RES;
@@RES =
register_netdevice(NDEV);if (RES) { <...(quoted diff) +2
-1- free_netdev(NDEV); + foo(NDEV); + free_netdev(NDEV);
You can just put +foo(NDEV) in front of the existing call to free_netdev, rather that removing the call to free_netdev and adding it back.
Quoted 7 lines
Quoted 7 lines
...>}
However, I want to catch more variations, ie.
if (RES < 0) if (RES != 0)
Perhaps you don't care much about how RES is tested? You can try
if (<+...RES...+>)
which will match a case with any reference to RES in the if test. This could give false positives, if RES is used in some other way, but it could be worth trying.
Quoted 1 line
Quoted 1 line
if (
register_netdevice(NDEV)< 0)
For this, you would need a separate pattern. Coccinelle doesn't do any dataflow analysis that would detect that naming the result of the call and then testing the name is the same as simply tsting the call.
Quoted 3 lines
Quoted 3 lines
and so on. To boot the function call may either be
register_netdeviceorregister_netdev.
You can put a disjunction: \(fn1\|fn2\). Another option is to make an identifier metavariable with an appropriate regular expression, but this would be less efficient than the disjuction option. Coccinelle does some optimizations based on what is the pattern matching code, but not regular expression constraints.
julia
Quoted 21 lines
Quoted 11 lines
Now I can create rules for all variations, but I was wondering if I could do things smarter. The grammar documentation [1] always confuses me mixing it all with C grammar.
Regards,
Arend
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci--
Peter
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
Quoted 30 lines · Julia Lawall
Quoted 4 lines · Peter Senna Tschudin
Hi Arend,
Check these examples of using '... when':
Quoted 21 lines
https://github.com/coccinelle/coccinellery/search?utf8=%E2%9C%93&q=when&type=
I guess this can be helpful
Quoted 13 lines · Arend van Spriel
I am trying to write a cocci script as below:
@@
identifier NDEV;
int RES;
@@RES = register_netdevice(NDEV); if (RES) { <...(quoted diff) +2
-1- free_netdev(NDEV); + foo(NDEV); + free_netdev(NDEV);
Quoted 2 lines
You can just put +foo(NDEV) in front of the existing call to
free_netdev, rather that removing the call tofree_netdevand adding it back.
I tried that, but spatch complaint so I tried this and it worked. Not sure what spatch version I used as I was working on another machine.
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
...>}
However, I want to catch more variations, ie.
if (RES < 0) if (RES != 0)
Quoted 7 lines
Perhaps you don't care much about how RES is tested? You can try
if (<+...RES...+>)
which will match a case with any reference to RES in the if test. This could give false positives, if RES is used in some other way, but it could be worth trying.
Assuming the if statement after calling register_netdevice handles the error path, which is probably true if it contains the free_netdev() call. So yeah, it is worth trying. Thanks for the suggestion.
Quoted 5 lines
Quoted 1 line
Quoted 1 line
if (
register_netdevice(NDEV)< 0)For this, you would need a separate pattern. Coccinelle doesn't do any dataflow analysis that would detect that naming the result of the call and then testing the name is the same as simply tsting the call.
Too bad. Still a great tool ;-)
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
and so on. To boot the function call may either be
register_netdeviceorregister_netdev.You can put a disjunction: \(fn1\|fn2\). Another option is to make an identifier metavariable with an appropriate regular expression, but this would be less efficient than the disjuction option. Coccinelle does some optimizations based on what is the pattern matching code, but not regular expression constraints.
Will try that.
Thanks,
Arend