[Cocci] spatch misbehavior?

Julia Lawall julia at diku.dk
Tue Jun 2 10:05:34 CEST 2009


I guess you expected to get: void f() { if (1) free(p); } as the output?

The problem is in the position of + free(E)
Coccinelle is not supposed to accept + code that is attached to a ( | )
The strange thing is that it doesn't complain, and if you look at the 
semantic patch with -parse_cocci, it looks like it has distributed the + 
code to the right place.  But for some reason it doesn't work in the end, 
so you are better off with the following rule:

@r@
expression E;
@@
(
- if (E != NULL) free(E);
+ free(E);
|
- if (E != NULL) { free(E); }
+ free(E);
)

I will try to cause the original rule to work or fail in a more coherent 
way.

Actually, you would be fine with the rule:

@r@
expression E;
@@
- if (E != NULL) { free(E); }
+ free(E);

because by the braces4 isomorphism it will match a case where the braces 
are not there

julia


On Tue, 2 Jun 2009, Jim Meyering wrote:

> Hello,
> 
> First, thank you for a very useful tool!
> 
> When I run this script, it prints the following output:
> 
> $ cat README-cocci-oddity
> #!/bin/bash
> cat > i <<\EOF
> void f() { if (1) if (p) free (p); }
> EOF
> 
> cat > x.cocci <<\EOF
> @@
> expression E;
> @@
> (
> - if (E != NULL) free(E);
> |
> - if (E != NULL) { free(E); }
> )
> + free(E);
> EOF
> 
> cat > co.iso <<\EOF
> Expression
> @ isnt_zero @
> expression X;
> @@
> X != NULL <=> NULL != X => X
> EOF
> 
> spatch -sp_file x.cocci -o /dev/null -iso_file co.iso i
> $ bash README-cocci-oddity
> init_defs_builtins: /usr/share/coccinelle/standard.h
> HANDLING: i
> diff =
> --- i   2009-06-02 09:27:49.895213940 +0200
> +++ /t/jt876.U28O9c/cocci-output-4838-7ae765-i  2009-06-02 09:27:50.025339618 +0200
> @@ -1 +1,2 @@
> -void f() { if (1) if (p) free (p); }
> +void f() { if (1) {
> +} }
> 
> 
> I expected it to remove the unnecessary "if-before-free" test, like this:
> 
>   void f() { if (1) free (p); }
> 
> Instead, it inserts an empty "{}" statement where I wanted the
> "free(p)".
> 
> FYI, I'm using spatch version: 0.1.8 on fedora rawhide.
> 
> Jim
> 
> P.S., The above arose as I am learning about spatch and attempting
> to test those two simple rules to transform C code, removing
> all useless-if-before-free tests.  I have already proposed
> a patch that does that to the linux kernel:
> 
>   http://meyering.net/code/remove-useless-if-before-kfree.patch
> 
> but it was done using a relatively contorted perl-based tool.
> _______________________________________________
> Cocci mailing list
> Cocci at diku.dk
> http://lists.diku.dk/mailman/listinfo/cocci
> (Web access from inside DIKUs LAN only)
> 


More information about the Cocci mailing list