Skip to content

Commit

Permalink
Fix segfault with GetMapFileName()
Browse files Browse the repository at this point in the history
  • Loading branch information
erysdren committed Jul 14, 2024
1 parent e1be995 commit 99c1e11
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion rott/rt_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7976,7 +7976,7 @@ void ShowBattleOptions
}
ShowBattleOption( inmenu, PosX, PosY, 0, 9, "Danger Damage", string );

GetMapFileName ( text );
GetMapFileName ( text, sizeof(text) );
ShowBattleOption( inmenu, PosX, PosY, 0, 10, "Filename", text );

itoa( numplayers, text, 10 );
Expand Down
2 changes: 1 addition & 1 deletion rott/rt_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -2420,7 +2420,7 @@ void SendGameDescription( void )
desc->teamplay = gamestate.teamplay;
memcpy( &desc->SpecialsTimes, &gamestate.SpecialsTimes, sizeof( specials ) );
BATTLE_GetOptions( &( desc->options ) );
GetMapFileName( &(desc->battlefilename[0]) );
GetMapFileName( &(desc->battlefilename[0]), sizeof(desc->battlefilename) );
desc->randomseed=GetRNGindex ( );
gamestate.randomseed=desc->randomseed;
desc->ludicrousgibs=battlegibs;
Expand Down
39 changes: 21 additions & 18 deletions rott/rt_ted.c
Original file line number Diff line number Diff line change
Expand Up @@ -1622,24 +1622,27 @@ void GetMapFileInfo
=
======================
*/
void GetMapFileName ( char * filename )
void GetMapFileName ( char * filename, size_t n )
{
if ( ( BATTLEMODE ) && (BattleLevels.avail == true) )
{
strcpy(filename,BattleLevels.file);
}
else if (GameLevels.avail == true)
{
strcpy(filename,GameLevels.file);
}
else if ( BATTLEMODE )
{
strcpy(filename,BATTMAPS);
}
else
{
strcpy(filename,ROTTMAPS);
}
char *src, *ptr;

if (BATTLEMODE && BattleLevels.avail == true)
src = BattleLevels.file;
else if (GameLevels.avail == true)
src = GameLevels.file;
else if ( BATTLEMODE )

This comment has been minimized.

Copy link
@fabiangreffrath

fabiangreffrath Jul 14, 2024

Owner

Inconsistent parentheses?

This comment has been minimized.

Copy link
@erysdren

erysdren Jul 14, 2024

Author Collaborator

Will fix.

src = BATTMAPS;
else
src = ROTTMAPS;

ptr = strrchr(src, PATH_SEP_CHAR);

if (ptr == NULL)
ptr = src;

This comment has been minimized.

Copy link
@fabiangreffrath

fabiangreffrath Jul 14, 2024

Owner

We have M_BaseName() for this:

taradino/rott/m_misc2.c

Lines 197 to 221 in 32dcda8

const char *M_BaseName(const char *path)
{
const char *pf, *pb;
pf = strrchr(path, '/');
#ifdef _WIN32
pb = strrchr(path, '\\');
// [FG] allow C:filename
if (pf == NULL && pb == NULL)
{
pb = strrchr(path, ':');
}
#else
pb = NULL;
#endif
if (pf == NULL && pb == NULL)
{
return path;
}
else
{
const char *p = max(pb, pf);
return p + 1;
}
}

This comment has been minimized.

Copy link
@erysdren

erysdren Jul 14, 2024

Author Collaborator

Ah right, thanks. Will change.

else
ptr = ptr + 1;

strncpy(filename,ptr,n);
}

/*
Expand Down Expand Up @@ -1673,7 +1676,7 @@ unsigned short GetMapCRC
RTLMAP RTLMap;
size_t mapsoffset;

GetMapFileName( &filename[ 0 ] );
GetMapFileName( &filename[ 0 ], sizeof(filename) );
CheckRTLVersion( filename );
filehandle = SafeOpenRead( filename );
mapsoffset = GetMapArrayOffset( filehandle );
Expand Down
2 changes: 1 addition & 1 deletion rott/rt_ted.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void PrintMapStats (void);
void PrintTileStats (void);

void GetMapInfo (mapfileinfo_t * mapinfo);
void GetMapFileName ( char * filename );
void GetMapFileName ( char * filename, size_t n );
void SetBattleMapFileName ( char * filename );
unsigned short GetMapCRC ( int num );

Expand Down

0 comments on commit 99c1e11

Please sign in to comment.