Skip to content

Commit

Permalink
Use strcspn instead of strtok for reentrancy.
Browse files Browse the repository at this point in the history
Also, trim codes so game genie and pro-action replay codes
parse properly.
  • Loading branch information
bearoso committed Dec 29, 2018
1 parent 2b0e4b5 commit ae1477d
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions cheats2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
#include "cheats.h"
#include "bml.h"

static inline char *trim (char *string)
{
int start;
int end;

for (start = 0; string[start] && isspace (string[start]); start++) {}
for (end = start; string[end] && !isspace (string[end]); end++) {}
string[end] = '\0';
return &string[start];
}

static inline uint8 S9xGetByteFree (uint32 Address)
{
int block = (Address & 0xffffff) >> MEMMAP_SHIFT;
Expand Down Expand Up @@ -394,20 +405,23 @@ SCheat S9xTextToCheat (char *text)
SCheatGroup S9xCreateCheatGroup (const char *name, const char *cheat)
{
SCheatGroup g;
char *code;
char *code_string = strdup (cheat);
char *code_ptr = code_string;
int len;

g.name = strdup (name);
g.enabled = false;

for (code = strtok (code_string, "+"); code; code = strtok (NULL, "+"))
for (len = strcspn (code_ptr, "+"); len; len = strcspn (code_ptr, "+"))
{
if (code)
{
SCheat c = S9xTextToCheat (code);
if (c.address)
g.c.push_back (c);
}
char *code = code_ptr;
code_ptr += len + (code_ptr[len] == '\0' ? 0 : 1);
code[len] = '\0';
code = trim (code);

SCheat c = S9xTextToCheat (code);
if (c.address)
g.c.push_back (c);
}

delete[] code_string;
Expand Down

0 comments on commit ae1477d

Please sign in to comment.