Skip to contentSkip to conversation

Match and print size of allocation

cocci Yudi Yang 2 participants 6 messages Apr 15, 2023 – Apr 18, 2023 1 message not in the archive

lore
Share & tools
lore

Yudi Yang

author
#0 lore raw

Hello Julia,

Thank you for your answer! I have modified my code:

@@
expression E, SIZE, N;
statement S;
@@
(
  E = malloc(SIZE);
+ printf("%d", SIZE);
|
  E = calloc(N, SIZE);
+ printf("%d", (N) * (SIZE));
)

However, it cannot match initializations and if-statements. I am not quite understanding your "malloc(SIZE)@S" solution. Could you point on how to use the it, or perhaps some related links?

Thank you,
Yudi

Quoted 36 lines · Julia Lawall
Quoted 10 lines · Yudi Yang

Hello,

I would like to match the size of allocation, I am wondering if I write the following:

@print@
expression N, SIZE;
@@

(
  malloc(SIZE);

Code doesn't usually contain just malloc(...); because then the return value would be thrown away.  You can write x = malloc(SIZE); for an expression metavariable x.  If malloc can be used in other contexts, eg an if header, then you can write malloc(SIZE)@S where S is a statement metavariable.  I think that that will allow the printf to be added after the innermost statement containing the call to malloc.

julia

Quoted 10 lines

|
  calloc(N, SIZE);
)
+ printf(“Allocation: %d\n", (N) * (SIZE));

I am not sure if I could set the default value for N in the malloc case, or is there anything I should do to make it correct.

Best, Yudi

#1 lore raw
Quoted 16 lines · Yudi Yang

Hello Julia,

Thank you for your answer! I have modified my code:

@@
expression E, SIZE, N;
statement S;
@@
(
  E = malloc(SIZE);
+ printf("%d", SIZE);
|
  E = calloc(N, SIZE);
+ printf("%d", (N) * (SIZE));
)

Quoted 3 lines

However, it cannot match initializations and if-statements. I am not quite understanding your "malloc(SIZE)@S" solution. Could you point on how to use the it, or perhaps some related links?

What I proposed didn't work, but the following works fine:

@@
statement S;
expression size;
@@

(
S
+printf("%d\n",size);
&
malloc(size)
)

julia

author
#2 lore raw

Thank you. It now matches if-statements. However, in case of a variable initializer such as:

void* p = malloc(100);

The solution does not correctly recognize the allocation.

Best,
Yudi

Quoted 39 lines · 在 2023年4月15日 -0500 02:31,Julia Lawall <[email protected]>,写道:
Quoted 16 lines · Yudi Yang

Hello Julia,

Thank you for your answer! I have modified my code:

@@
expression E, SIZE, N;
statement S;
@@
(
  E = malloc(SIZE);
+ printf("%d", SIZE);
|
  E = calloc(N, SIZE);
+ printf("%d", (N) * (SIZE));
)

Quoted 3 lines

However, it cannot match initializations and if-statements. I am not quite understanding your "malloc(SIZE)@S" solution. Could you point on how to use the it, or perhaps some related links?

What I proposed didn't work, but the following works fine:

@@
statement S;
expression size;
@@

(
S
+printf("%d\n",size);
&
malloc(size)
)

julia

#3 lore raw
Quoted 5 lines · Yudi Yang

Thank you. It now matches if-statements. However, in case of a variable initializer such as:

void* p = malloc(100);

The solution does not correctly recognize the allocation.

You should be able to use the same idea, except put D, declared as

declaration D;

julia

Quoted 47 lines

Best, Yudi

在 2023年4月15日 -0500 02:31,Julia Lawall <[email protected]>,写道:

      On Sat, 15 Apr 2023, Yudi Yang wrote:

            Hello Julia,

            Thank you for your answer! I have modified my code:

            @@
            expression E, SIZE, N;
            statement S;
            @@
            (
              E = malloc(SIZE);
            + printf("%d", SIZE);
            |
              E = calloc(N, SIZE);
            + printf("%d", (N) * (SIZE));
            )

            However, it cannot match initializations and if-statements.
            I am not quite understanding your "malloc(SIZE)@S" solution.
            Could you point on how to use the it, or perhaps some related links?


      What I proposed didn't work, but the following works fine:

      @@
      statement S;
      expression size;
      @@

      (
      S
      +printf("%d\n",size);
      &
      malloc(size)
      )

      julia
author
#4 lore raw

Hi Julia,
I have tried out the following, but it seems not working.

(
  D;
+ printf("%d", SIZE);
&
  malloc(SIZE)
)

Best,
Yudi

Quoted 62 lines · 在 2023年4月15日 -0500 11:53,Julia Lawall <[email protected]>,写道:
Quoted 5 lines · Yudi Yang

Thank you. It now matches if-statements. However, in case of a variable initializer such as:

void* p = malloc(100);

The solution does not correctly recognize the allocation.

You should be able to use the same idea, except put D, declared as

declaration D;

julia

Quoted 46 lines

Best, Yudi

在 2023年4月15日 -0500 02:31,Julia Lawall <[email protected]>,写道:

On Sat, 15 Apr 2023, Yudi Yang wrote:

Hello Julia,

Thank you for your answer! I have modified my code:

@@
expression E, SIZE, N;
statement S;
@@
(
  E = malloc(SIZE);
+ printf("%d", SIZE);
|
  E = calloc(N, SIZE);
+ printf("%d", (N) * (SIZE));
)

However, it cannot match initializations and if-statements. I am not quite understanding your "malloc(SIZE)@S" solution. Could you point on how to use the it, or perhaps some related links?

What I proposed didn't work, but the following works fine:

@@
statement S;
expression size;
@@

(
S
+printf("%d\n",size);
&
malloc(size)
)

julia

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.22.394.2304172139580.2883@hadrien

author
#6 lore raw

Without having the “;” here, we are experiencing an error:

plus: parse error:
 File "patch.cocci", line 11, column 0, charpos = 103 around = '&',
 whole content = &

Quoted 94 lines · 在 2023年4月17日 -0500 14:40,Julia Lawall <[email protected]>,写道:
Quoted 5 lines · Yudi Yang

Hi Julia,
I have tried out the following, but it seems not working.

(
  D;

I don't think you should have the semicolon. That would be matching eg int x;;

julia

Quoted 79 lines

+ printf("%d", SIZE);
&
  malloc(SIZE)
)

Best, Yudi
在 2023年4月15日 -0500 11:53,Julia Lawall <[email protected]>,写道:

On Sat, 15 Apr 2023, Yudi Yang wrote:

Thank you. It now matches if-statements. However, in case of a variable initializer such as:

void* p = malloc(100);

The solution does not correctly recognize the
allocation.

You should be able to use the same idea, except put D, declared as

declaration D;

julia

Best, Yudi

在 2023年4月15日 -0500 02:31,Julia Lawall
<[email protected]>,写道:

On Sat, 15 Apr 2023, Yudi Yang wrote:

Hello Julia,

Thank you for your answer! I have modified my code:

@@
expression E, SIZE, N;
statement S;
@@
(
  E = malloc(SIZE);
+ printf("%d", SIZE);
|
  E = calloc(N, SIZE);
+ printf("%d", (N) * (SIZE));
)

However, it cannot match initializations and if-statements.
I am not quite understanding your "malloc(SIZE)@S" solution.
Could you point on how to use the it, or perhaps some related links?

What I proposed didn't work, but the following works fine:

@@
statement S;
expression size;
@@

(
S
+printf("%d\n",size);
&
malloc(size)
)

julia