PR: Fix memory corruption and undefined behavior in cheat sequence handling#41
Open
ThomasHumper wants to merge 1 commit into
Open
PR: Fix memory corruption and undefined behavior in cheat sequence handling#41ThomasHumper wants to merge 1 commit into
ThomasHumper wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR: Fix memory corruption and undefined behavior in cheat sequence handling
Summary
This PR fixes critical bugs in the cheat sequence system that could lead to memory corruption, undefined behavior, and unstable cheat parsing. The changes make cht_CheckCheat and cht_GetParam safer and non-destructive while preserving original behavior.
🐛 Issues fixed
The original implementation modified cht->sequence while reading it
This line was destructive:
*(p++) = 0;
Result: cheat sequences were being overwritten during parameter extraction, breaking future cheat checks.
2. Unsafe buffer handling
cht_GetParam could fail to null-terminate buffer if the expected end marker (0xff) was missing.
This could lead to string over-reads and undefined behavior.
3. Fragile pointer traversal
Both functions assumed valid sentinel markers (1 and 0xff) exist in all cases.
Missing validation could lead to out-of-bounds reads in malformed or unexpected inputs.
🔧 Changes made
cht_CheckCheat
Preserves original logic
Adds explicit casting safety for translation table initialization
No behavioral changes intended
cht_GetParam
Removed destructive write to cht->sequence
Rewrote parsing to be read-only
Added safe early exit if start marker (1) is not found
Ensured always null-terminated output string
Improved pointer bounds safety
🧠 Behavior preservation
Cheat detection logic remains unchanged
Cheat sequences still use:
1 as parameter start marker
0xff as end marker
No change in external cheat functionality
🛡️ Safety improvements
Eliminates in-place mutation of cheat sequence buffer
Prevents undefined string reads
Improves robustness against malformed sequences
Reduces risk of engine instability in edge cases
✔️ Testing notes
Verified cheat sequences still trigger correctly
Verified parameter extraction works for valid sequences
Verified safe behavior when markers are missing or malformed
📌 Impact
Low-level subsystem fix with high stability impact. Recommended for all downstream builds and ports relying on original DOOM cheat handling logic.