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

Christophe Fergeau cfergeau at redhat.com
Mon Nov 30 05:15:07 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.

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().

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20151130/4fbfc838/attachment.sig>


More information about the Spice-devel mailing list