[LTP] [PATCH 1/2] lib: initialize lib|main_pid to zero in the setup_ipc

Li Wang liwang@redhat.com
Wed May 14 03:14:36 CEST 2025


On Tue, May 13, 2025 at 11:42 PM Petr Vorel <pvorel@suse.cz> wrote:
>
> > On Tue, May 13, 2025 at 9:02 PM Li Wang <liwang@redhat.com> wrote:
>
> > > On Tue, May 13, 2025 at 8:52 PM Petr Vorel <pvorel@suse.cz> wrote:
>
> > > > Hi all,
>
> > > > ...
> > > > > > +++ b/lib/tst_test.c
> > > > > > @@ -59,7 +59,6 @@ static const char *tid;
> > > > > >  static int iterations = 1;
> > > > > >  static float duration = -1;
> > > > > >  static float timeout_mul = -1;
> > > > > > -static pid_t lib_pid;
> > > > > >  static int mntpoint_mounted;
> > > > > >  static int ovl_mounted;
> > > > > >  static struct timespec tst_start_time; /* valid only for test pid */
> > > > > > @@ -143,7 +142,9 @@ static void setup_ipc(void)
> > > > > >             tst_futexes = (char *)results + sizeof(struct results);
> > > > > >             tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
> > > > > >     }
> > > > > > -   results->lib_pid = lib_pid;
> > > > > > +
> > > > > > +   results->lib_pid = 0;
> > > > > > +   results->main_pid = 0;
>
> > > > nit: Is it really needed to int them to 0? Because they should be 0 due the
> > > > default struct value, right?
>
> > The results structure memory was dynamically allocated via
> > mmap so the default value might not be zero. Unless we do
> > explicitly call memset(, 0, ...).
>
> Thanks for info. Or just use {} instead of memset?

This only applies to automatic (stack) or static allocation.
As we're dealing with dynamically allocated memory via mmap(),
we still need memset() or manually initialize it.

In setup_ipc():

results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);

>
> struct results saved_results = {};
>
> Tiny detail, just it's more readable to me.
>
> Kind regards,
> Petr
>


-- 
Regards,
Li Wang



More information about the ltp mailing list