opensourcerouting / c-capnproto

C library/compiler for the Cap'n Proto serialization/RPC protocol
MIT License
118 stars 40 forks source link

Fix code generation for nested unions. #44

Closed detly closed 3 years ago

detly commented 3 years ago

When a union is nested inside another union, it must be enclosed in a struct so that its "which" member is not overlapping with its other members.

Schema in question:

struct A {
    union {
        zero @0 :Void;
        one :union {
            first @1 :Void;
            second @2 :UInt16;
        }
    }
}

C code showing bug:

struct A {
    enum A_which which;
    capnp_nowarn union {
        enum A_one_which one_which;
        capnp_nowarn union {
            uint16_t second;
        } one;
    };
};

Correct C code:

struct A {
    enum A_which which;
    capnp_nowarn union {
        capnp_nowarn struct {
            enum A_one_which which;
            capnp_nowarn union {
                uint16_t second;
            };
        } one;
    };
};

Fixes #43.