String copy util method and fix Save Manager string copy overflows #3274
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.
This PR is attempting to consolidate patterns for copying strings to char arrays safely. The problem was that in the SaveManager we were doing a memcpy and using
string.length()
which under-reports the size of the corresponding.c_str()
(missing the null terminator). This would lead to situations were if a longer string was copied first, then a shorter string, the null terminator would be missing and so extra characters after the shorter string would be seen from the first longer string.A separate solution for string copying using
strncpy
and null terminator handling was done in the spoiler log parsing for hint text, but was repetitive. I have promoted this pattern toSohUtils::CopyStringToCharArray
so that all the different locations in our code can depend on the same solution.Various string copies in the spoiler log parsing, SaveManager context handling, and some gameplay stats are now using this utility method or a helper method from the SaveManager class. In the case of the SaveManager, due to the pattern of
LoadData()
needing to assign to a pointer, we still needed string intermediate variables before passing into the copy method, so I created a new helper methodLoadCharArray()
then manages the string intermediate var and does the string copy together.Also
FormatJsonHintText
was called for all the hint text, when really all it does is inject item icons for the child/adult alter hints, so I've removed the unnecessary calls.Not sure if
SohUtils
feels like the right place for a helper method like, but it fits in my mind as it is technically a utility.Looking for feedback on the two method names:
SohUtils::CopyStringToCharArray
SaveManager::LoadCharArray()
And feedback if we even like
LoadCharArray
or would rather prefer to drop it in favor of managing the string intermediate values like before (this can be seen by looking at this commit diff)Build Artifacts