Skip to contentSkip to conversation

Matching a label after a return

cocci Christoph Böhmwalder 3 participants 3 messages Apr 8, 2022

lore
Share & tools
lore
#0 lore raw

Hi all,

Consider some function like this:

void test (int x)
{
if (x)

       goto label;

return;
label:
do_something();
}

and the following coccinelle patch:

@@
identifier x;
identifier label;
@@
if (x)

    goto label;

...
- label:
- do_something();

I would expect this to match the label, but it does not:

$ spatch --sp-file test.cocci test.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: test.c

Is this a bug or am I holding it wrong?
It does not appear to be directly related to the label itself, because

@@
identifier label;
@@
- label:
- do_something();

gives the expected result.

$ spatch --version
spatch version 1.1.0-g69ea912-dirty compiled with OCaml version 4.12.0
Signature · 3 lines
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running
DRBD HA —  Disaster Recovery — Software defined Storage
#1 lore raw
Quoted 22 lines · Christoph Böhmwalder

Hi all,

Consider some function like this:

void test (int x)

{
   if (x)
       goto label;
   return;
label:
   do_something();
}

and the following coccinelle patch:

@@
identifier x;
identifier label;
@@
if (x)

Quoted 7 lines
    goto label;

...
- label:
- do_something();

I would expect this to match the label, but it does not:

The problem is that there is no cotrol-flow path from the bottom of the if to the top of the label, due to the return. Maybe the following will work, but I'm mot sure:

 if (x) {
     goto label;
     ...

- label:
- do_something();
}

julia

Quoted 28 lines

$ spatch --sp-file test.cocci test.c
init_defs_builtins: /usr/lib64/coccinelle/standard.h
HANDLING: test.c

Is this a bug or am I holding it wrong?
It does not appear to be directly related to the label itself, because

@@
identifier label;
@@
- label:
- do_something();

gives the expected result.

$ spatch --version
spatch version 1.1.0-g69ea912-dirty compiled with OCaml version 4.12.0

--
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running DRBD HA — Disaster Recovery — Software defined Storage

#2 lore raw
In reply to Christoph Böhmwalder
Quoted 8 lines

and the following coccinelle patch:

@@
identifier x;
identifier label;
@@
if (x)

Quoted 4 lines
    goto label;

...
- label:
- do_something();

I find such a source code transformation approach questionable.

Would you like to keep the goto statement valid?

Quoted 1 line

Is this a bug or am I holding it wrong?

I suggest to achieve further software evolution according to affected implementation details.

Regards,
Markus