A C++ network service casts a received char buffer to `const Header*` to read packet fields; what is wrong with that, and how should it parse them?
answer
- what objects live in the buffer
- alignment at an odd offset
- struct layout is not the wire format
- byte order on the wire
- memcpy into a local struct
basics
~20 sA char buffer holds no Header object, so reading through the cast pointer breaks strict aliasing; the address may also be misaligned, and padding and byte order may not match the wire. memcpy into a local Header, then convert byte order.
solid answer
~40 sThe buffer holds `char` objects. `reinterpret_cast<const Header*>(buf)->seq` reads them through a `std::uint32_t` glvalue, which strict aliasing forbids: undefined behaviour, independent of whether the bytes look right. It also has three practical problems. **Alignment**: a field at an odd offset in a `char` array is misaligned for `std::uint32_t`; some processors fault and UBSan reports it. **Layout**: the compiler may insert padding, so the struct may not match the wire format. **Byte order**: network protocols are usually big-endian, so the value needs conversion on little-endian hosts. The robust parser declares a local `Header`, `std::memcpy`s `sizeof(Header)` bytes from the buffer (after checking the length), `static_assert`s the struct size, and converts each field's byte order, for example with C++23 `std::byteswap` when `std::endian::native` is little. Compilers turn the small `memcpy` into ordinary loads.
code
cpp · 27 lines#include <cstdint>
#include <cstdio>
#include <cstring>
struct Header { // wire layout: 4 + 2 + 2 bytes, no padding
std::uint32_t seq;
std::uint16_t type;
std::uint16_t length;
};
static_assert(sizeof(Header) == 8);
// Wrong: the char buffer holds char objects, not a Header, and buf + 1 is misaligned.
std::uint32_t seq_cast(const char* buf) {
return reinterpret_cast<const Header*>(buf + 1)->seq; // undefined behaviour
}
// Right: copy the bytes into a real Header object.
std::uint32_t seq_copy(const char* buf) {
Header h;
std::memcpy(&h, buf + 1, sizeof h);
return h.seq; // bytes are in wire order: convert from big-endian before use
}
int main() {
char frame[64] = {0x7e, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x01, 0x00, 0x08};
std::printf("%u %u\n", seq_cast(frame), seq_copy(frame));
}go deeper
Recall that casting a byte buffer to a struct pointer is a classic C++ bug and that copying the bytes into a struct with memcpy is the safe way.
Explain the aliasing direction, alignment, padding and byte order as four separate problems with the in-place cast.
Review a real parser: find the in-place casts, show the misaligned offsets, and replace them with bounds-checked memcpy readers and explicit byte-order conversion.
Decide whether parsing goes through one audited reader library or a schema-generated codec, so no service hand-casts wire buffers again.
## The pattern Parsers written in a hurry do this: ```cpp char buf[1500]; std::size_t n = receive(buf, sizeof buf); auto* h = reinterpret_cast<const Header*>(buf + offset); if (h->type == 7) handle(h->length); ``` It is short, it allocates nothing, and on a typical x86-64 or AArch64 desktop it usually appears to work. It is still wrong in four separate ways, and an interviewer will want all of them. ## Problem 1: no Header object lives there The strict aliasing rule allows an object to be accessed only through its own type, its signed or unsigned variant, or `char`, `unsigned char` or `std::byte`. The buffer contains `char` objects written by the receive call. `h->type` accesses some of those bytes through a `std::uint16_t` glvalue, which is not a permitted type for a `char` object, so the access is **undefined behaviour**. The character-type exception runs one way only: `char` may view other objects, but other types may not view `char` objects. C++20's implicit object creation does not rescue this. It applies when storage is obtained from `malloc`, `operator new` or an `unsigned char` or `std::byte` array, not to a `char` array, and it can never make a misaligned address valid. C++23 added `std::start_lifetime_as<Header>(p)` to begin a `Header`'s lifetime in suitable storage without copying; it requires suitably aligned storage, and libc++ does not ship it yet (libstdc++ 16 does). The lifetime material covers it. ## Problem 2: alignment `alignof(std::uint32_t)` is typically 4. A header starting at `buf + 1`, or after a 14-byte link-layer header, is not 4-aligned. Accessing a misaligned `std::uint32_t` is undefined behaviour; x86-64 and AArch64 usually tolerate it in ordinary loads, other processors raise a bus error, and vectorised code can fault on any of them. UndefinedBehaviorSanitizer reports it as "member access within misaligned address". ## Problem 3: struct layout is not a wire format A struct's layout is the compiler's choice within the ABI: padding may be inserted between members and at the end. A header of `uint8_t`, `uint32_t`, `uint16_t` usually gets three padding bytes after the first member. `#pragma pack` or `[[gnu::packed]]` can remove padding, but they are compiler extensions and create misaligned members of their own. A `static_assert(sizeof(Header) == 8)` at least turns a layout surprise into a compile error. ## Problem 4: byte order Most network protocols send multi-byte integers **big-endian**. On a little-endian host the raw `std::uint32_t` holding the bytes `00 00 00 2a` reads as 704643072, not 42. The value must be converted, with `ntohl` from the sockets API, C++23's `std::byteswap` guarded by `std::endian::native == std::endian::little` (C++20), or by assembling the integer from bytes with shifts, which is endian-independent. ## The robust parser 1. Check that the buffer holds at least `offset + sizeof(Header)` bytes. 2. Declare `Header h;` and `std::memcpy(&h, buf + offset, sizeof h);`. 3. Convert each multi-byte field from network byte order. 4. Validate the fields (lengths, types) before using them. Reviewing an existing parser for this bug comes down to a short list of checks: - every `reinterpret_cast` or C-style cast whose operand is a byte buffer, a `void*` from an I/O call or a pointer into a message; - every struct used both as an in-memory type and as a description of wire bytes; - every offset added to a buffer pointer before a cast, since those are the misaligned cases; - every multi-byte field used without a byte-order conversion; - every read that is not preceded by a check of the received length. Alternatively, read each field separately with `memcpy` at its wire offset, which also frees the struct from having to match the wire layout. Either way the copies are tiny and constant-sized, and optimising compilers emit plain loads. | Concern | Cast in place | memcpy into a local | |---|---|---| | strict aliasing | undefined | defined | | misaligned offset | undefined, may fault | fine: memcpy has no alignment requirement | | padding mismatch | silent misread | caught by `static_assert` or avoided with per-field copies | | byte order | must still be converted | must still be converted | | cost after optimisation | loads | loads |
- Would declaring the buffer as alignas(Header) unsigned char fix the cast?It fixes alignment at offset zero only, and an `unsigned char` array can implicitly create objects, so some argue the cast becomes defined for a header at the very start. Any header at another offset is still misaligned, and padding and byte order are untouched. A `memcpy` into a local is still simpler to defend in review.
- Why prefer assembling integers with shifts over memcpy plus byteswap?`std::uint32_t(b[0]) << 24 | std::uint32_t(b[1]) << 16 | std::uint32_t(b[2]) << 8 | b[3]` over `unsigned char` bytes yields the big-endian value on any host, with no conditional swap and no alignment or aliasing questions; widening first avoids shifting into an `int`'s sign bit. Clang 21 at `-O2` compiles it to one load and one byte-reverse instruction.
- The cast is fine because char may alias any type.
- x86 handles misaligned loads, so alignment never matters.
- A struct with fixed-width members always matches the wire layout.
- memcpy into a local struct is too slow for a packet parser.
- Using #pragma pack makes the in-place cast standard-conforming.