Skip to content

PR: Fix memory corruption and undefined behavior in cheat sequence handling#41

Open
ThomasHumper wants to merge 1 commit into
id-Software:masterfrom
ThomasHumper:patch-1
Open

PR: Fix memory corruption and undefined behavior in cheat sequence handling#41
ThomasHumper wants to merge 1 commit into
id-Software:masterfrom
ThomasHumper:patch-1

Conversation

@ThomasHumper
Copy link
Copy Markdown

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

  1. Memory corruption in cht_GetParam
    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant