Limit substring length in NiHeader::SetExportInfo #44
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.
When calling SetExportInfo with a string shorter than 255 to set only exportInfo1, the function works as expected. However, issues occur when attempting to set each member according to the comment above the declaration or when passing a string larger than 254 characters.
If one passes a string resized to 3 * 256 with the desired export items at position 0, 256, and 512 into SetExportInfo, the header's exportInfo members are set to 256 character strings containing the provided data, which appears correct initially. The issue occurs when NiString::Write is called on the export items; The string length is cast to a uint8_t and stored. The string is resized using the value of that integer, but since the string was set to 256 characters of the source by SetExportInfo and a uint8_t can't be larger than 255, the string is resized to 0. SetNullOutput is called with its default parameter and thus is set to true for these members so the uint8_t is incremented to 1. This is then written to the file along with the null terminator. This results in a nif with valid but empty export info items.
If the input string is set to one character shorter than a multiple of 256, the resulting nif will fail to load since the last exportInfo item to be set has a size of 255 during writing. The length is incremented to account for the null terminator, writing 0 for the size while the full string and null terminator are still written to the file.
Since a null terminated string whose size is stored as an 8-bit integer can only be 254 characters long, not including the null terminator, and since NiString::Write handles this logic by incrementing the size and writing a null terminator, NiHeader::SetExportInfo should only take a maximum of 254 characters from each position in the input string.
Edit: I was referring to SetExportInfo as SetExportItems for some reason