@@ -24,7 +24,7 @@ ByteVector ByteVectorFromString(const char* s) {
2424 char * s1 = strdup (s);
2525 char * p = strtok (s1, " \\ x " );
2626 while (p) {
27- uint8_t byte = strtol (p, nullptr , 16 );
27+ uint8_t byte = static_cast < uint8_t >( strtol (p, nullptr , 16 ) );
2828 payload.push_back (byte);
2929 p = strtok (nullptr , " \\ x " );
3030 }
@@ -141,26 +141,79 @@ SMCResult MemPatchGameConfig::ReadSMC_KeyValue(const SMCStates *states, const ch
141141 {
142142 case PState_Runtime:
143143 if (!strcmp (key, " signature" )) {
144- g_CurrentPatchInfo->signature = value;
144+ if (value && value[0 ] != ' \0 ' ) {
145+ g_CurrentPatchInfo->signature = value;
146+ } else {
147+ smutils->LogError (myself, " Error: empty signature in patch \" %s\" at line %i col %i" , \
148+ g_CurrentSection.c_str (), states->line , states->col );
149+ return SMCResult_HaltFail;
150+ }
145151 } else if (!strcmp (key, " offset" )) {
146- // Support for IDA-style address offsets
147- if (value[strlen (value)-1 ] == ' h' ) {
148- g_CurrentPatchInfo->offset = (size_t ) strtol (value, nullptr , 16 );
152+ // size_t - supports only unsigned int
153+ std::string s (value);
154+ char * end = nullptr ;
155+
156+ if (s.empty ()) {
157+ smutils->LogError (myself, " Error: empty offset in patch \" %s\" at line %i col %i" , \
158+ g_CurrentSection.c_str (), states->line , states->col );
159+ return SMCResult_HaltFail;
149160 } else {
150- g_CurrentPatchInfo->offset = atoi (value);
161+ if (s.back () == ' h' || s.back () == ' H' ) {
162+ // IDA-style hex: "10h"
163+ s.pop_back ();
164+ g_CurrentPatchInfo->offset = strtoul (s.c_str (), &end, 16 );
165+ } else if (s.rfind (" 0x" , 0 ) == 0 || s.rfind (" 0X" , 0 ) == 0 ) {
166+ // C-style hex: "0x10"
167+ g_CurrentPatchInfo->offset = strtoul (s.c_str (), &end, 16 );
168+ } else {
169+ // Decimal
170+ g_CurrentPatchInfo->offset = strtoul (s.c_str (), &end, 10 );
171+ }
172+
173+ if (*end != ' \0 ' ) {
174+ smutils->LogError (myself, " Error: Invalid offset value \" %s\" at line %i col %i" ,\
175+ value, states->line , states->col );
176+ return SMCResult_HaltFail;
177+ }
151178 }
152179 } else if (!strcmp (key, " patch" )) {
153180 g_CurrentPatchInfo->vecPatch = ByteVectorFromString (value);
181+
182+ if (g_CurrentPatchInfo->vecPatch .size () < 1 ) {
183+ smutils->LogError (myself, " Error: empty patch section in patch \" %s\" at line %i col %i" ,
184+ g_CurrentSection.c_str (), states->line , states->col );
185+ return SMCResult_HaltFail;
186+ }
154187 } else if (!strcmp (key, " verify" )) {
155188 g_CurrentPatchInfo->vecVerify = ByteVectorFromString (value);
189+
190+ if (g_CurrentPatchInfo->vecVerify .size () < 1 ) {
191+ smutils->LogError (myself, " Error: empty verify section in patch \" %s\" at line %i col %i" ,
192+ g_CurrentSection.c_str (), states->line , states->col );
193+ return SMCResult_HaltFail;
194+ }
156195 } else if (!strcmp (key, " preserve" )) {
157196 g_CurrentPatchInfo->vecPreserve = ByteVectorFromString (value);
197+
198+ if (g_CurrentPatchInfo->vecPreserve .size () < 1 ) {
199+ smutils->LogError (myself, " Error: empty preserve section in patch \" %s\" at line %i col %i" ,
200+ g_CurrentSection.c_str (), states->line , states->col );
201+ return SMCResult_HaltFail;
202+ }
203+ } else {
204+ // Force users to check their gamedata in case something is incorrect.
205+ // It's possible that the user made a typo, and the extension doesn't display errors;
206+ // This has been verified in practice.
207+ smutils->LogError (myself, " Error: unknown key \" %s\" in patch \" %s\" at line %i col %i" ,
208+ key, g_CurrentSection.c_str (), states->line , states->col );
209+ return SMCResult_HaltFail;
158210 }
159211 break ;
160212 default :
161- smutils->LogError (myself, " Unknown key in MemPatches section \" %s\" : line: %i col: %i" , key, states->line , states->col );
213+ smutils->LogError (myself, " Error: Unknown key in MemPatches section \" %s\" : line: %i col: %i" , key, states->line , states->col );
162214 return SMCResult_HaltFail;
163215 }
216+
164217 return SMCResult_Continue;
165218}
166219
0 commit comments