g_game.c: Tidy in anticipation of next commit

- Use WRITESTRINGL and READSTRINGL instead of their N equivalents. These are mostly the same, but the macro length is inclusive of the null terminator, so this prevents one-byte overruns.
- Make sure datatype for iteration over nummapheaders is of sufficient unsigned region
- Make sure hashes are calculated based on final field for guaranteed consistency, not initial buffer
- Additional newlines for spacing
This commit is contained in:
toaster 2023-05-28 20:19:30 +01:00
parent 3e663867b9
commit aa35e249d5

View file

@ -451,9 +451,9 @@ INT32 player_name_changes[MAXPLAYERS];
// MAKE SURE YOU SAVE DATA BEFORE CALLING THIS // MAKE SURE YOU SAVE DATA BEFORE CALLING THIS
void G_ClearRecords(void) void G_ClearRecords(void)
{ {
INT16 i; UINT16 i;
for (i = 0; i < nummapheaders; ++i) for (i = 0; i < nummapheaders; i++)
{ {
memset(&mapheaderinfo[i]->records, 0, sizeof(recorddata_t)); memset(&mapheaderinfo[i]->records, 0, sizeof(recorddata_t));
} }
@ -4947,7 +4947,7 @@ void G_LoadGameData(void)
char mapname[MAXMAPLUMPNAME]; char mapname[MAXMAPLUMPNAME];
INT16 mapnum; INT16 mapnum;
READSTRINGN(save.p, mapname, sizeof(mapname)); READSTRINGL(save.p, mapname, MAXMAPLUMPNAME);
mapnum = G_MapNumber(mapname); mapnum = G_MapNumber(mapname);
recorddata_t dummyrecord; recorddata_t dummyrecord;
@ -4981,7 +4981,7 @@ void G_LoadGameData(void)
// Establish properties, for later retrieval on file add. // Establish properties, for later retrieval on file add.
unloadedmap->lumpname = Z_StrDup(mapname); unloadedmap->lumpname = Z_StrDup(mapname);
unloadedmap->lumpnamehash = quickncasehash(mapname, MAXMAPLUMPNAME); unloadedmap->lumpnamehash = quickncasehash(unloadedmap->lumpname, MAXMAPLUMPNAME);
// Insert at the head, just because it's convenient. // Insert at the head, just because it's convenient.
unloadedmap->next = unloadedmapheaders; unloadedmap->next = unloadedmapheaders;
@ -5004,7 +5004,7 @@ void G_LoadGameData(void)
cupwindata_t dummywindata[4]; cupwindata_t dummywindata[4];
// Find the relevant cup. // Find the relevant cup.
READSTRINGN(save.p, cupname, sizeof(cupname)); READSTRINGL(save.p, cupname, sizeof(cupname));
UINT32 hash = quickncasehash(cupname, MAXCUPNAME); UINT32 hash = quickncasehash(cupname, MAXCUPNAME);
for (cup = kartcupheaders; cup; cup = cup->next) for (cup = kartcupheaders; cup; cup = cup->next)
{ {
@ -5053,7 +5053,7 @@ void G_LoadGameData(void)
// Establish properties, for later retrieval on file add. // Establish properties, for later retrieval on file add.
strlcpy(unloadedcup->name, cupname, sizeof(unloadedcup->name)); strlcpy(unloadedcup->name, cupname, sizeof(unloadedcup->name));
unloadedcup->namehash = quickncasehash(cupname, MAXCUPNAME); unloadedcup->namehash = quickncasehash(unloadedcup->name, MAXCUPNAME);
// Insert at the head, just because it's convenient. // Insert at the head, just because it's convenient.
unloadedcup->next = unloadedcupheaders; unloadedcup->next = unloadedcupheaders;
@ -5190,6 +5190,7 @@ void G_SaveGameData(void)
length += 4 + (numgamedatamapheaders * (MAXMAPLUMPNAME+1+4+4)); length += 4 + (numgamedatamapheaders * (MAXMAPLUMPNAME+1+4+4));
UINT32 numgamedatacups = 0; UINT32 numgamedatacups = 0;
unloaded_cupheader_t *unloadedcup; unloaded_cupheader_t *unloadedcup;
@ -5222,6 +5223,7 @@ void G_SaveGameData(void)
length += 4 + (numgamedatacups * (MAXCUPNAME+4)); length += 4 + (numgamedatacups * (MAXCUPNAME+4));
if (P_SaveBufferAlloc(&save, length) == false) if (P_SaveBufferAlloc(&save, length) == false)
{ {
CONS_Alert(CONS_ERROR, M_GetText("No more free memory for saving game data\n")); CONS_Alert(CONS_ERROR, M_GetText("No more free memory for saving game data\n"));
@ -5322,7 +5324,7 @@ void G_SaveGameData(void)
if (!(mapheaderinfo[i]->records.mapvisited & MV_MAX)) if (!(mapheaderinfo[i]->records.mapvisited & MV_MAX))
continue; continue;
WRITESTRINGN(save.p, mapheaderinfo[i]->lumpname, MAXMAPLUMPNAME); WRITESTRINGL(save.p, mapheaderinfo[i]->lumpname, MAXMAPLUMPNAME);
UINT8 mapvisitedtemp = (mapheaderinfo[i]->records.mapvisited & MV_MAX); UINT8 mapvisitedtemp = (mapheaderinfo[i]->records.mapvisited & MV_MAX);
@ -5347,7 +5349,7 @@ void G_SaveGameData(void)
if (!(unloadedmap->records.mapvisited & MV_MAX)) if (!(unloadedmap->records.mapvisited & MV_MAX))
continue; continue;
WRITESTRINGN(save.p, unloadedmap->lumpname, MAXMAPLUMPNAME); WRITESTRINGL(save.p, unloadedmap->lumpname, MAXMAPLUMPNAME);
WRITEUINT8(save.p, unloadedmap->records.mapvisited); WRITEUINT8(save.p, unloadedmap->records.mapvisited);
@ -5383,7 +5385,7 @@ void G_SaveGameData(void)
if (cup->windata[0].best_placement == 0) if (cup->windata[0].best_placement == 0)
continue; continue;
WRITESTRINGN(save.p, cup->name, MAXCUPNAME); WRITESTRINGL(save.p, cup->name, MAXCUPNAME);
WRITECUPWINDATA(cup); WRITECUPWINDATA(cup);
@ -5398,7 +5400,7 @@ void G_SaveGameData(void)
if (unloadedcup->windata[0].best_placement == 0) if (unloadedcup->windata[0].best_placement == 0)
continue; continue;
WRITESTRINGN(save.p, unloadedcup->name, MAXCUPNAME); WRITESTRINGL(save.p, unloadedcup->name, MAXCUPNAME);
WRITECUPWINDATA(unloadedcup); WRITECUPWINDATA(unloadedcup);