Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-3650

incorrect union serialization

    XMLWordPrintableJSON

Details

    • 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

          Activity

            People

              jensg Jens Geyer
              jensg Jens Geyer
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: