Skip to contentSkip to conversation

Matching and replacing array types in sizeof()

cocci Mats Kindahl 2 participants 2 messages Jan 3, 2025

lore
Share & tools
lore
#0 lore raw

Hi again,

Here is yet another problem I ran into.

I would like to replace malloc() of an array with calloc() instead, but I cannot match the array type in this code:

   #include <stdlib.h>
   #include <string.h>

   int main() {
      int* mem = malloc(sizeof(int[2]));
      memset(mem, 1, sizeof(int[2]));
   }

I tried with this semantic patch:

@@
type T;
constant C;
@@
- malloc(sizeof(T[C]))
+ calloc(C, sizeof(T))

But it gives a parse error:

   mats@abzu:~/lang/cocci/tests$ spatch --debug --cocci-file
   array_type.cocci code
   init_defs_builtins: /usr/lib/coccinelle/standard.h
   -----------------------------------------------------------------------
   processing semantic patch file: array_type.cocci
   with isos from: /usr/lib/coccinelle/standard.iso
   -----------------------------------------------------------------------
   @@
   type T;
   constant C;
   @@
   - malloc(sizeof(T[C]))
   + calloc(C, sizeof(T))

minus: parse error:
 File "array_type.cocci", line 5, column 17, charpos = 43 around = '[',
 whole content = - malloc(sizeof(T[C]))

I have also tried replacing the "T[C]" with "(T)[C]" and even tried just matching "T[...]" and "(T)[...]" in the hope that the parser is just not able to handle the type, no no avail.

This parses fine in a normal compiler, so I suspect the Cocci parser is missing this case (looking at the code I see nothing similar to the direct-abstract-declarator from the C99 standard), so are there any alternative way to deal with this?

Best wishes,
Mats Kindahl

#1 lore raw
Quoted 44 lines · Mats Kindahl

Hi again,

Here is yet another problem I ran into.

I would like to replace malloc() of an array with calloc() instead, but I cannot match the array type in this code:

      #include <stdlib.h>
      #include <string.h>

      int main() {
        int* mem = malloc(sizeof(int[2]));
        memset(mem, 1, sizeof(int[2]));
      }

I tried with this semantic patch:

      @@
      type T;
      constant C;
      @@
      - malloc(sizeof(T[C]))
      + calloc(C, sizeof(T))

But it gives a parse error:

      mats@abzu:~/lang/cocci/tests$ spatch --debug --cocci-file array_type.cocci code
      init_defs_builtins: /usr/lib/coccinelle/standard.h
      -----------------------------------------------------------------------
      processing semantic patch file: array_type.cocci
      with isos from: /usr/lib/coccinelle/standard.iso
      -----------------------------------------------------------------------
      @@
      type T;
      constant C;
      @@
      - malloc(sizeof(T[C]))
      + calloc(C, sizeof(T))

minus: parse error:
 File "array_type.cocci", line 5, column 17, charpos = 43 around = '[',
 whole content = - malloc(sizeof(T[C]))

Quoted 4 lines

I have also tried replacing the "T[C]" with "(T)[C]" and even tried just matching "T[...]" and "(T)[...]" in the hope that the parser is just not able to handle the type, no no avail.

This parses fine in a normal compiler, so I suspect the Cocci parser is missing this case (looking at the code I see nothing similar to the direct-abstract-declarator from the C99 standard), so are there any alternative way to deal with this?

It's indeed quite possible that this case is not handled. In the short term, you can use python code.

@r@
type t : script:python() { ... };
@@

malloc(sizeof(t))

@script:python s@
t << r.t;
@@

coccinelle.ty = ...
coccinelle.sz = ...

@@
type r.t;
type s.ty;
expression s.sz;
@@

- malloc(sizeof(t))
+ calloc(C, sizeof(T))

The first ... should be python code to recognize the pattern of T[C]. The ones in the python script should extract the T and C from that code.

You would have to check to the extent that you want that T is a type and C is a constant.

It's a bit clunky, but it eliminates the need to wait for the fix. I will try to fix it soon, though.

julia