Skip to content

Commit 1638bc0

Browse files
committed
saves.cpp: Improve the code consistency of other functions that have a similar file name usage pattern. (Related to commit 6754adf.)
1 parent 6754adf commit 1638bc0

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

desmume/src/saves.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -661,25 +661,27 @@ void scan_savestates()
661661
struct stat sbuf;
662662
#endif
663663

664-
char filename[MAX_PATH + 1];
664+
char filename[MAX_PATH] = {0};
665665
path.getpathnoext(path.STATE_SLOTS, filename);
666666

667667
const size_t filenameLen = strlen(filename);
668-
const size_t extLen = strlen(".ds");
669668
const size_t numberLen = strlen("-2147483648"); // Longest possible string length for a 32-bit int
669+
const size_t extLen = strlen(".ds") + numberLen;
670+
const size_t totalLen = filenameLen + extLen;
670671

671672
clear_savestates();
672673

673-
if ((filenameLen + extLen + numberLen) >= MAX_PATH)
674+
if (totalLen >= MAX_PATH)
674675
{
676+
printf("Scan Save State ERROR: The file path is too long. (path length is %llu bytes, maximum length is %llu bytes)\n", (unsigned long long)totalLen, (unsigned long long)MAX_PATH);
675677
return;
676678
}
677679

678680
char *filenameExt = filename + filenameLen;
679681

680682
for (int i = 0; i < NB_STATES; i++)
681683
{
682-
snprintf(filenameExt, extLen + numberLen, ".ds%d", i);
684+
snprintf(filenameExt, extLen, ".ds%d", i);
683685

684686
#ifdef _MSC_VER
685687
wchar_t wgarbage[1024] = {0};
@@ -697,14 +699,25 @@ void scan_savestates()
697699
void savestate_slot(int num)
698700
{
699701
struct stat sbuf;
700-
char filename[MAX_PATH + 1];
702+
char filename[MAX_PATH] = {0};
701703

702704
lastSaveState = num; //Set last savestate used
703705

704706
path.getpathnoext(path.STATE_SLOTS, filename);
705-
706-
if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ > MAX_PATH) return;
707-
snprintf(filename + strlen(filename), sizeof(filename), ".ds%d", num);
707+
708+
const size_t filenameLen = strlen(filename);
709+
const size_t numberLen = strlen("-2147483648"); // Longest possible string length for a 32-bit int
710+
const size_t extLen = strlen(".ds") + numberLen;
711+
const size_t totalLen = filenameLen + extLen;
712+
713+
if (totalLen >= MAX_PATH)
714+
{
715+
printf("Save State ERROR: The file path is too long. (path length is %llu bytes, maximum length is %llu bytes)\n", (unsigned long long)totalLen, (unsigned long long)MAX_PATH);
716+
return;
717+
}
718+
719+
char *filenameExt = filename + filenameLen;
720+
snprintf(filenameExt, extLen, ".ds%d", num);
708721

709722
if (savestate_save(filename))
710723
{
@@ -731,12 +744,23 @@ void savestate_slot(int num)
731744

732745
void loadstate_slot(int num)
733746
{
734-
char filename[MAX_PATH];
747+
char filename[MAX_PATH] = {0};
735748
int max_index = -1;
736749

737750
lastSaveState = num; //Set last savestate used
738751

739752
path.getpathnoext(path.STATE_SLOTS, filename);
753+
754+
const size_t filenameLen = strlen(filename);
755+
const size_t numberLen = strlen("-2147483648"); // Longest possible string length for a 32-bit int
756+
const size_t extLen = strlen(".ds") + numberLen;
757+
const size_t totalLen = filenameLen + extLen;
758+
759+
if (totalLen >= MAX_PATH)
760+
{
761+
printf("Load State ERROR: The file path is too long. (path length is %llu bytes, maximum length is %llu bytes)\n", (unsigned long long)totalLen, (unsigned long long)MAX_PATH);
762+
return;
763+
}
740764

741765
//save the state before we load the state, to give people a path for recovery in case they hit the wrong key
742766
#ifdef HOST_WINDOWS
@@ -794,9 +818,9 @@ void loadstate_slot(int num)
794818

795819
}
796820
}
797-
798-
if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ > MAX_PATH) return;
799-
snprintf(filename + strlen(filename), sizeof(filename), ".ds%d", num);
821+
822+
char *filenameExt = filename + filenameLen;
823+
snprintf(filenameExt, extLen, ".ds%d", num);
800824

801825
if (savestate_load(filename))
802826
{

0 commit comments

Comments
 (0)