EmbedwrightArticlesLesson guideFree lessonsThe course · $79

Validate frame lengths before reading embedded buffers

By Chinmay Gupta · Updated 2026-12-01
Short answer. Validate the length field against the bytes remaining in the buffer before reading a payload. Avoid arithmetic that can wrap before a length comparison.

A length-prefixed message contains a claim about its own size. The claim may be wrong because of transmission errors, partial reads or a producer bug. A parser should not use it as permission to read beyond the actual storage that has been received. The framing format and the buffer owner's count are separate sources of information.

The contract

Consider a toy format: one octet gives the payload length, followed by exactly that many octets. The parser first checks that the length octet is available. It can then compare the declared length to the remaining storage, calculated only after the first check. In this format trailing bytes are accepted as possible next data; a stricter format might reject them.

A small executable example

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>
#include <stdio.h>
static bool complete(const uint8_t *p, size_t n) {
    if (n < 1) return false;
    return (size_t)p[0] <= n - 1;
}
int main(void) {
    const uint8_t a[] = {2, 10, 20};
    const uint8_t b[] = {0};
    assert(complete(a, sizeof a));
    assert(!complete(a, 2));
    assert(complete(b, sizeof b));
    assert(!complete(a, 0));
    puts("frame boundaries verified");
}
Real output
frame boundaries verified

Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.

Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1

What the example does not establish

The function assumes the caller supplies a valid pointer and truthful buffer length. It reports completeness only; it does not authenticate the message, validate payload semantics, or consume bytes. A streaming parser needs explicit partial-frame handling, a maximum permitted payload and a defined recovery policy. A timeout, if required, belongs to the protocol state machine rather than an unchecked read.

Checks to add in your own project

Make fixtures for zero-length payloads, maximum permitted lengths, unexpected extra bytes and every truncation point. For fixed-size input storage, reject an impossible claimed length before allocating or waiting for additional data. Independently test framing and application-level validation so that failures have precise meanings. The host test here intentionally checks only the small format's stated invariant.

The course goes further on this topic. Lesson 21 of DSA for Firmware, “The state machine that never forgets”, adds interactive labs and C exercises with tests that you run on your own machine. Read the lesson guide · See the course
Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
DSA for Firmware teaches it one real bug at a time.
  • 28 lessons, each starting from a real firmware bug.
  • 110 C exercises with tests, and a reference solution for every one.
  • Lessons 1 and 2 are free. No sign-up.
Start Lesson 1 freeSee the full course · $79

Get the next article by email

One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.