Completely rewrite how joined IPs are saved and written... again

- Use strlcpy instead of strcpy to prevent footguns
- Use fprintf instead of raw fputs
- Enforce consistent MAX_LOGIP (255) length for each component instead of a combination of magic number 255 and MAXSTRINGLENGTH (32!?) depending on where you look
- Fix fun memory leak in waiting
- Avoid pointless strlens
This commit is contained in:
toaster 2022-09-26 20:59:00 +01:00
parent dfce863449
commit 52f0217bac
5 changed files with 37 additions and 44 deletions

View file

@ -2425,7 +2425,7 @@ static void Command_connect(void)
// Last IPs joined:
// Keep the address we typed in memory so that we can save it if we *succesfully* join the server
strcpy(joinedIP, COM_Argv(1));
strlcpy(joinedIP, COM_Argv(1), MAX_LOGIP);
}
else
{

View file

@ -2417,7 +2417,7 @@ void M_DrawMPJoinIP(void)
case IT_STRING:
{
char str[MAXSTRINGLENGTH];
char str[MAX_LOGIP];
strcpy(str, currentMenu->menuitems[i].text);
// The last 3 options of this menu are to be the joined IP addresses...
@ -2430,9 +2430,9 @@ void M_DrawMPJoinIP(void)
}
if (joinedIPlist[index][1][0]) // Try drawing server name
strcpy(str, joinedIPlist[index][1]);
strlcpy(str, joinedIPlist[index][1], MAX_LOGIP);
else if (joinedIPlist[index][0][0]) // If that fails, get the address
strcpy(str, joinedIPlist[index][0]);
strlcpy(str, joinedIPlist[index][0], MAX_LOGIP);
else
strcpy(str, "---"); // If that fails too then there's nothing!
}

View file

@ -3875,7 +3875,7 @@ boolean M_JoinIPInputs(INT32 ch)
M_SetMenuDelay(pid);
// Is there an address at this part of the table?
if (strlen(joinedIPlist[index][0]))
if (*joinedIPlist[index][0])
M_JoinIP(joinedIPlist[index][0]);
else
S_StartSound(NULL, sfx_lose);

View file

