skip to content

A C++ packet parser checks if (p + len < p || p + len > end) before reading a field, yet the release build skips the wrap-around test; why, and what is the fix?

level: seniorimportance: should knowfreq 40%

answer

  1. pointer arithmetic has bounds too
  2. past one-past-the-end is undefined
  3. p + len < p is always false
  4. compare lengths, not pointers

basics

~20 s

Forming p + len beyond one past the end of the buffer is already undefined, so the compiler may assume p + len never wraps and fold p + len < p to false. Compare lengths instead: len > static_caststd::size_t(end - p).

solid answer

~50 s

Pointer arithmetic is only defined within an array and up to one past its end; a result outside that range is **undefined as soon as it is computed**, before any comparison. So `p + len < p` can only be true in an execution that already broke the rules, and Apple clang 21 warns `pointer comparison always evaluates to false [-Wtautological-compare]` and emits no code for it at `-O2`. The other half, `p + len > end`, also relies on forming an out-of-range pointer when `len` is too large, so it is not a sound bounds check either. The fix is to compare **lengths**, which are plain integers: `end - p` is the number of bytes left and is always valid, so `if (len > static_cast<std::size_t>(end - p)) reject();` never forms a bad pointer. A hostile `len` from the wire is exactly the input that reaches the deleted branch.

code

cpp · 21 lines
cpp
#include <cstddef>
#include <cstdio>

// Does a field of `len` bytes starting at `p` fit before `end`?
bool field_fits_naive(const unsigned char* p, const unsigned char* end,
                      std::size_t len) {
    if (p + len < p)          // "wraparound" guard: p + len past the buffer is already UB
        return false;
    return p + len <= end;
}

// Well-defined: compare lengths, never form an out-of-range pointer.
bool field_fits(const unsigned char* p, const unsigned char* end,
                std::size_t len) {
    return len <= static_cast<std::size_t>(end - p);
}

int main() {
    unsigned char buf[16] = {};
    std::printf("%d %d\n", field_fits(buf, buf + 16, 8), field_fits(buf + 4, buf + 16, 20));
}

go deeper

for a junior

Remember that a pointer may only move within its buffer and to one past its end; check sizes before moving it.

for a middle

Explain why p + len < p is always false in a valid program and why comparing len with end - p is safe.

for a senior

Trace the hostile-length input to the deleted branch, rewrite the parser around spans and length comparisons, and fuzz the optimized build.

for a principal

Require length-based bounds checks and span-style buffer types for every parser of untrusted input, with optimized-build fuzzing in CI.

## The scenario A network service parses length-prefixed fields out of a receive buffer `[p, end)`. A defensive engineer wrote: ```cpp bool field_fits_naive(const unsigned char* p, const unsigned char* end, std::size_t len) { if (p + len < p) // "wraparound" guard return false; return p + len <= end; } ``` Fuzzing with a huge `len` crashes the release build, while stepping through a debug build shows the wrap-around guard working. The release build did not lose the check by accident: the language says the check can never be true in a valid program. ## Why the compiler may drop the wrap-around test The standard's rule for adding an integer to a pointer allows results that point into the same array or one past its last element. Any other result means the behaviour is undefined, whether or not the pointer is ever read. Two consequences follow: 1. If `len` fits in the buffer, `p + len` is a valid pointer at or after `p`, and `p + len < p` is false. 2. If `len` is too large, forming `p + len` is already undefined, so the compiler owes nothing. As with the signed overflow check, only case 1 constrains the output, and there the comparison is always false. Apple clang 21 reports this at compile time, without optimization and even without `-Wall`, with the diagnostic `pointer comparison always evaluates to false [-Wtautological-compare]`, and at `-O2` the generated function is just an add, a compare against `end` and a return. The guard does not exist in the binary. At `-O0` the compiler emitted the addition and the comparison literally, so on a machine where the address wrapped the guard happened to fire; that is the "debug build honoured it" observation, and it was luck. ## The second half is not sound either `p + len <= end` looks correct, but for a hostile `len` it also forms a pointer far past the buffer, which is undefined in the same way. Today's compilers usually keep that comparison, yet the code still relies on undefined behaviour, and a future optimization that reasons about the buffer's size could change it. ## The fix: compare lengths, not pointers | Check | Forms an out-of-range pointer? | Survives optimization? | |---|---|---| | `p + len < p` | yes, when `len` is too large | no, folded to `false` | | `p + len > end` | yes, when `len` is too large | usually kept, still undefined | | `len > static_cast<std::size_t>(end - p)` | never | yes | | `std::span` with `len > s.size()` | never | yes | `end - p` subtracts two pointers into the same buffer, which is always defined, and yields the number of remaining bytes as a `std::ptrdiff_t`. Comparing `len` against it is ordinary integer arithmetic with no undefined case. The cast to `std::size_t` is safe because `end >= p` is an invariant of the parser; assert it. ## Hardening the whole parser - **Keep the buffer as a `std::span<const std::uint8_t>`** and advance with `subspan`, so every check compares sizes. - **Validate before computing offsets**: a field offset plus a length is the same pattern one level up, so check `offset <= size && len <= size - offset`. - **Read fields with `std::memcpy`** into local integers instead of casting pointers, which also sidesteps alignment and aliasing problems covered in the lesson on type punning. - **Fuzz the optimized build**, not only the debug build: the debug build is where the removed guard still looked present. - **Read the warnings**: `-Wtautological-compare` pointed straight at this bug. ## Debugging a release-only parser failure When a packet that the debug build rejects crashes the release build, work through it in order: 1. **Reproduce at the release optimization level** with debug information, using the exact packet, ideally the fuzzer's saved input. 2. **Look at the generated code** for the bounds check, with `-S` or a disassembler: if a comparison in the source has no instruction in the output, the compiler proved it dead. 3. **Ask what the compiler assumed**: a comparison that only an out-of-range pointer, a signed overflow or a null pointer could make true is dead by the rules of the language. 4. **Rewrite the check on integers** that are always in range, rebuild, and confirm the comparison now appears in the output. 5. **Add the packet to the regression corpus** so the optimized build is tested against it from then on.

  • Why is end - p always safe to compute in the fixed check?
    Subtracting two pointers into the same array, including its one-past-the-end position, is defined and yields the element count between them. The parser's invariant is that `p` never moves past `end`, so the difference is non-negative and fits in `std::ptrdiff_t`; converting it to `std::size_t` and comparing with `len` involves no undefined step.
  • Would doing the check with std::uintptr_t addresses instead of pointers be correct?
    Converting pointers to `std::uintptr_t` and comparing integers avoids forming a bad pointer, and unsigned wrap is defined, but the mapping from pointers to integers is implementation-defined, so the arithmetic is not guaranteed to mean what it seems. Comparing `len` with `end - p` says exactly what is meant, portably.

  • p + len < p reliably detects pointer wraparound on any flat-memory machine.
  • Pointer arithmetic is only undefined when the resulting pointer is dereferenced.
  • If the debug build rejects the bad packet, the release build will too.
  • p + len <= end is a fully well-defined bounds check for any len.