Skip to content

Commit 6754adf

Browse files
committed
saves.cpp: Add a bit more clarity to what scan_savestates() is actually doing, which is to concatenate a file extension to a base file name, and then use the resultant file name for a check.
1 parent 9f009f4 commit 6754adf

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

desmume/src/saves.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,15 +662,24 @@ void scan_savestates()
662662
#endif
663663

664664
char filename[MAX_PATH + 1];
665+
path.getpathnoext(path.STATE_SLOTS, filename);
666+
667+
const size_t filenameLen = strlen(filename);
668+
const size_t extLen = strlen(".ds");
669+
const size_t numberLen = strlen("-2147483648"); // Longest possible string length for a 32-bit int
665670

666671
clear_savestates();
672+
673+
if ((filenameLen + extLen + numberLen) >= MAX_PATH)
674+
{
675+
return;
676+
}
677+
678+
char *filenameExt = filename + filenameLen;
667679

668680
for (int i = 0; i < NB_STATES; i++)
669681
{
670-
path.getpathnoext(path.STATE_SLOTS, filename);
671-
672-
if (strlen(filename) + strlen(".dst") + strlen("-2147483648") /* = biggest string for i */ > MAX_PATH) return;
673-
snprintf(filename + strlen(filename), sizeof(filename), ".ds%d", i);
682+
snprintf(filenameExt, extLen + numberLen, ".ds%d", i);
674683

675684
#ifdef _MSC_VER
676685
wchar_t wgarbage[1024] = {0};
@@ -683,8 +692,6 @@ void scan_savestates()
683692
strncpy(savestates[i].date, format_time(sbuf.st_mtime), 40);
684693
savestates[i].date[40 - 1] = '\0';
685694
}
686-
687-
return;
688695
}
689696

690697
void savestate_slot(int num)

0 commit comments

Comments
 (0)