Menu

Reserving a length prefix and its body in one bounds check
📰

Reserving a length prefix and its body in one bounds check

DEV Community: cpp·polycratia·3 days ago
#mGwrSLat
#devtocpp#devto#dev#size#payload
Reading 0:00
15s threshold

A serialiser that writes a count and then writes the payload it counted has two failure points, and the second one is the dangerous one: the count has landed in the buffer, the payload has not, and whatever reads that buffer back trusts a length nothing backs. You don't close that gap with a better error path after the fact. You close it by reserving the prefix and the body in a single bounds check, then refusing the whole field before a single byte of it goes down.

I have written length-prefixed framing for card-rail payment messages, for on-chain payloads, and for service-to-service protocols that were never supposed to meet hostile input and eventually did. The partial-write bug has the same shape every time, and it does not look wrong in review.

The second failure point is the one that escapes

Here is the shape I am arguing against. It is not how bytespan does it; it is how almost everything else does it.

// Two calls, two bounds checks, two chances to refuse.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.