On Thu, Aug 23, 2018 at 3:21 PM, Joe Perches <j...@perches.com> wrote:
> On Thu, 2018-08-23 at 18:13 -0400, Julia Lawall wrote:
>>
>> On Thu, 23 Aug 2018, Kees Cook wrote:
>>
>> (a + b) * c
>>
>> It will consider a pattern with the parentheses removed, but that pattern
>> won't match anything, because real trees that consist of a + b * c are
>> parsed in a different way.
>
> spatch 1.0.4 doesn't seem to:
>
> $ spatch --version
> spatch version 1.0.4 with Python support and with PCRE support
> $ cat match_mul.cocci
> @@
> expression a, b, c;
> int d;
> @@
>
> *       d = a * b + c;

But "(a * b) + c" for the rule does:


$ cat isomorphisms.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        int a, b, c;

        a = atoi(argv[1]);
        b = atoi(argv[2]);
        c = atoi(argv[3]);

        printf("%d\n", a + b * c);
        printf("%d\n", (a + b) * c);
        printf("%d\n", a + (b * c));
        printf("%d\n", b * c + a);
        printf("%d\n", b * (c + a));
        printf("%d\n", (b * c) + a);

        return 0;
}
$ cat match.cocci
@@
identifier A, B, C;
@@

* (A * B) + C

$ spatch -sp-file match.cocci isomorphisms.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: isomorphisms.c
diff =
--- isomorphisms.c
+++ /tmp/cocci-output-8402-870fd6-isomorphisms.c
@@ -9,12 +9,8 @@ int main(int argc, char *argv[])
        b = atoi(argv[2]);
        c = atoi(argv[3]);

-       printf("%d\n", a + b * c);
        printf("%d\n", (a + b) * c);
-       printf("%d\n", a + (b * c));
-       printf("%d\n", b * c + a);
        printf("%d\n", b * (c + a));
-       printf("%d\n", (b * c) + a);

        return 0;
 }


So it sounds like I should put parens around all kinds of things in my rules...

-Kees

-- 
Kees Cook
Pixel Security
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to