qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] QAPI and empty structs
@ 2014-12-23 15:50 Peter Maydell
  2014-12-23 15:54 ` Eric Blake
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2014-12-23 15:50 UTC (permalink / raw)
  To: QEMU Developers; +Cc: Markus Armbruster, Luiz Capitulino

In qapi-schema.json we have a couple of entries that define
empty structures, like this:

{ 'type': 'ChardevDummy', 'data': { } }

In the generated qapi-types.h these are turned into empty C structs:

struct ChardevDummy
{
};

and clang warns about them:
./qapi-types.h:3752:1: warning: empty struct has size 0 in C, size 1
in C++ [-Wextern-c-compat]
struct Abort
^
./qapi-types.h:3811:1: warning: empty struct has size 0 in C, size 1
in C++ [-Wextern-c-compat]
struct NetdevNoneOptions
^
./qapi-types.h:4282:1: warning: empty struct has size 0 in C, size 1
in C++ [-Wextern-c-compat]
struct ChardevDummy
^

Although you could argue that we don't care about differences
in C and C++ semantics, having a zero-sized struct floating
around seems a bit risky to me, since for instance a naive
attempt to g_malloc() space for it will return NULL.

How should we handle these? Should the qapi-types generator
stick a dummy field in so they aren't zero sized? Are they
actually an error in the schema? (what's the point of them?)
Do we claim that actually all our code correctly handles
zero sized structs and just suppress the clang warning?

thanks
-- PMM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] QAPI and empty structs
  2014-12-23 15:50 [Qemu-devel] QAPI and empty structs Peter Maydell
@ 2014-12-23 15:54 ` Eric Blake
  2015-01-12 15:31   ` Markus Armbruster
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Blake @ 2014-12-23 15:54 UTC (permalink / raw)
  To: Peter Maydell, QEMU Developers; +Cc: Markus Armbruster, Luiz Capitulino

[-- Attachment #1: Type: text/plain, Size: 1181 bytes --]

On 12/23/2014 08:50 AM, Peter Maydell wrote:
> In qapi-schema.json we have a couple of entries that define
> empty structures, like this:
> 
> { 'type': 'ChardevDummy', 'data': { } }
> 
> In the generated qapi-types.h these are turned into empty C structs:
> 

> Although you could argue that we don't care about differences
> in C and C++ semantics, having a zero-sized struct floating
> around seems a bit risky to me, since for instance a naive
> attempt to g_malloc() space for it will return NULL.

Oh, good point.

> 
> How should we handle these? Should the qapi-types generator
> stick a dummy field in so they aren't zero sized?

Sounds like the best plan to me.

> Are they
> actually an error in the schema? (what's the point of them?)

No, they are valid - it is how you express a union between a branch that
needs no additional members compared to any other branch.

> Do we claim that actually all our code correctly handles
> zero sized structs and just suppress the clang warning?

I'd be fine with adding a dummy member.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] QAPI and empty structs
  2014-12-23 15:54 ` Eric Blake
@ 2015-01-12 15:31   ` Markus Armbruster
  2015-01-12 15:33     ` Peter Maydell
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Armbruster @ 2015-01-12 15:31 UTC (permalink / raw)
  To: Eric Blake; +Cc: Peter Maydell, QEMU Developers, Luiz Capitulino

Eric Blake <eblake@redhat.com> writes:

> On 12/23/2014 08:50 AM, Peter Maydell wrote:
>> In qapi-schema.json we have a couple of entries that define
>> empty structures, like this:
>> 
>> { 'type': 'ChardevDummy', 'data': { } }
>> 
>> In the generated qapi-types.h these are turned into empty C structs:
>> 
>
>> Although you could argue that we don't care about differences
>> in C and C++ semantics, having a zero-sized struct floating
>> around seems a bit risky to me, since for instance a naive
>> attempt to g_malloc() space for it will return NULL.
>
> Oh, good point.

Less of an issue than with malloc(), because NULL isn't both an error
and a success value.

>> How should we handle these? Should the qapi-types generator
>> stick a dummy field in so they aren't zero sized?
>
> Sounds like the best plan to me.

I wouldn't bother myself, but if somebody posted working patches, I
wouldn't bother objecting to the idea, either.

>> Are they
>> actually an error in the schema? (what's the point of them?)
>
> No, they are valid - it is how you express a union between a branch that
> needs no additional members compared to any other branch.

Yes.

>> Do we claim that actually all our code correctly handles
>> zero sized structs and just suppress the clang warning?
>
> I'd be fine with adding a dummy member.

The clang warning is for code meant to be compiled both as C and as C++,
i.e. it's of no interest to us.  Suppressing it sounds fine to me.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] QAPI and empty structs
  2015-01-12 15:31   ` Markus Armbruster
@ 2015-01-12 15:33     ` Peter Maydell
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2015-01-12 15:33 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: QEMU Developers, Luiz Capitulino

On 12 January 2015 at 15:31, Markus Armbruster <armbru@redhat.com> wrote:
> Eric Blake <eblake@redhat.com> writes:
>> On 12/23/2014 08:50 AM, Peter Maydell wrote:
>>> Although you could argue that we don't care about differences
>>> in C and C++ semantics, having a zero-sized struct floating
>>> around seems a bit risky to me, since for instance a naive
>>> attempt to g_malloc() space for it will return NULL.
>>
>> Oh, good point.
>
> Less of an issue than with malloc(), because NULL isn't both an error
> and a success value.

No, it's just a value that pretty much no caller is going
to expect to get and which they're very likely to be buggy
in dealing with...

>>> How should we handle these? Should the qapi-types generator
>>> stick a dummy field in so they aren't zero sized?
>>
>> Sounds like the best plan to me.
>
> I wouldn't bother myself, but if somebody posted working patches, I
> wouldn't bother objecting to the idea, either.

http://patchwork.ozlabs.org/patch/423777/  :-)

-- PMM

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-01-12 15:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-23 15:50 [Qemu-devel] QAPI and empty structs Peter Maydell
2014-12-23 15:54 ` Eric Blake
2015-01-12 15:31   ` Markus Armbruster
2015-01-12 15:33     ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).