Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
0.9.3
-
None
-
Patch Available
Description
With C++ the following union generates incorrect serialization code:
union type_ex {
1 : string foo
2 : i32 bar
3 : double baz
}
This results in all three fields being unconditionally written, which not only wastes space on the wire, but also sets all __isset bits during deserialization. This makes it a bit hard to the other side to find out which union field is the right one.
uint32_t type_ex::write(::apache::thrift::protocol::TProtocol* oprot) const { uint32_t xfer = 0; apache::thrift::protocol::TOutputRecursionTracker tracker(*oprot); xfer += oprot->writeStructBegin("type_ex"); xfer += oprot->writeFieldBegin("STRING", ::apache::thrift::protocol::T_STRING, 1); xfer += oprot->writeString(this->STRING); xfer += oprot->writeFieldEnd(); xfer += oprot->writeFieldBegin("INT64", ::apache::thrift::protocol::T_I64, 2); xfer += oprot->writeI64(this->INT64); xfer += oprot->writeFieldEnd(); xfer += oprot->writeFieldBegin("DOUBLE", ::apache::thrift::protocol::T_DOUBLE, 3); xfer += oprot->writeDouble(this->DOUBLE); xfer += oprot->writeFieldEnd(); xfer += oprot->writeFieldStop(); xfer += oprot->writeStructEnd(); return xfer; }
Note that the code above is just right (due to "default" requiredness) if we talk about structs. But these rules do not really apply to union members where by definition only one value is used. Hence, union members implicitly have optional requiredness
Workaround
Add explicit optionals:
union type_ex {
1 : optional string foo
2 : optional i32 bar
3 : optional double baz
}
Attachments
Issue Links
- duplicates
-
THRIFT-3653 incorrect union serialization
- Closed
-
THRIFT-3655 incorrect union serialization
- Closed
-
THRIFT-3648 Union in c++ doesn't work
- Closed
- is duplicated by
-
THRIFT-1833 Thrift generates incorrect C++ writer for union
- Closed
1.
|
incorrect serialization of optionals | Closed | Jens Geyer | ||
2.
|
incorrect union serialization | Closed | Jens Geyer | ||
3.
|
incorrect serialization of optionals | Closed | Jens Geyer | ||
4.
|
incorrect union serialization | Closed | Jens Geyer | ||
5.
|
incorrect serialization of optionals | Closed | Jens Geyer |