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

Julia Lawall <[email protected]> Sun, 14 Jun 2026 18:20:51 +0200 (CEST)
Newsgroups fr.inria.cocci
Message-ID <[email protected]>

On Sun, 14 Jun 2026, Markus Elfring wrote:

> > Could you please make something that doesn't involve a database?
>
> Do you find another test approach helpful for the desired clarification?
>
>
> SmPL script example:
> @initialize:python@
> @@
> import sys
> delimiter = "|"
>
> def display_data(fun, source1, source2, x, y):
>     sys.stdout.write(delimiter.join(("info", fun, x, y))
>                         + "\n")
>     for place in source1:
>        sys.stdout.write(delimiter.join(("first",
>                                         place.file,
>                                         place.line,
>                                         str(int(place.column) + 1)))
>                         + "\n")
>
>     for place in source2:
>        sys.stdout.write(delimiter.join(("tail",
>                                         place.file,
>                                         place.line,
>                                         str(int(place.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
>  }
>
> @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)
>
>
> https://elixir.bootlin.com/linux/v7.1-rc7/source/fs/smb/client/fs_context.c#L1060-L1241

Thank you for making a complete example.  Now take the huge C function and
make a minimal example that illustrates the problem you are concerned
about.  The function is almost 200 lines long.

julia


>
> Extracted function implementation
>
>
> Questionable test result:
> Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/Probe> time /usr/bin/spatch ../janitor/list_selected_duplicate_statements_in_if_branches3.cocci smb3_reconfigure-excerpt2-20260608.c
> init_defs_builtins: /usr/lib64/coccinelle/standard.h
> HANDLING: smb3_reconfigure-excerpt2-20260608.c
> info|smb3_reconfigure|old_ctx|rc
> first|smb3_reconfigure-excerpt2-20260608.c|34|3
> tail|smb3_reconfigure-excerpt2-20260608.c|185|2
> info|smb3_reconfigure|old_ctx|rc
> first|smb3_reconfigure-excerpt2-20260608.c|185|2
> tail|smb3_reconfigure-excerpt2-20260608.c|185|2
>
> real    0m0,506s
> user    0m0,400s
> sys     0m0,095s
>
>
>
> How do you think about to avoid repeated data display here (despite of the applied goto chain)?
>
> Regards,
> Markus
>