1

I want to change my code using a Coccinelle script:

// Before modification

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int *p;

    *p=i;

    return 0;
}

The expected result is:

// After modification

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int *p;

    if (p!=NULL)
        *p=i;

    return 0;
}

My Coccinelle script is as follows:

@rule1@
type T;
T* ptr;
expression E;
@@

-*ptr= E;
+ if (ptr!=NULL)
+ptr=E;

My script is wrong because the "star" operator is used by Coccinelle. Could anyone help me to know for what the "star" operator is useful because I am a little bit confused about this operator. How can I modify my script to get the expected result?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
fedi
  • 368
  • 3
  • 7
  • 18
  • The C code you presented is just an example right? Since you are reading uninitialized variables. – this Aug 07 '15 at 16:50
  • Your example C program would crash (if you're lucky). You're attempting to write to unallocated memory. – Filipe Gonçalves Aug 07 '15 at 16:52
  • @FilipeGonçalves: And the modified version reads an uninitialized variable. – EOF Aug 07 '15 at 16:53
  • you mean `p = &i` instead of `*p = i ?` it is referred to as `pointer` a c programming book can help you. – Soner from The Ottoman Empire Aug 07 '15 at 16:53
  • @itsnotmyrealname See, and it even solves your star problems. :-) – this Aug 07 '15 at 16:56
  • Yes it could be another problem to check ,but the problem is not here the problem consists in pattern matching a line that contains "star operator" – fedi Aug 07 '15 at 17:11
  • I tried to escape * by a backslash \* but still not working i got this error Fatal error: exception Lexer_cocci.Lexical ("unrecognised symbol, in token rule:\") – fedi Aug 10 '15 at 07:48

2 Answers2

2

Put a space before the *.

Coccinelle uses * to indicate that a match is wanted, rather than a transformation. The * indicates a line that should be highlighted in the result.

0

I suppose that the * operator is used by cocinelle for pattern matching, x* would match several occurrences of x in a row.

I would try to escape the * with a backslash \*.

Also, the * is missing from your replacement pattern, I think.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • In the Coccinelle script above i escaped * with backslash \* but i got an error :Fatal error: exception Lexer_cocci.Lexical ("unrecognised symbol, in token rule:\") – fedi Aug 10 '15 at 07:46