Re: [cocci] Searching for duplicate exception handling code with SmPL?

Julia Lawall <[email protected]> Tue, 16 Jun 2026 09:19:54 +0200 (CEST)
Newsgroups fr.inria.cocci
Message-ID <[email protected]>
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323329-441025157-1781594394=:3706
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8BIT



On Tue, 16 Jun 2026, Markus Elfring wrote:

> >>> Is such a number suspicious together with the information “9620 files match”?
> >>
> >> I adjusted the implementation of the function “display_data” another bit
> >> so that a questionable condition can be checked and displayed directly.
> …
> >> Markus_Elfring@Sonne:…/Projekte/Bau/Linux/scripts/Coccinelle> rg 'hit' list_selected_duplicate_statements_in_if_branches4-20260612.txt | wc -l
> >> 254
> >>
> >>
> >> Such a number looks more promising for possible update candidates,
> >> doesn't it?
> >
> > I don't know what your semantic patch does,
>
> Why did you not forward original background information to the mailing list
> for Coccinelle so far?
>
> 2026-06-14
> https://lore.kernel.org/cocci/[email protected]/
> https://sympa.inria.fr/sympa/arc/cocci/2026-06/msg00010.html
>
>
> >                                             so I don't know whether the
> > result is useful or not.
> So I hope also again that the following SmPL script variant can influence development ideas
> in desirable directions.
>
>
> @initialize:python@
> @@
> import sys
> delimiter = "|"
>
> def display_data(fun, source1, source2, x, y):
>     sys.stdout.write(delimiter.join(("info", fun, x, y))
>                         + "\n")
>     place1 = source1[0]
>     place2 = source2[0]
> #    if place1 == place2:
>     if (place1.line == place2.line) and (place1.column == place2.column):
>        sys.stdout.write(delimiter.join(("error",
>                                         "same position",
>                                         place1.file,
>                                         place1.line,
>                                         str(int(place1.column) + 1)))
>                         + "\n")
>
>     else:
>        sys.stdout.write(delimiter.join(("hit",
>                                         place1.file,
>                                         place1.line,
>                                         str(int(place1.column) + 1),
>                                         place2.line,
>                                         str(int(place2.column) + 1)))
>                         + "\n")
>
> @searching exists@
> expression e, x;
> identifier item, rc, work;
> position p1, p2;
> type t;
> @@
>  t work(...)
>  {
>  ... when any
>  if (...)
>  {
>  ... when != rc = e
>      when != item = x
>  kfree@p1(item);
>  return rc;
>  }
>  ... when any
>  if (...)
>  {
>  ... when != rc = e
>      when != item = x
>  kfree@p2(item);
>  return rc;
>  }
>  ... when any
>  }

So why do you think that the number of relevant files diminshed?
Coccinelle informs you of what tokens it has considered, but I don't think
you included that.

Furthermore, it seems strange that you declare rc to be an identifier.
-ENOMEM, for example, is not an identifier.  It is an expression.

And you still have no checks for gotos, so you will still get cases where
both kfrees are on the same line.  Another approach is to just adjust your
python code to discard such cases.  I see that you do that already.  But
why not just ignore such results instead of complaining about them?

Is there really nothing like printf in python?
sys.stdout.write(delimiter.join looks very primitive.

julia


>
> @script:python collection@
> fun << searching.work;
> x << searching.item;
> y << searching.rc;
> p1 << searching.p1;
> p2 << searching.p2;
> @@
> display_data(fun, p1, p2, x, y)
>
>
>
> Test result examples:
> Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> /usr/bin/spatch ../janitor/list_selected_duplicate_statements_in_if_branches4.cocci snd_seq_fifo_new-excerpt-20260608.c
> …
> info|snd_seq_fifo_new|f|NULL
> hit|snd_seq_fifo_new-excerpt-20260608.c|13|3|18|3
> Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> /usr/bin/spatch ../janitor/list_selected_duplicate_statements_in_if_branches4.cocci test-insufficient_goto_chain_usage.c
> …
> info|my_test|md1|rc
> hit|test-insufficient_goto_chain_usage.c|12|5|36|2
> info|my_test|md1|rc
> error|same position|test-insufficient_goto_chain_usage.c|36|2
> Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> /usr/bin/spatch arch/arm/xen/enlighten.c /home/altes_Heim2/elfring/Projekte/Coccinelle/janitor/list_selected_duplicate_statements_in_if_branches4.cocci
> …
> info|arch_xen_unpopulated_init|regs|rc
> error|same position|arch/arm/xen/enlighten.c|410|2
>
>
> Regards,
> Markus
>
--8323329-441025157-1781594394=:3706--