From 35ecd4022803f2d4587cde2b5deb6ead6e8b6fde Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 8 Jul 2005 05:05:06 +0000 Subject: [PATCH] Fixed bug at15479 (karlos) Fixed bug at11812 (Kamikaze) Added new build_pathname implementation for reentracy. build_pathname_r added to module API --- amxmodx/CPlugin.cpp | 6 ++++-- amxmodx/amxmodx.cpp | 26 +++++++++++++---------- amxmodx/amxmodx.h | 1 + amxmodx/amxxlog.cpp | 27 +++++++++++++----------- amxmodx/meta_api.cpp | 17 ++++++++------- amxmodx/modules.cpp | 43 +++++++++++++++++++++++++++++++++----- amxmodx/sdk/amxxmodule.cpp | 3 +++ amxmodx/sdk/amxxmodule.h | 4 ++++ 8 files changed, 90 insertions(+), 37 deletions(-) diff --git a/amxmodx/CPlugin.cpp b/amxmodx/CPlugin.cpp index f1480d6b..318a4fdd 100755 --- a/amxmodx/CPlugin.cpp +++ b/amxmodx/CPlugin.cpp @@ -51,7 +51,8 @@ void CPluginMngr::unloadPlugin( CPlugin** a ) { int CPluginMngr::loadPluginsFromFile( const char* filename ) { - FILE *fp = fopen(build_pathname("%s",filename) , "rt"); + char file[256]; + FILE *fp = fopen(build_pathname_r(file, sizeof(file)-1, "%s",filename) , "rt"); if ( !fp ) { @@ -157,7 +158,8 @@ CPluginMngr::CPlugin::CPlugin(int i, const char* p,const char* n, char* e, int d title.assign(unk); author.assign(unk); version.assign(unk); - char* path = build_pathname("%s/%s",p,n); + char file[256]; + char* path = build_pathname_r(file, sizeof(file)-1, "%s/%s",p,n); code = 0; int err = load_amxscript(&amx,&code,path,e, d); if ( err == AMX_ERR_NONE ) diff --git a/amxmodx/amxmodx.cpp b/amxmodx/amxmodx.cpp index b189ffc2..88196c38 100755 --- a/amxmodx/amxmodx.cpp +++ b/amxmodx/amxmodx.cpp @@ -1323,13 +1323,19 @@ static cell AMX_NATIVE_CALL log_to_file(AMX *amx, cell *params) /* 1 param */ int ilen; char* szFile = get_amxstring(amx,params[1],0,ilen); FILE*fp; - const char* filename = build_pathname("%s/%s",g_log_dir.c_str(),szFile); + char file[256]; + if (strchr(szFile, '/') || strchr(szFile, '\\')) + { + build_pathname_r(file, sizeof(file)-1, "%s", szFile); + } else { + build_pathname_r(file, sizeof(file)-1, "%s/%s", g_log_dir.c_str(), szFile); + } bool first_time = true; - if ((fp=fopen(filename,"r"))!=NULL){ + if ((fp=fopen(file,"r"))!=NULL){ first_time = false; fclose(fp); } - if ((fp=fopen(filename,"a")) == NULL){ + if ((fp=fopen(file,"a")) == NULL){ //amx_RaiseError(amx,AMX_ERR_NATIVE); //would cause too much troubles in old plugins return 0; @@ -1343,13 +1349,10 @@ static cell AMX_NATIVE_CALL log_to_file(AMX *amx, cell *params) /* 1 param */ message[len++]='\n'; message[len]=0; if ( first_time ){ - char game_dir[512]; - GET_GAME_DIR(game_dir); - filename = build_pathname("%s/%s",g_log_dir.c_str(),szFile); fprintf(fp,"L %s: Log file started (file \"%s\") (game \"%s\") (amx \"%s\")\n", - date,filename,g_mod_name.c_str(),Plugin_info.version); + date,file,g_mod_name.c_str(),Plugin_info.version); print_srvconsole("L %s: Log file started (file \"%s\") (game \"%s\") (amx \"%s\")\n", - date,filename,g_mod_name.c_str(),Plugin_info.version); + date,file,g_mod_name.c_str(),Plugin_info.version); } fprintf(fp,"L %s: %s",date,message); print_srvconsole("L %s: %s",date,message); @@ -2495,8 +2498,8 @@ static cell AMX_NATIVE_CALL get_func_id(AMX *amx, cell *params) int len; const char *funcName = get_amxstring(amx, params[1], 0, len); - int index; - if (amx_FindPublic(plugin->getAMX(), funcName, &index) != AMX_ERR_NONE) + int index, err; + if ( (err = amx_FindPublic(plugin->getAMX(), funcName, &index)) != AMX_ERR_NONE) index = -1; return index; @@ -2728,7 +2731,8 @@ static cell AMX_NATIVE_CALL get_lang(AMX *amx, cell *params) static cell AMX_NATIVE_CALL register_dictionary(AMX *amx, cell *params) { int len; - int result = g_langMngr.MergeDefinitionFile(build_pathname("%s/lang/%s", + static char file[256]; + int result = g_langMngr.MergeDefinitionFile(build_pathname_r(file, sizeof(file)-1, "%s/lang/%s", get_localinfo("amxx_datadir", "addons/amxmodx/data"), get_amxstring(amx, params[1], 1, len))); return result; } diff --git a/amxmodx/amxmodx.h b/amxmodx/amxmodx.h index 87b95ec0..9d4a973b 100755 --- a/amxmodx/amxmodx.h +++ b/amxmodx/amxmodx.h @@ -245,6 +245,7 @@ void modules_callPluginsLoaded(); int add_amxnatives(module_info_s* info,AMX_NATIVE_INFO*natives); cell* get_amxaddr(AMX *amx,cell amx_addr); char* build_pathname(char *fmt, ... ); +char* build_pathname_r(char *buffer, size_t maxlen, char *fmt, ...); char* format_amxstring(AMX *amx, cell *params, int parm,int& len); AMX* get_amxscript(int, void**,const char**); const char* get_amxscriptname(AMX* amx); diff --git a/amxmodx/amxxlog.cpp b/amxmodx/amxxlog.cpp index 6bf36591..6d8fdd27 100755 --- a/amxmodx/amxxlog.cpp +++ b/amxmodx/amxxlog.cpp @@ -92,7 +92,7 @@ void CLog::CreateNewFile() int i = 0; while (true) { - m_LogFile.assign(build_pathname("%s/L%02d%02d%03d.log", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday, i)); + char file[256]; FILE *pTmpFile = fopen(m_LogFile.c_str(), "r"); // open for reading to check whether the file exists if (!pTmpFile) break; @@ -115,16 +115,18 @@ void CLog::CreateNewFile() void CLog::UseFile(const String &fileName) { - m_LogFile.assign(build_pathname("%s/%s", g_log_dir.c_str(), fileName.c_str())); + static char file[256]; + m_LogFile.assign(build_pathname_r(file, sizeof(file)-1, "%s/%s", g_log_dir.c_str(), fileName.c_str())); } void CLog::MapChange() { // create dir if not existing + char file[256]; #ifdef __linux - mkdir(build_pathname("%s", g_log_dir.c_str()), 0700); + mkdir(build_pathname_r(file, sizeof(file)-1,"%s", g_log_dir.c_str()), 0700); #else - mkdir(build_pathname("%s", g_log_dir.c_str())); + mkdir(build_pathname_r(file, sizeof(file)-1,"%s", g_log_dir.c_str())); #endif m_LogType = atoi(get_localinfo("amxx_logging", "1")); @@ -150,6 +152,7 @@ void CLog::MapChange() void CLog::Log(const char *fmt, ...) { + static char file[256]; if (m_LogType == 1 || m_LogType == 2) { // get time @@ -161,14 +164,14 @@ void CLog::Log(const char *fmt, ...) strftime(date, 31, "%m/%d/%Y - %H:%M:%S", curTime); // msg - char msg[3072]; + static char msg[3072]; va_list arglst; va_start(arglst, fmt); vsnprintf(msg, 3071, fmt, arglst); va_end(arglst); - FILE *pF; + FILE *pF = NULL; if (m_LogType == 2) { pF = fopen(m_LogFile.c_str(), "a+"); @@ -186,7 +189,8 @@ void CLog::Log(const char *fmt, ...) } else { - pF = fopen(build_pathname("%s/L%02d%02d.log", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday), "a+"); + build_pathname_r(file, sizeof(file)-1, "%s/L%02d%02d.log", g_log_dir.c_str(), curTime->tm_mon + 1, curTime->tm_mday); + pF = fopen(file, "a+"); } if (pF) { @@ -195,7 +199,7 @@ void CLog::Log(const char *fmt, ...) } else { - ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", m_LogFile.c_str()); + ALERT(at_logged, "[AMXX] Unexpected fatal logging error (couldn't open %s for a+). AMXX Logging disabled for this map.\n", file); m_LogType = 0; return; } @@ -206,12 +210,11 @@ void CLog::Log(const char *fmt, ...) else if (m_LogType == 3) { // build message - // :TODO: Overflow possible here - char msg[3072]; + static char msg_[3072]; va_list arglst; va_start(arglst, fmt); - vsnprintf(msg, 3071, fmt, arglst); + vsnprintf(msg_, 3071, fmt, arglst); va_end(arglst); - ALERT(at_logged, "%s\n", msg); + ALERT(at_logged, "%s\n", msg_); } } diff --git a/amxmodx/meta_api.cpp b/amxmodx/meta_api.cpp index 74a359aa..f7e23ab3 100755 --- a/amxmodx/meta_api.cpp +++ b/amxmodx/meta_api.cpp @@ -221,8 +221,9 @@ int C_Spawn( edict_t *pent ) { g_tasksMngr.registerTimers( &gpGlobals->time, &mp_timelimit->value, &g_game_timeleft ); // ###### Load lang - g_langMngr.LoadCache(build_pathname("%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); - g_langMngr.Load(build_pathname("%s/languages.dat", get_localinfo("amxmodx_datadir", "addons/amxmodx/data"))); + char file[256]; + g_langMngr.LoadCache(build_pathname_r(file, sizeof(file)-1, "%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); + g_langMngr.Load(build_pathname_r(file, sizeof(file)-1, "%s/languages.dat", get_localinfo("amxmodx_datadir", "addons/amxmodx/data"))); // ###### Initialize commands prefixes g_commands.registerPrefix( "amx" ); g_commands.registerPrefix( "amxx" ); @@ -249,7 +250,7 @@ int C_Spawn( edict_t *pent ) { CVAR_SET_STRING(init_amxmodx_modules.name, buffer); // ###### Load Vault - g_vault.setSource( build_pathname("%s", get_localinfo("amxx_vault", "addons/amxmodx/configs/vault.ini")) ); + g_vault.setSource( build_pathname_r(file, sizeof(file)-1, "%s", get_localinfo("amxx_vault", "addons/amxmodx/configs/vault.ini")) ); g_vault.loadVault( ); if (strlen(g_vault.get("server_language")) < 1) { @@ -400,8 +401,9 @@ void C_ServerActivate_Post( edict_t *pEdictList, int edictCount, int clientMax ) executeForwards(FF_PluginCfg); // ###### Save lang - g_langMngr.Save(build_pathname("%s/languages.dat", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); - g_langMngr.SaveCache(build_pathname("%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); + char file[256]; + g_langMngr.Save(build_pathname_r(file, sizeof(file)-1, "%s/languages.dat", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); + g_langMngr.SaveCache(build_pathname_r(file, sizeof(file)-1, "%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); // Correct time in Counter-Strike and other mods (except DOD) if ( !g_bmod_dod) g_game_timeleft = 0; @@ -458,8 +460,9 @@ void C_ServerDeactivate_Post() { g_vault.clear(); g_xvars.clear(); g_plugins.clear(); - g_langMngr.Save(build_pathname("%s/languages.dat", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); - g_langMngr.SaveCache(build_pathname("%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); + char file[256]; + g_langMngr.Save(build_pathname_r(file, sizeof(file)-1, "%s/languages.dat", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); + g_langMngr.SaveCache(build_pathname_r(file, sizeof(file)-1, "%s/dictionary.cache", get_localinfo("amxx_datadir", "addons/amxmodx/data"))); g_langMngr.Clear(); //clear module name cache while (!CurModuleList.empty()) diff --git a/amxmodx/modules.cpp b/amxmodx/modules.cpp index 530125b1..b3eca036 100755 --- a/amxmodx/modules.cpp +++ b/amxmodx/modules.cpp @@ -72,11 +72,11 @@ void report_error( int code, char* fmt, ... ) void print_srvconsole( char *fmt, ... ) { va_list argptr; - char string[256]; - va_start (argptr, fmt); - vsnprintf (string, 255, fmt,argptr); - string[255] = 0; - va_end (argptr); + static char string[384]; + va_start(argptr, fmt); + vsnprintf(string, sizeof(string)-1, fmt, argptr); + string[sizeof(string)-1] = '\0'; + va_end(argptr); SERVER_PRINT(string); } @@ -422,6 +422,38 @@ char* build_pathname(char *fmt, ... ) return string; } +char *build_pathname_r(char *buffer, size_t maxlen, char *fmt, ...) +{ + snprintf(buffer, maxlen, +#ifdef __linux__ + "%s/", +#else + "%s\\", +#endif + g_mod_name.c_str() + ); + + size_t len = strlen(buffer); + char *ptr = buffer + len; + + va_list argptr; + va_start(argptr, fmt); + vsnprintf (ptr, maxlen-len, fmt, argptr); + va_end (argptr); + + while (*ptr) + { +#ifndef __linux__ + if (*ptr == '/') *ptr = '\\'; +#else + if (*ptr == '\\') *ptr = '/'; +#endif + ++ptr; + } + + return buffer; +} + // build pathname based on addons dir char* build_pathname_addons(char *fmt, ... ) @@ -1347,6 +1379,7 @@ void Module_CacheFunctions() func_s *pFunc; REGISTER_FUNC("BuildPathname", build_pathname) + REGISTER_FUNC("BuildPathnameR", build_pathname_r) REGISTER_FUNC("PrintSrvConsole", print_srvconsole) REGISTER_FUNC("GetModname", MNF_GetModname) REGISTER_FUNC("Log", MNF_Log) diff --git a/amxmodx/sdk/amxxmodule.cpp b/amxmodx/sdk/amxxmodule.cpp index 2db3f481..f27f65e6 100755 --- a/amxmodx/sdk/amxxmodule.cpp +++ b/amxmodx/sdk/amxxmodule.cpp @@ -2438,6 +2438,7 @@ static amxx_module_info_s g_ModuleInfo = // Storage for the requested functions PFN_ADD_NATIVES g_fn_AddNatives; PFN_BUILD_PATHNAME g_fn_BuildPathname; +PFN_BUILD_PATHNAME_R g_fn_BuildPathnameR; PFN_GET_AMXADDR g_fn_GetAmxAddr; PFN_PRINT_SRVCONSOLE g_fn_PrintSrvConsole; PFN_GET_MODNAME g_fn_GetModname; @@ -2542,6 +2543,7 @@ C_DLLEXPORT int AMXX_Attach(PFN_REQ_FNPTR reqFnptrFunc) // Req all known functions // Misc REQFUNC("BuildPathname", g_fn_BuildPathname, PFN_BUILD_PATHNAME); + REQFUNC("BuildPathnameR", g_fn_BuildPathnameR, PFN_BUILD_PATHNAME_R); REQFUNC("PrintSrvConsole", g_fn_PrintSrvConsole, PFN_PRINT_SRVCONSOLE); REQFUNC("GetModname", g_fn_GetModname, PFN_GET_MODNAME); REQFUNC("Log", g_fn_Log, PFN_LOG); @@ -2672,6 +2674,7 @@ void MF_LogError(AMX *amx, int err, const char *fmt, ...) void ValidateMacros_DontCallThis_Smiley() { MF_BuildPathname("str", "str", 0); + MF_BuildPathnameR(NULL, 0, "%d", 0); MF_FormatAmxString(NULL, 0, 0, NULL); MF_GetAmxAddr(NULL, 0); MF_PrintSrvConsole("str", "str", 0); diff --git a/amxmodx/sdk/amxxmodule.h b/amxmodx/sdk/amxxmodule.h index 2d5dcee0..772aaa89 100755 --- a/amxmodx/sdk/amxxmodule.h +++ b/amxmodx/sdk/amxxmodule.h @@ -1909,6 +1909,7 @@ enum ForwardParam typedef int (*PFN_ADD_NATIVES) (const AMX_NATIVE_INFO * /*list*/); typedef char * (*PFN_BUILD_PATHNAME) (const char * /*format*/, ...); +typedef char * (*PFN_BUILD_PATHNAME_R) (char * /*buffer*/, size_t /* maxlen */, const char * /* format */, ...); typedef cell * (*PFN_GET_AMXADDR) (AMX * /*amx*/, cell /*offset*/); typedef void (*PFN_PRINT_SRVCONSOLE) (char * /*format*/, ...); typedef const char * (*PFN_GET_MODNAME) (void); @@ -1981,6 +1982,7 @@ typedef void (*PFN_REGISTERFUNCTION) (void * /*pfn*/, const char * /*desc*/) extern PFN_ADD_NATIVES g_fn_AddNatives; extern PFN_BUILD_PATHNAME g_fn_BuildPathname; +extern PFN_BUILD_PATHNAME_R g_fn_BuildPathnameR; extern PFN_GET_AMXADDR g_fn_GetAmxAddr; extern PFN_PRINT_SRVCONSOLE g_fn_PrintSrvConsole; extern PFN_GET_MODNAME g_fn_GetModname; @@ -2046,6 +2048,7 @@ extern PFN_REQ_FNPTR g_fn_RequestFunction; // They understand #if 0 so we use #ifdef MAY_NEVER_BE_DEFINED int MF_AddNatives (const AMX_NATIVE_INFO *list) { } char * MF_BuildPathname (const char * format, ...) { } +char * MF_BuildPathnameR (char *buffer, size_t maxlen, const char *fmt, ...) { } cell * MF_GetAmxAddr (AMX * amx, cell offset) { } void MF_PrintSrvConsole (char * format, ...) { } const char * MF_GetModname (void) { } @@ -2101,6 +2104,7 @@ void * MF_RequestFunction (const char *description) { } #define MF_AddNatives g_fn_AddNatives #define MF_BuildPathname g_fn_BuildPathname +#define MF_BuildPathnameR g_fn_BuildPathnameR #define MF_FormatAmxString g_fn_FormatAmxString #define MF_GetAmxAddr g_fn_GetAmxAddr #define MF_PrintSrvConsole g_fn_PrintSrvConsole