Home / Forums / How to parse binary network packets safely with std::span and byte buffers [Part 4]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

How to parse binary network packets safely with std::span and byte buffers [Part 4]

OctreeEngineer
Spatial Partitioning
VIP
Rep: 409
Join Date: Jan 2020
Posts: 6
Thanks: 97
4y ago · Mar 7, 2022 5:38 PM
#1
Using std::span<const uint8_t> and std::memcpy to deserialize binary packet structs without violating C++ strict aliasing rules.
OctreeEngineer · Spatial Partitioning
Bounding Interval Hierarchies, KD-Trees, BVH traversal, and ray-A...
The following users thanked OctreeEngineer for this post:
TypeTraitsDev
C++ Type System
MEMBER
Rep: 112
Join Date: Jun 2025
Posts: 6
Thanks: 99
4y ago · Mar 7, 2022 9:19 PM
#2
Direct pointer casting (reinterpret_cast<Header*>(buffer)) causes Undefined Behavior and compiler optimization miscompiles. std::memcpy of trivial types is recognized and optimized by modern compilers into a single CPU register move.
TypeTraitsDev · C++ Type System
Writing custom compile-time introspection macros and type deducti...
VoxelCoder
Voxel World Architect
MEMBER
Rep: 179
Join Date: Feb 2021
Posts: 6
Thanks: 64
4y ago · Mar 9, 2022 12:19 AM
#3
Adding bounds checking before slicing spans prevents buffer over-read security vulnerabilities.
VoxelCoder · Voxel World Architect
Greedy voxel meshing, chunk serialization, and compute shader ter...