All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Senna Tschudin <peter.senna@gmail.com>
To: Julia Lawall <julia.lawall@inria.fr>
Cc: cocci@inria.fr
Subject: Re: [cocci] Returning statically allocated nested structs
Date: Thu, 28 Mar 2024 14:03:09 +0100	[thread overview]
Message-ID: <CA+MoWDo-DJKq1jjC7Sr41c3+7G=P_wGsQHJrGaxMNyB+UOsjwg@mail.gmail.com> (raw)
In-Reply-To: <23254a95-222e-494-3f0-508a2790753@inria.fr>

Thanks Julia,

Your question about nesting inspired me to a simpler solution:

@rule1@
identifier i, s;
position p;
@@
struct i s;
...
return@p s;

@script:python@
p << rule1.p;
s << rule1.s;
@@
cocci.print_main(s, p)

Do I need to worry about calling cocci.print_secs() after the print_main()?

On Thu, Mar 28, 2024 at 11:10 AM Julia Lawall <julia.lawall@inria.fr> wrote:
>
>
>
> On Thu, 28 Mar 2024, Peter Senna Tschudin wrote:
>
> > Hi Julia,
> >
> > Thanks for the reply!
> >
> > On Thu, Mar 28, 2024 at 10:08 AM Julia Lawall <julia.lawall@inria.fr> wrote:
> > >
> > >
> > >
> > > On Thu, 28 Mar 2024, Peter Senna Tschudin wrote:
> > >
> > > > Dear list,
> > > >
> > > > I am trying to come up with a semantic patch to detect uses of nested
> > > > structs, more specifically:
> > > >  - the nested struct is statically allocated
> > > >  - the statically allocated nested struct is returned by a function.
> > > >
> > > > Here is an example:
> > > >
> > > > struct inner {
> > > >
> > > >     /* some inner struct stuff*/
> > > >
> > > > } inner;
> > > >
> > > > struct outer  {
> > > >
> > > >     /* some outer struct stuff*/
> > > >
> > > >     struct inner i; // The kind of nesting I care about
> > > >     struct inner is[SOME_MAGIC_NUMBER]; // The kind of nesting I care about too
> > > >
> > > >     struct inner *ip; // Nah, this is boring. I don't care about boring
> > > > } outer;
> > > >
> > > > void sillyfu() {
> > Argh, that was a typo!
> >
> > struct outer sillyfu() {
> > > >     struct outer ou = { }; // initialization does not matter.
> > > >     struct outer *oup = NULL; // Nah, this is boring. I don't care about boring
> > > >
> > > >     /* some serious silly stuff */
> > > >
> > > >     return ou;
> > >
> > > Not sure to understand.  The return type of the function is void.  Was
> > > that a typo?
> > >
> > > Returning a structure in general seems like something to be concerned
> > > about.  Does it matter that another structure is nested inside?
> >
> > I am not sure if it matters, but it may. The compiler seems to be the
> > arbiter who decides what happens when returning a local struct. It may
> > work, it may not. One theory for not working is variable scope.
> > Returning a local struct may not work due to the local nature of the
> > struct. In this case the compiler would free the memory used by the
> > struct when the function returns, causing undefined behavior. In this
> > scenario the nested struct simply adds another layer of the same
> > problem.
> >
> > Another theory says that the compiler may decide based on the struct
> > size. The compiler may tolerate returning certain struct sizes, but
> > not others.
>
> The following is not tested, but seems like it should be sufficient.
>
> @r@
> identifier i, j, k;
> @@
>
> struct i { ...
>   struct j k;
>   ...
> };
>
> @@
> identifier r.i;
> identifier f,x;
> @@
>
> f(...) {
>   struct i x = ...;
>   ...
> * return x;
> }
>
> julia



-- 
                         Peter

  reply	other threads:[~2024-03-28 13:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28  8:41 [cocci] Returning statically allocated nested structs Peter Senna Tschudin
2024-03-28  9:07 ` Julia Lawall
2024-03-28 10:00   ` Peter Senna Tschudin
2024-03-28 10:10     ` Julia Lawall
2024-03-28 13:03       ` Peter Senna Tschudin [this message]
2024-03-28 14:49         ` Julia Lawall
2024-03-28 14:57           ` Peter Senna Tschudin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+MoWDo-DJKq1jjC7Sr41c3+7G=P_wGsQHJrGaxMNyB+UOsjwg@mail.gmail.com' \
    --to=peter.senna@gmail.com \
    --cc=cocci@inria.fr \
    --cc=julia.lawall@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.