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

String copy util method and fix Save Manager string copy overflows #3274

Merged
merged 9 commits into from
Oct 27, 2023

Conversation

Archez
Copy link
Contributor

@Archez Archez commented Oct 9, 2023

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 to SohUtils::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 method LoadCharArray() 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

Copy link
Contributor

@garrettjoecox garrettjoecox left a comment

Choose a reason for hiding this comment

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

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)

I think method names and SaveManager change look good, obvious what they are supposed to do and not do 👍

@garrettjoecox
Copy link
Contributor

Conflict here, otherwise ready for merge

@Archez Archez merged commit fd09a12 into HarbourMasters:develop Oct 27, 2023
@Archez Archez deleted the better-string-copies branch November 5, 2023 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants