From 2ed26b3f638787f8c64b19e813f39fa65c66ad66 Mon Sep 17 00:00:00 2001 From: Daggolin Date: Thu, 23 Nov 2023 00:32:10 +0100 Subject: [PATCH] Unix: copy path into a static variable to work on in Sys_Basename and Sys_Dirname. This prevents overwriting argv[0]. (#1183) --- shared/sys/sys_public.h | 4 ++-- shared/sys/sys_unix.cpp | 12 ++++++++---- shared/sys/sys_win32.cpp | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/shared/sys/sys_public.h b/shared/sys/sys_public.h index bad8218531..c93006a698 100644 --- a/shared/sys/sys_public.h +++ b/shared/sys/sys_public.h @@ -141,8 +141,8 @@ char *Sys_DefaultAppPath(void); #endif char *Sys_DefaultHomePath(void); -const char *Sys_Dirname( char *path ); -const char *Sys_Basename( char *path ); +const char *Sys_Dirname( const char *path ); +const char *Sys_Basename( const char *path ); bool Sys_PathCmp( const char *path1, const char *path2 ); diff --git a/shared/sys/sys_unix.cpp b/shared/sys/sys_unix.cpp index 96f5b7aab3..e3149819fc 100644 --- a/shared/sys/sys_unix.cpp +++ b/shared/sys/sys_unix.cpp @@ -164,9 +164,11 @@ qboolean Sys_LowPhysicalMemory( void ) Sys_Basename ================== */ -const char *Sys_Basename( char *path ) +const char *Sys_Basename( const char *path ) { - return basename( path ); + static char buf[ MAX_OSPATH ]; + Q_strncpyz( buf, path, sizeof(buf) ); + return basename( buf ); } /* @@ -174,9 +176,11 @@ const char *Sys_Basename( char *path ) Sys_Dirname ================== */ -const char *Sys_Dirname( char *path ) +const char *Sys_Dirname( const char *path ) { - return dirname( path ); + static char buf[ MAX_OSPATH ]; + Q_strncpyz( buf, path, sizeof(buf) ); + return dirname( buf ); } /* diff --git a/shared/sys/sys_win32.cpp b/shared/sys/sys_win32.cpp index 5b6c234cf5..4a8768130f 100644 --- a/shared/sys/sys_win32.cpp +++ b/shared/sys/sys_win32.cpp @@ -38,7 +38,7 @@ static UINT timerResolution = 0; Sys_Basename ============== */ -const char *Sys_Basename( char *path ) +const char *Sys_Basename( const char *path ) { static char base[ MAX_OSPATH ] = { 0 }; int length; @@ -68,7 +68,7 @@ const char *Sys_Basename( char *path ) Sys_Dirname ============== */ -const char *Sys_Dirname( char *path ) +const char *Sys_Dirname( const char *path ) { static char dir[ MAX_OSPATH ] = { 0 }; int length;