-
-
Notifications
You must be signed in to change notification settings - Fork 598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: replace C-style array with std::array for buffer management #2914
Merged
dudantas
merged 15 commits into
main
from
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
Sep 29, 2024
Merged
refactor: replace C-style array with std::array for buffer management #2914
dudantas
merged 15 commits into
main
from
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
Sep 29, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This refactors the buffer management from using a C-style array to `std::vector<uint8_t>` in the `NetworkMessage` and `OutputMessage` classes. The modification enhances memory safety, simplifies buffer handling, and reduces the risk of buffer overflows. This change also aligns with modern C++ best practices by using STL containers instead of raw pointers.
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 25, 2024 21:43
1318c31
to
3cb75d8
Compare
Qodana for C/C++1020 new problems were found
💡 Qodana analysis was run in the pull request mode: only the changed files were checked View the detailed Qodana reportTo be able to view the detailed Qodana report, you can either:
To get - name: 'Qodana Scan'
uses: JetBrains/[email protected]
with:
upload-result: true Contact Qodana teamContact us at [email protected]
|
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 25, 2024 22:54
2448486
to
3d609bb
Compare
…ad of memcpy improve: memory management from memcpy/memset to std::ranges::copy and std::fill fix: compile erros fix: wrong functions fix: ubuntu warnings
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 26, 2024 22:24
b829686
to
fad17e5
Compare
dudantas
changed the title
refactor: replace C-style array with std::vector for buffer management
refactor: replace C-style array with std::array for buffer management
Sep 26, 2024
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 26, 2024 23:06
e707ef9
to
6d308f6
Compare
This was what caused the crash, it added a uint32_t to a string and in some scenarios it caused overflow: [2024-26-09 22:05:03.669] [error] [NetworkMessage::getString] not enough data to read string of length: 65002. Called line 3049:30 in void __cdecl ProtocolGame::parseDebugAssert(class NetworkMessage &)
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 27, 2024 01:54
95b9165
to
7e21aef
Compare
dudantas
force-pushed
the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
from
September 27, 2024 11:32
71a0e26
to
ec6f23a
Compare
dudantas
deleted the
dudantas/refactor-networkmessage-buffer-from-c-array-to-vector
branch
September 29, 2024 01:45
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request refactors the buffer management from using C-style arrays to
std::array<uint8_t, NETWORKMESSAGE_MAXSIZE>
in theNetworkMessage
andOutputMessage
classes. The modification enhances memory safety, ensures compile-time size checks, and improves code clarity by leveraging modern C++ best practices. Transitioning tostd::array
offers fixed-size buffer management with the benefits of type safety and better integration with the Standard Template Library (STL).Additionally, this update includes several other improvements:
Removal of
memcpy
in Favor ofstd::ranges::copy
: All instances ofmemcpy
have been replaced withstd::ranges::copy
, providing a safer and more expressive way to copy data between containers while maintaining type safety and avoiding manual pointer arithmetic.Replacement of
memset
withstd::fill
: The usage ofmemset
for buffer initialization and padding has been replaced withstd::fill
, ensuring a more readable and type-safe approach to filling buffers with specific values.Elimination of
reinterpret_cast
: Instances ofreinterpret_cast
used to convert data types have been removed. For example, string extraction from the buffer now uses direct string construction:std::string result(buffer.begin() + info.position, buffer.begin() + info.position + stringLen);
Introduce the use of std::source_location: The addString and getString functions, along with an optional function parameter. These enhancements improve logging by providing more contextual information about where and how the addString function is invoked, whether from C++ code or Lua scripts. This dual-parameter approach ensures flexibility and more precise debugging information.
Fixed bug in the "decodeHeader" function and added unit tests
Behaviour
Actual
addString
relies solely onstd::source_location
, limiting contextual information when invoked from Lua scripts.Expected
std::array<uint8_t, NETWORKMESSAGE_MAXSIZE>
, which provides compile-time size checks and prevents buffer overflows through safer access methods.std::array
simplifies the code by eliminating the need for manual index management and reducing the risk of memory leaks and undefined behavior.addString
function continues to utilizestd::source_location
and an optionalfunction
parameter to enhance logging, providing detailed contextual information whether called from C++ or Lua scripts.addString
invocations.Possible fixes this crash:
stack_smashing.log
Edit:
I confirmed that this fixed the crash, the problem was that a uint32_t was being added to a getString, which potentially caused an overflow, I was able to find out due to a log that was given and I saw that the header changed to something from the store: cfae88a
Type of change
How Has This Been Tested
The changes were tested by executing the following scenarios to ensure that buffer management and logging behaviors operate as expected:
std::array<uint8_t, NETWORKMESSAGE_MAXSIZE>
.addString
logs correctly capture contextual information using bothstd::source_location
and the optionalfunction
parameter.addString
with and without thefunction
parameter, and that logs reflect the appropriate context.Checklist
Benefits of Switching to
std::array
from C-Style Arrays1. Compile-Time Size Checking
std::array
provides compile-time size information, ensuring that buffer sizes are consistent and reducing the likelihood of buffer overflows that are common with C-style arrays.2. Improved Memory Safety
std::array
offers member functions likeat()
that throw exceptions on out-of-bounds access, enhancing memory safety.std::array
does not decay to pointers implicitly, preventing unintended pointer arithmetic and related bugs.3. Better Integration with STL Algorithms
std::array
seamlessly integrates with STL algorithms, allowing for more expressive and concise code. Functions likestd::copy
,std::fill
, and range-based for loops work naturally withstd::array
.std::copy(value.begin(), value.end(), buffer.begin() + info.position);
4. Enhanced Readability and Maintainability
Using
std::array
makes the intention of fixed-size buffer management explicit, improving code readability and maintainability. It clearly communicates that the buffer size is fixed and known at compile time.5. No Dynamic Memory Allocation Overhead
std::array` provides a fixed-size buffer at compile-time, ensuring that no dynamic memory allocation or reallocation occurs during its lifetime.