@ -179,8 +179,8 @@ boolean takescreenshot = false; // Take a screenshot this tic
moviemode_t moviemode = MM_OFF;
char joinedIPlist[NUMLOGIP][2][255];
char joinedIP[255];
char joinedIPlist[NUMLOGIP][2][MAX_LOGIP];
char joinedIP[MAX_LOGIP];
// This initializes the above array to have NULL evrywhere it should.
void M_InitJoinedIPArray(void)
@ -198,7 +198,7 @@ void M_AddToJoinedIPs(char *address, char *servname)
UINT8 i = 0;
// Check for dupes...
for (i = 0; i < NUMLOGIP-1; i++)
for (i = 0; i < NUMLOGIP-1; i++) // intentionally not < NUMLOGIP
{
// Check the addresses
if (strcmp(joinedIPlist[i][0], address) == 0)
@ -212,13 +212,13 @@ void M_AddToJoinedIPs(char *address, char *servname)
// Start by moving every IP up 1 slot (dropping the last IP in the table)
for (; i; i--)
{
strcpy(joinedIPlist[i][0], joinedIPlist[i-1][0]);
strcpy(joinedIPlist[i][1], joinedIPlist[i-1][1]);
strlcpy(joinedIPlist[i][0], joinedIPlist[i-1][0], MAX_LOGIP);
strlcpy(joinedIPlist[i][1], joinedIPlist[i-1][1], MAX_LOGIP);
}
// and add the new IP at the start of the table!
strcpy(joinedIPlist[0][0], address);
strcpy(joinedIPlist[0][1], servname);
strlcpy(joinedIPlist[0][0], address, MAX_LOGIP);
strlcpy(joinedIPlist[0][1], servname, MAX_LOGIP);
}
// ==========================================================================
@ -471,34 +471,24 @@ void M_SaveJoinedIPs(void)
{
FILE *f = NULL;
UINT8 i;
char *filepath;
const char *filepath = va("%s"PATHSEP"%s", srb2home, IPLOGFILE);
if (!strlen(joinedIPlist[0][0]))
if (!*joinedIPlist[0][0])
return; // Don't bother, there's nothing to save.
// append srb2home to beginning of filename
// but check if srb2home isn't already there, first
if (!strstr(IPLOGFILE, srb2home))
filepath = va(pandf,srb2home, IPLOGFILE);
else
filepath = Z_StrDup(IPLOGFILE);
f = fopen(filepath, "w");
if (f == NULL)
return; // Uh I guess you don't have disk space?????????
for (i=0; i < NUMLOGIP; i++)
if (!f)
{
if (strlen(joinedIPlist[i][0]))
{
char savestring[MAXSTRINGLENGTH];
strcpy(savestring, joinedIPlist[i][0]);
strcat(savestring, IPLOGFILESEP);
strcat(savestring, joinedIPlist[i][1]);
CONS_Alert(CONS_WARNING, "Could not save recent IP list into %s\n", IPLOGFILE);
return;
}
fputs(savestring, f);
fputs("\n", f); // Because this won't do it automatically now will it...
for (i = 0; i < NUMLOGIP; i++)
{
if (*joinedIPlist[i][0])
{
fprintf(f, "%s%s%s\n", joinedIPlist[i][0], IPLOGFILESEP, joinedIPlist[i][1]);
}
}
@ -513,7 +503,7 @@ void M_LoadJoinedIPs(void)
UINT8 i = 0;
char *filepath;
char *s;
char content[255]; // 255 is more than long enough!
char buffer[2*(MAX_LOGIP+1)];
filepath = va("%s"PATHSEP"%s", srb2home, IPLOGFILE);
f = fopen(filepath, "r");
@ -521,27 +511,30 @@ void M_LoadJoinedIPs(void)
if (f == NULL)
return; // File doesn't exist? sure, just do nothing then.
while (fgets(content, 255, f) && i < NUMLOGIP && content[0] && content[0] != '\n') // Don't let us write more than we can chew!
for (i = 0; fgets(buffer, (int)sizeof(buffer), f); i++) // Don't let us write more than we can chew!
{
if (i >= NUMLOGIP)
break;
// Now we have garbage under the form of "address;string"
// Now you might ask yourself, but what do we do if the player fucked with their file and now there's a bunch of garbage?
// ...Well that's not my problem lol.
if (!*buffer || *buffer == '\n')
break;
s = strtok(content, IPLOGFILESEP); // We got the address
strcpy(joinedIPlist[i][0], s);
s = strtok(buffer, IPLOGFILESEP); // We got the address
strlcpy(joinedIPlist[i][0], s, MAX_LOGIP);
s = strtok(NULL, IPLOGFILESEP); // Let's get rid of this awful \n while we're here!
if (s)
{
UINT16 j = 1;
//strcpy(joinedIPlist[i][1], s); -- get rid of \n too...
char *c = joinedIPlist[i][1];
while (*s && *s != '\n')
while (*s && *s != '\n' && j < MAX_LOGIP)
{
*c = *s;
s++;
c++;
j++;
}
*c = '\0';
}

View file

@ -46,15 +46,15 @@ void M_StopMovie(void);
#define IPLOGFILE "ringsavedips.txt"
#define IPLOGFILESEP ";"
#define NUMLOGIP 3
#define MAX_LOGIP 255
// Array where we'll store addresses to display for last servers joined
// {address, servame}
// 255 is long enough to store the text
extern char joinedIPlist[NUMLOGIP][2][255];
extern char joinedIPlist[NUMLOGIP][2][MAX_LOGIP];
// Keep the address we're joining in mind until we've finished joining.
// Since we don't wanna add an IP address we aren't even sure worked out.
extern char joinedIP[255];
extern char joinedIP[MAX_LOGIP];
void M_InitJoinedIPArray(void);
void M_AddToJoinedIPs(char *address, char *servname);