Fix memory overwriting by EnumToString & StringToEnum #4089
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.
Fixes #4078
Based on PR #4085, it was determined that the strange memory overwriting errors after using EnumToString / StringToEnum are a result of passing a type smaller than 4 bytes, causing the remaining memory to be overwritten (thanks @Merlin for solving this puzzle, although it's simple, I didn't think of it for a moment why the memory was magically overwritten).
The solution is to get rid of casting to a 4-byte size and dynamically determine the type and its size. Tested on several different variants, and I haven't detected any additional issues related to this PR.
I had to modify several enums because now a given value in ADD_ENUM must be associated with the enum provided in IMPLEMENT_ENUM_BEGIN and must be of the same type due to templating. I leave IMPLEMENT_ENUM_CLASS, etc., for compatibility (too many places in the code use it, so I didn't want to change it to IMPLEMENT_ENUM now).
Advanced template methods and complex (hard-to-read) macros are not my strong suit, and I don't claim that this solution is the best possible, but I tried to change as little as possible and maintain compatibility with the current code, and most importantly, as I mentioned – it works, and I haven't noticed any issues.