qemu-trivial
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [PATCH-for-5.2? 1/2] tests/qtest: variable defined by g_autofree nee


From: Chenqun (kuhn)
Subject: RE: [PATCH-for-5.2? 1/2] tests/qtest: variable defined by g_autofree need to be initialized
Date: Thu, 19 Nov 2020 12:41:37 +0000

> > On Thu, 19 Nov 2020 at 08:35, Chenqun (kuhn)
> <kuhn.chenqun@huawei.com>
> > wrote:
> > >
> > > > -----Original Message-----
> > > > >  static void tim_add_test(const char *name, const TestData *td,
> > > > > GTestDataFunc fn)  {
> > > > > -    g_autofree char *full_name;
> > > > > -
> > > > > -    full_name =
> > g_strdup_printf("npcm7xx_timer/tim[%d]/timer[%d]/%s",
> > > > > -                                tim_index(td->tim),
> > > > timer_index(td->timer),
> > > > > -                                name);
> > > > > +    g_autofree char *full_name = g_strdup_printf(
> > > > > +        "npcm7xx_timer/tim[%d]/timer[%d]/%s",
> tim_index(td->tim),
> > > > > +        timer_index(td->timer), name);
> > > >
> > > > Which compiler is so unintelligent that it cannot see that a
> > > > declaration immediately followed by an assignment must always
> > > > initialize
> > the variable ???
> > > >
> > > Hi Peter,
> > >   Glib requires that all g_auto* macros must be initialized.
> > >
> > > https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.ht
> > > ml
> > > #g-autofree
> >
> > Yes, and we initialize it with the "full_name = ..." line.
> > The g_autofree documentation says "this macro has similar constraints
> > as g_autoptr()", and the g_autoptr() documentation says "You must
> > initialise the variable in some way — either by use of an initialiser
> > or by ensuring that it is assigned to unconditionally before it goes out of
> scope."
> >
> > In this case the test code is doing the second of those two things.
> 
> Emm, maybe I didn't get it right. I've tried something as following:
> There are three pieces of code complied in GCC9.3 and GCC7.3.
> Code1:
>    g_autofree char *full_name;
>    full_name = g_strdup_printf("npcm7xx_timer");
I guess the GCC thinks that g_strdup_printf() may fail to be executed. After 
the function fails to be executed, and not give a NULL return value.
If this occurs, g_autofree will still executes g_free(*pp).  So, an warning is 
generated in the Code1 case.

In the example code provided by g-autoptr() documentation, the value assignment 
statement of the 'dirname' variable uses g_variant_lookup_value().
If the g_variant_lookup_value() function fails to be executed, a NULL value is 
returned. So, this example code is safe.
https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#g-autoptr
> 
> Code2:
>    g_autofree char *full_name = g_strdup_printf("npcm7xx_timer");
In this case, if g_strdup_printf() fails, the variable definition also fails. 
So, the Code2 is safe.
> 
> Code3:
>    g_autofree char *full_name;
>    full_name = NULL;
> 
> The result is as follows:
>   Code1: An warnig is generated for GCC7.3 or GCC9.3.
> 
>   Code2 and Code3: no any warnig whether compiler is GCC7.3 or GCC9.3.
> 
> I cannot explain why the Code1 warning is generated. But it always generates
> warning on the GCC compiler.

The above analysis is just my own guess. That may not be the truth.

Thanks,
Chen Qun



reply via email to

[Prev in Thread] Current Thread [Next in Thread]