golang/xenlight: Fix handling of marshalling of empty elements for keyed unions
authorGeorge Dunlap <george.dunlap@citrix.com>
Thu, 5 Mar 2020 11:34:07 +0000 (11:34 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 9 Mar 2020 14:36:29 +0000 (14:36 +0000)
commit99f1c935190986068a36fb5e78a00e6b71b08f25
tree3ed6a7db673da6dd47a034d6326df5bfde476dd6
parent86184145006d5cc02f8c57e1e516c76205f4d8cc
golang/xenlight: Fix handling of marshalling of empty elements for keyed unions

Keyed types in libxl_types.idl can have elements of type 'None'.  The
golang type generator (correctly) don't implement any union types for
these empty elements.  However, the toC and fromC helper generators
incorrectly treat these elements as invalid.

Consider for example, libxl_channelinfo.  The idl contains the
following keyed element:

    ("u", KeyedUnion(None, libxl_channel_connection, "connection",
           [("unknown", None),
            ("pty", Struct(None, [("path", string),])),
            ("socket", None),
           ])),

But the toC marshaller currently looks like this:

switch x.Connection {
case ChannelConnectionPty:
tmp, ok := x.ConnectionUnion.(ChannelinfoConnectionUnionPty)
if !ok {
return errors.New("wrong type for union key connection")
}
var pty C.libxl_channelinfo_connection_union_pty
if tmp.Path != "" {
pty.path = C.CString(tmp.Path)
}
ptyBytes := C.GoBytes(unsafe.Pointer(&pty), C.sizeof_libxl_channelinfo_connection_union_pty)
copy(xc.u[:], ptyBytes)
default:
return fmt.Errorf("invalid union key '%v'", x.Connection)
}

Which means toC() will fail for ChannelConnectionUnknown or
ChannelConnectionSocket.

Modify the generator to handle keyed union elements of type 'None'.
For fromC, set the value to 'nil'; for toC, leave things as-is.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Reviewed-by: Nick Rosbrook <rosbrookn@ainfosec.com>
tools/golang/xenlight/gengotypes.py
tools/golang/xenlight/helpers.gen.go