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

Fix memory corruption on removal / update of cheats with the same address hash #570

Merged
merged 1 commit into from
Oct 22, 2023

Conversation

max-m
Copy link
Contributor

@max-m max-m commented Oct 22, 2023

Previously instead of resizing the GB_cheat_hash_t to fit the new (smaller) number of cheats a new chunk of (uninitialized) memory was used.
Now we shrink the GB_cheat_hash_t by using realloc instead.

I have tested this change by:

  1. Applying the following patch:
diff --git a/SDL/main.c b/SDL/main.c
index 5b2164d9..0d2710d9 100644
--- a/SDL/main.c
+++ b/SDL/main.c
@@ -805,6 +805,15 @@ restart:
     }
     else {
         GB_load_rom(&gb, filename);
+
+        GB_add_cheat(&gb, "Cheat #0", 0x0000, 0x00, 0x12, 0x34, false, false);
+        GB_add_cheat(&gb, "Cheat #1", 0x3456, 0x12, 0x78, 0x90, true, true);
+        GB_add_cheat(&gb, "Cheat #2", 0x3456, 0x12, 0x78, 0x90, false, true);
+        GB_add_cheat(&gb, "Cheat #3", 0x3456, 0x12, 0x78, 0x90, true, false);
+        GB_add_cheat(&gb, "Cheat #4", 0x0995, 0xFF, 0xab, 0x42, false, true);
+        GB_add_cheat(&gb, "Cheat #5", 0xcdef, 0xab, 0x00, 0x00, false, true);
+        GB_add_cheat(&gb, "Cheat #6", 0x0000, 0x00, 0x00, 0x00, false, false);
+        GB_add_cheat(&gb, "Cheat #7", 0xffff, 0xff, 0xff, 0xff, true, true);
     }
     
     /* Configure battery */
  1. Loading a game
  2. Toggling a cheat so the cheat database gets saved to the file system
  3. Loading the same game again

Previously step 4 would have crashed:

[New Thread 0x7ffff6442700 (LWP 182590)]

SameBoy v0.15.8ffff57b2700 (LWP 182591)]
[New Thread 0x7fffeddaf700 (LWP 182646)]
[New Thread 0x7fffedd6e700 (LWP 182647)]
> 
Thread 1 "sameboy" received signal SIGSEGV, Segmentation fault.
0x0000555555567ad3 in GB_remove_cheat ()
(gdb) bt
#0  0x0000555555567ad3 in GB_remove_cheat ()
#1  0x0000555555568532 in GB_load_cheats ()
#2  0x00005555555ab394 in run ()
#3  0x00005555555aa66d in main ()

@LIJI32 LIJI32 merged commit aff42af into LIJI32:master Oct 22, 2023
@max-m max-m deleted the fix-cheats branch October 22, 2023 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants