The Protobuf Fixed32 Is Four Bytes in Every Message (and the First Byte Is the Least Significant One)

fixed32, sfixed32, float and their 64-bit siblings are the only types in the format whose size never changes, and the one wire convention that trips hand-written readers is which end they arrive on. I encoded the value 0x01020304 in a fixed32 field at number 1 and got 0d04030201, tag 0d then the four value bytes in reverse, least significant byte first. The format is little-endian here, the same direction the varints run, so the low bits of a fixed value come first exactly the way the low bits of a varint come first, only with a declared size of four. Read the payload back little-endian and 16909060 comes out. Read it big-endian and you get 67305985, a perfectly plausible number, no error, no warning, just the digits in the wrong order. There is nothing in the bytes that says which end is up, so every raw reader that ever hit a fixed type has made that mistake once.

Where fixed earns its keep is size predictability, and the crossover with varint is a number you can write down. A fixed32 field is five bytes on the wire for any value, the maximum included, because it is the tag plus exactly four. A uint32 in the same position is two bytes for 1, five bytes for 268435455, and six bytes the moment the value reaches 268435456, when the varint needs an extra byte of headroom. So values that stay below 2^28 make the varint the smaller encoding, and anything in the top of the 32-bit range makes fixed the smaller one. The 64-bit pair splits later: a uint64 and a fixed64 both take nine bytes around 2^49, and from 2^56 up the flat nine of fixed beats the ten of the varint. Absence is agreed on by both encoders, an unset fixed costs zero bytes exactly like an unset varint, so the choice is a per-field one made on what the values in that field actually look like.

The encoders also split on an explicit zero, and the split is measurable in bytes. A fixed32 set to zero on purpose: the JS library writes the tag and four zero bytes, 0d00000000, a field that announces itself and carries nothing, the same present-but-empty rule the empty-string and the Any posts describe. The C++ encoder writes nothing at all, protoc 25.3 with the field set to zero emits no bytes. Both decode back to zero either way, so the wire interoperates, but a byte-counting diff between two encodings of the same message will find five bytes of difference that both sides consider the same value, and that is the class of discrepancy that makes a silent schema migration look like data changed.

The retype case is the one that hurts, and it hurts silently. float 1.5 on the wire is the four bytes 00 00 c0 3f, and if a later schema declares those bytes as fixed32, the decoder hands back 1069547520 in the JS library and in protoc, same number, no error. fixed32 1 read back as a float gives the smallest positive denormal, about 1.4e-45, in both. float shares wire type 5 with fixed32 and double shares wire type 1 with fixed64, so a retype inside that family changes no tag and no byte, and every old message still in the wild now means something different to the new schema. That is why the diff tab reports a float-to-fixed32 change as a same-wire silent reinterpret instead of a loud break: the tag does not move, the bytes do not move, and the reader is the only thing that changed.

The two decoders stop agreeing in the cross-wire case, when the tag says one wire type and the declared type says another. Feed protoc the four-byte fixed32 value 0d04030201 under a schema that declares uint32 and it follows the tag, reads the four bytes, prints 16909060, exit zero, no complaint. The JS library follows the schema: it reads a varint starting at the first payload byte, and depending on the bytes it either runs off the end of the buffer with an index-out-of-range error, or hits a zero byte early and decodes the whole field to zero with no error at all, which is what the float 1.5 bytes do. Same bytes, same schema, two different outcomes, one of them a clean-looking zero. A schema decoder will not always flag a wire-type mismatch for you, and the mismatch is only guaranteed to be visible in the no-schema view, where the tag is all there is to read.

The tool handles all four of these types with the endianness built in, and the generated code keeps them apart, a float32 in Go and Rust, a fixed64 as a decimal string where the 64-bit JSON rule applies, base64 nowhere to be found. Where the split matters most is when you are writing a reader by hand or reviewing a schema change: use the raw decode tab to see the tag and the exact byte order, because the endianness and the wire type live in the tag, not in the schema, and the schema is what a normal decoder trusts on your behalf. The converter this post measured is at proto.datajutsu.com, and the tag and varint math underneath it is in the wire format post.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe