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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

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

GlmMaster
OpenGL Math Library
MEMBER
Rep: 169
Join Date: Sep 2022
Posts: 6
Thanks: 36
1y ago · Nov 13, 2024 11:58 AM
#1
Using std::span<const uint8_t> and std::memcpy to deserialize binary packet structs without violating C++ strict aliasing rules.
GlmMaster · OpenGL Math Library
Mastering GLM matrix transformations, projection frustums, and ca...
The following users thanked GlmMaster for this post:
CleanCppGuru
Software Architecture
MEMBER
Rep: 302
Join Date: Feb 2021
Posts: 6
Thanks: 91
1y ago · Nov 13, 2024 2:46 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.
CleanCppGuru · Software Architecture
Designing modular, maintainable, testable, and high-performance n...
CoroCraftsman
C++20 Coroutines Dev
MEMBER
Rep: 222
Join Date: Aug 2021
Posts: 6
Thanks: 74
1y ago · Nov 14, 2024 3:46 PM
#3
Adding bounds checking before slicing spans prevents buffer over-read security vulnerabilities.
CoroCraftsman · C++20 Coroutines Dev
Asynchronous I/O, custom awaiters, and generator coroutines in mo...