5

I'm getting the following error "unmarshaling error: proto: cannot parse reserved wire type" while unmarshaling a binary protobuf message.

newMessage := &MessageName{}
err = proto.Unmarshal(data, newMessage)

Here for data I'm reading from Protobuf Binary file whose contents look something like the Binary format given here -> What does the protobuf text format look like?

After reading the file and storing in data, data looks something like this [23 67 102 56 ... ]

How to fix this error?

Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
sxp
  • 193
  • 1
  • 1
  • 11

1 Answers1

4

[23 67 102 56 ... ] looks like a slice of ints, I'll assume the ints are the byte values. If that's true, this is not a protobuf stream!

Looking at the protobuf docs, the first byte is always a varint key. This is made up of a field number (first 5 bits) and a wire type (last 3 bits). Your int of 23 looks like this:

  0001  0111
  ^      ^
  varint wiretype

The valid wire types are shown in the docs. Your binary value 111 is decimal 7, which is not one of the listed types. Hence: cannot parse reserved wire type

Type    Meaning             Used For
0       Varint              int32, int64, uint32, uint64, sint32, sint64, bool, enum
1       64-bit              fixed64, sfixed64, double
2       Length-delimited    string, bytes, embedded messages, packed repeated fields
3       Start group         groups (deprecated)
4       End group           groups (deprecated)
5       32-bit              fixed32, sfixed32, float
dwurf
  • 12,393
  • 6
  • 30
  • 42
  • The actual Binary file has something like this: 00000000 0a 0b 64 73 66 61 64 73 61 66 73 61 66 10 ea 01 |..dsfadsafsaf...| 00000010 18 f6 f9 8e 01 18 f7 f9 8e 01 18 f8 f9 8e 01 20 |............... |. Not the exact one but something similar to this. I did ReadFile() on that which returns []byte and stored them in 'data' . I was trying to unmarshal the byte array into an object – sxp May 27 '20 at 04:47
  • 2
    My answer is the same, the error message you are getting indicates that the parser is choking on invalid data while parsing the protobuf stream. That error is raised from here https://github.com/golang/protobuf/blob/b5de78c91d0d09482d65f0a96927631cd343d7bb/proto/buffer.go#L313 – dwurf May 27 '20 at 05:01
  • This "cannot parse reserved wire type" may be caused by a breaking change. Like if you reused a field number. The specified field of one type is changed into a different type. This can cause an unmarshal error. Solution is mark old field number as reserved only and use a different number to the changed fields. Becareful on these kind of changes. – Vaisakh Rajagopal Jul 19 '23 at 13:03