Skip to content
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

Fix memory issues #16

Merged
merged 6 commits into from
Apr 23, 2024
Merged

Fix memory issues #16

merged 6 commits into from
Apr 23, 2024

Conversation

higaski
Copy link
Contributor

@higaski higaski commented Mar 13, 2024

As previously mentioned the DCCEXProtocol library has various memory issues (UB and leaks) which should be taken care of.

For now, let's turn on the sanitizer.

@github-actions github-actions bot added the DCCEXProtocol Item relates to the DCCEXProtocol Arduino library specifically label Mar 13, 2024
@@ -8,8 +8,8 @@ file(GLOB_RECURSE SRC src/*.cpp)
add_library(DCCEXProtocol STATIC ${SRC})
add_library(DCCEX::Protocol ALIAS DCCEXProtocol)

# Requires at least C++11
target_compile_features(DCCEXProtocol PUBLIC cxx_std_11)
# Stuck at C++11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the C++ standard to 11

@@ -18,4 +18,4 @@ jobs:
CC: gcc-13
CXX: g++-13
- run: cmake --build build --parallel --target DCCEXProtocolTests
- run: ctest --test-dir build --schedule-random
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctest swallows errors thrown by LeakSanitizer, call test executable directly


// Public methods

void DCCEXInbound::setup(int16_t maxParameterValues) {
_parameterValues = (int32_t *)malloc(maxParameterValues * sizeof(int32_t));
_parameterValues = (int32_t *)realloc(_parameterValues, maxParameterValues * sizeof(int32_t));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to realloc in case this function gets called multiple times

@@ -62,6 +62,14 @@ DCCEXProtocol::DCCEXProtocol(int maxCmdBuffer) {
_bufflen = 0;
}

DCCEXProtocol::~DCCEXProtocol() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add destructor to cleanup memory

versionStartAt = versionStartAt + strlen(temp) + 1;
temp = _nextServerDescriptionParam(description, versionStartAt, true);
_patchVersion = atoi(temp);
char *description{DCCEXInbound::getText(0) + 7};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New "inplace" version parsing which does not create unnecessary copies

}
_tempString[i] = '\0';
char *_result;
_result = (char *)malloc(strlen(_tempString));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line caused an out of bounce access by the way

@higaski
Copy link
Contributor Author

higaski commented Mar 14, 2024

Suggestion for API-changes:

  • Every method taking a char* should copy that string if it wants to keep it around.

@higaski higaski marked this pull request as ready for review March 17, 2024 13:58
@higaski higaski changed the title WIP: Fix memory issues Fix memory issues Mar 17, 2024
@higaski
Copy link
Contributor Author

higaski commented Mar 17, 2024

Suggestion for API-changes:

* Every method taking a char* should copy that string if it wants to keep it around.

Ok, not sure if that's possible without way more effort. I will leave it like it is for now.
The code still leaks if any of the lists get updated though.

@peteGSX peteGSX merged commit 2019810 into DCC-EX:main Apr 23, 2024
3 checks passed
@higaski higaski deleted the fix_memory_issues branch April 24, 2024 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DCCEXProtocol Item relates to the DCCEXProtocol Arduino library specifically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants