[Spice-devel] [spice-common 3/7] log: Use G_STATIC_ASSERT for spice_static_assert implementation

Frediano Ziglio fziglio at redhat.com
Mon Nov 30 05:57:31 PST 2015


> 
> On Mon, Nov 30, 2015 at 07:01:23AM -0500, Frediano Ziglio wrote:
> > Nacked!
> > 
> > Believe or not this seems to not work!
> > 
> > I added this code in quic.c
> > 
> >   spice_static_assert (evol == 500);
> > 
> > which is surely wrong... and it's compiling !!!
> > 
> > Than I tried to get code using
> > 
> >   #define BBB(s) #s
> >   #define AAA(s) BBB(s)
> >   printf("%s\n", AAA(spice_static_assert (evol == 500)));
> > 
> > and I'm getting
> > 
> >   typedef char _GStaticAssertCompileTimeAssertion_2[(evol == 500) ? 1 : -1]
> >   __attribute__((__unused__))
> > 
> > I'm using Fedora 22 compiler.
> > 
> > I'm quite surprised!
> 
> This is explained in verify.h actually
> «
>      One might think that an array size check would have the same
>      effect, that is, that the type struct { unsigned int dummy[W]; }
>      would work as well.  However, inside a function, some compilers
>      (such as C++ compilers and GNU C) allow local parameters and
>      variables inside array size expressions.  With these compilers,
>      an array size check would not properly diagnose this misuse of
>      the verify macro:
> 
>        void function (int n) { verify (n < 0); }
> »
> 
> I filed https://bugzilla.gnome.org/show_bug.cgi?id=758844 for it.
> 

Good

> I can change this to use verify.h rather than G_STATIC_ASSERT. The
> places where we are currently using spice_static_assert() would not
> really be impacted by that though. One place needs to change (in quic.c)
> as the static assert should be a spice_assert().
> 

No, you can change type of constant to make the compile time check work

> Christophe
> 

Frediano


More information about the Spice-devel mailing list