From 8d5c8d2df43cacaf9949b31cef66af9931f59c6f Mon Sep 17 00:00:00 2001 From: s1lent Date: Sat, 18 Nov 2017 21:36:53 +0700 Subject: [PATCH] Use Q_strlcpy instead Q_strncpy for simplified code --- metamod/src/conf_meta.cpp | 3 +-- metamod/src/metamod.cpp | 29 ++++++++++++----------------- metamod/src/mlist.cpp | 18 ++++++------------ metamod/src/mplayer.cpp | 3 +-- metamod/src/mplugin.cpp | 27 +++++++++------------------ metamod/src/mreg.cpp | 21 +++++++-------------- metamod/src/mutil.cpp | 6 ++---- metamod/src/utils.cpp | 11 +++++------ 8 files changed, 43 insertions(+), 75 deletions(-) diff --git a/metamod/src/conf_meta.cpp b/metamod/src/conf_meta.cpp index 35f3ef5..de3ec78 100644 --- a/metamod/src/conf_meta.cpp +++ b/metamod/src/conf_meta.cpp @@ -177,8 +177,7 @@ void MConfig::set_directory() #else Dl_info addrInfo; if (dladdr((void *)GiveFnptrsToDll, &addrInfo)) { - Q_strncpy(m_directory, addrInfo.dli_fname, sizeof m_directory - 1); - m_directory[sizeof m_directory - 1] = '\0'; + Q_strlcpy(m_directory, addrInfo.dli_fname); } #endif diff --git a/metamod/src/metamod.cpp b/metamod/src/metamod.cpp index aa738c0..f72b548 100644 --- a/metamod/src/metamod.cpp +++ b/metamod/src/metamod.cpp @@ -79,16 +79,15 @@ void metamod_startup() META_LOG("Configfile specified via localinfo: %s", cp); if (is_file_exists_in_gamedir(cp)) { - Q_strncpy(configFile, cp, sizeof configFile - 1); - configFile[sizeof configFile - 1] = '\0'; + Q_strlcpy(configFile, cp); } - else + else { META_ERROR("Empty/missing config.ini file: %s; falling back to %s", cp, configFile); + } } if (!is_file_exists_in_gamedir(configFile)) { - Q_strncpy(configFile, g_config->directory(), sizeof configFile - 1); - configFile[sizeof configFile - 1] = '\0'; + Q_strlcpy(configFile, g_config->directory()); // Get out of sub directory and check char *dir = Q_strrchr(configFile, '/'); @@ -182,8 +181,7 @@ void metamod_startup() // Load plugins file if (!is_file_exists_in_gamedir(pluginFile)) { - Q_strncpy(pluginFile, g_config->directory(), sizeof pluginFile - 1); - pluginFile[sizeof pluginFile - 1] = '\0'; + Q_strlcpy(pluginFile, g_config->directory()); // Get out of sub directory and check char *dir = Q_strrchr(pluginFile, '/'); @@ -221,8 +219,7 @@ void metamod_startup() // avoid confusing users with "couldn't exec exec.cfg" console // messages. if (g_config->m_exec_cfg) { - Q_strncpy(execFile, g_config->m_exec_cfg, sizeof execFile - 1); - execFile[sizeof execFile - 1] = '\0'; + Q_strlcpy(execFile, g_config->m_exec_cfg); } if (is_file_exists_in_gamedir(execFile)) { @@ -262,13 +259,12 @@ bool meta_init_gamedll() // Old style; GET_GAME_DIR returned full pathname. Copy this into // our gamedir, and truncate to get the game name. // (note check for both linux and win32 full pathname.) - Q_strncpy(g_GameDLL.gamedir, gamedir, sizeof g_GameDLL.gamedir - 1); - g_GameDLL.gamedir[sizeof g_GameDLL.gamedir - 1] = '\0'; + Q_strlcpy(g_GameDLL.gamedir, gamedir); - char* cp = Q_strrchr(gamedir, '/') + 1; - - Q_strncpy(g_GameDLL.name, cp, sizeof g_GameDLL.name - 1); - g_GameDLL.name[sizeof g_GameDLL.name - 1] = '\0'; + char *cp = Q_strrchr(gamedir, '/') + 1; + if (cp) { + Q_strlcpy(g_GameDLL.name, cp); + } } else { // New style; GET_GAME_DIR returned game name. Copy this into our @@ -280,8 +276,7 @@ bool meta_init_gamedll() } Q_snprintf(g_GameDLL.gamedir, sizeof g_GameDLL.gamedir, "%s/%s", buf, gamedir); - Q_strncpy(g_GameDLL.name, gamedir, sizeof g_GameDLL.name - 1); - g_GameDLL.name[sizeof g_GameDLL.name - 1] = '\0'; + Q_strlcpy(g_GameDLL.name, gamedir); } META_DEBUG(3, "Game: %s", g_GameDLL.name); diff --git a/metamod/src/mlist.cpp b/metamod/src/mlist.cpp index 422beb7..3ad858f 100644 --- a/metamod/src/mlist.cpp +++ b/metamod/src/mlist.cpp @@ -4,8 +4,7 @@ MPluginList::MPluginList(const char* ifile) : m_last_index(0), m_plugins() { // store filename of ini file - Q_strncpy(m_inifile, ifile, sizeof m_inifile - 1); - m_inifile[sizeof m_inifile - 1] = '\0'; + Q_strlcpy(m_inifile, ifile); } plugins_t* MPluginList::getPlugins() @@ -199,8 +198,7 @@ MPlugin* MPluginList::add(MPlugin* padd) plug->m_index = ++m_last_index; // copy filename into this free slot - Q_strncpy(plug->m_filename, padd->m_filename, sizeof plug->m_filename - 1); - plug->m_filename[sizeof plug->m_filename - 1] = '\0'; + Q_strlcpy(plug->m_filename, padd->m_filename); // Copy file offset ptr. // Can't just copy ptr, as it points to offset in padd, which will go @@ -208,12 +206,10 @@ MPlugin* MPluginList::add(MPlugin* padd) plug->m_file = plug->m_filename + (padd->m_file - padd->m_filename); // copy description - Q_strncpy(plug->m_desc, padd->m_desc, sizeof plug->m_desc - 1); - plug->m_desc[sizeof plug->m_desc - 1] = '\0'; + Q_strlcpy(plug->m_desc, padd->m_desc); // copy pathname - Q_strncpy(plug->m_pathname, padd->m_pathname, sizeof plug->m_pathname - 1); - plug->m_pathname[sizeof plug->m_pathname - 1] = '\0'; + Q_strlcpy(plug->m_pathname, padd->m_pathname); normalize_path(plug->m_pathname); plug->m_source = padd->m_source; @@ -364,11 +360,9 @@ bool MPluginList::ini_refresh() } else { // This plugin is already in the current list of plugins. - // Pathname already matches. Recopy desc, if specified in - // plugins.ini. + // Pathname already matches. Recopy desc, if specified in plugins.ini. if (pl_temp.m_desc[0] != '<') { - Q_strncpy(pl_found->m_desc, pl_temp.m_desc, sizeof pl_found->m_desc - 1); - pl_found->m_desc[sizeof pl_found->m_desc - 1] = '\0'; + Q_strlcpy(pl_found->m_desc, pl_temp.m_desc); } // Check the file to see if it looks like it's been modified diff --git a/metamod/src/mplayer.cpp b/metamod/src/mplayer.cpp index 7d1449b..7bc9c03 100644 --- a/metamod/src/mplayer.cpp +++ b/metamod/src/mplayer.cpp @@ -17,8 +17,7 @@ void MPlayer::set_cvar_query(const char* cvar) } m_isQueried = true; - Q_strncpy(g_cvarName, cvar, sizeof g_cvarName - 1); - g_cvarName[sizeof g_cvarName - 1] = '\0'; + Q_strlcpy(g_cvarName, cvar); } // Unmark player as querying a client cvar diff --git a/metamod/src/mplugin.cpp b/metamod/src/mplugin.cpp index 0195d82..09a1c18 100644 --- a/metamod/src/mplugin.cpp +++ b/metamod/src/mplugin.cpp @@ -80,8 +80,7 @@ bool MPlugin::ini_parseline(char *line) return false; } - Q_strncpy(m_filename, token, sizeof m_filename - 1); - m_filename[sizeof m_filename - 1] = '\0'; + Q_strlcpy(m_filename, token); normalize_path(m_filename); // Store name of just the actual _file_, without dir components. @@ -96,8 +95,7 @@ bool MPlugin::ini_parseline(char *line) token = strtok_r(nullptr, "\n\r", &ptr_token); if (token) { token = token + strspn(token, " \t"); // skip whitespace - Q_strncpy(m_desc, token, sizeof m_desc - 1); - m_desc[sizeof m_desc - 1] = '\0'; + Q_strlcpy(m_desc, token); } else { // If no description is specified, temporarily use plugin file, @@ -120,8 +118,7 @@ bool MPlugin::cmd_parseline(const char *line) char buf[NAME_MAX + PATH_MAX + MAX_DESC_LEN]; char *ptr_token; - Q_strncpy(buf, line, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, line); // remove "load" char* token = strtok_r(buf, " \t", &ptr_token); @@ -133,8 +130,7 @@ bool MPlugin::cmd_parseline(const char *line) if (!token) return false; - Q_strncpy(m_filename, token, sizeof m_filename - 1); - m_filename[sizeof m_filename - 1] = '\0'; + Q_strlcpy(m_filename, token); normalize_path(m_filename); // store name of just the actual _file_, without dir components @@ -149,8 +145,7 @@ bool MPlugin::cmd_parseline(const char *line) token = strtok_r(nullptr, "", &ptr_token); if (token) { token = token + strspn(token, " \t"); // skip whitespace - Q_strncpy(m_desc, token, sizeof m_desc - 1); - m_desc[sizeof m_desc - 1] = '\0'; + Q_strlcpy(m_desc, token); } else { // if no description is specified, temporarily use plugin file, @@ -172,8 +167,7 @@ bool MPlugin::plugin_parseline(const char *fname, int loader_index) { m_source_plugin_index = loader_index; - Q_strncpy(m_filename, fname, sizeof m_filename - 1); - m_filename[sizeof m_filename - 1] = '\0'; + Q_strlcpy(m_filename, fname); normalize_path(m_filename); //store just name of the actual _file, without path @@ -254,8 +248,7 @@ bool MPlugin::resolve() META_DEBUG(2, "Resolved '%s' to file '%s'", m_filename, found); // store pathname: the resolved path (should be absolute) - Q_strncpy(m_pathname, found, sizeof m_pathname - 1); - m_pathname[sizeof m_pathname - 1] = '\0'; + Q_strlcpy(m_pathname, found); // store file: the name of the file (without dir) char* cp = Q_strrchr(m_pathname, '/'); @@ -271,8 +264,7 @@ bool MPlugin::resolve() if (!Q_strnicmp(m_pathname, g_GameDLL.gamedir, len)) filename += len + 1; - Q_strncpy(m_filename, filename, sizeof m_filename - 1); - m_filename[sizeof m_filename - 1] = '\0'; + Q_strlcpy(m_filename, filename); return true; } @@ -875,8 +867,7 @@ bool MPlugin::query() // Replace temporary desc with plugin's internal name. if (m_desc[0] == '<') { - Q_strncpy(m_desc, m_info->name, sizeof m_desc - 1); - m_desc[sizeof m_desc - 1] = '\0'; + Q_strlcpy(m_desc, m_info->name); } META_DEBUG(6, "dll: Plugin '%s': Query successful", m_desc); diff --git a/metamod/src/mreg.cpp b/metamod/src/mreg.cpp index 01c565d..1976993 100644 --- a/metamod/src/mreg.cpp +++ b/metamod/src/mreg.cpp @@ -110,12 +110,10 @@ void MRegCmdList::show() const if (reg->m_status == RG_VALID) { auto iplug = g_plugins->find(reg->m_plugid); - Q_strncpy(bplug, iplug ? iplug->description() : "(unknown)", sizeof bplug - 1); - bplug[sizeof bplug - 1] = '\0'; + Q_strlcpy(bplug, iplug ? iplug->description() : "(unknown)"); } else { - Q_strncpy(bplug, "(unloaded)", sizeof bplug - 1); - bplug[sizeof bplug - 1] = '\0'; + Q_strlcpy(bplug, "(unloaded)"); } META_CONS(" [%*d] %-*s %-s", WIDTH_MAX_REG, ++total_count, sizeof bplug - 1, bplug, reg->m_name); @@ -219,16 +217,13 @@ void MRegCvarList::show() const for (auto reg : m_list) { if (reg->m_status == RG_VALID) { auto plug = g_plugins->find(reg->m_plugid); - Q_strncpy(bplug, plug ? plug->description() : "(unknown)", sizeof bplug - 1); - bplug[sizeof bplug - 1] = '\0'; + Q_strlcpy(bplug, plug ? plug->description() : "(unknown)"); } else { - Q_strncpy(bplug, "(unloaded)", sizeof bplug - 1); - bplug[sizeof bplug - 1] = '\0'; + Q_strlcpy(bplug, "(unloaded)"); } - Q_strncpy(bname, reg->m_cvar->name, sizeof bname - 1); - bname[sizeof bname - 1] = '\0'; + Q_strlcpy(bname, reg->m_cvar->name); Q_snprintf(bval, sizeof bval, "%f", reg->m_cvar->value); META_CONS(" [%*d] %-*s %-*s %*s %s", WIDTH_MAX_REG, ++total_count, sizeof bplug - 1, bplug, sizeof bname - 1, bname, sizeof bval - 1, bval, reg->m_cvar->string); @@ -251,8 +246,7 @@ void MRegCvarList::show(int plugin_id) const if (reg->m_plugid != plugin_id) continue; - Q_strncpy(bname, reg->m_cvar->name, sizeof bname - 1); - bname[sizeof bname - 1] = '\0'; + Q_strlcpy(bname, reg->m_cvar->name); Q_snprintf(bval, sizeof bval, "%f", reg->m_cvar->value); META_CONS(" %-*s %*s %s", sizeof bname - 1, bname, sizeof bval - 1, bval, reg->m_cvar->string); total_count++; @@ -330,8 +324,7 @@ void MRegMsgList::show() META_CONS("%-*s %5s %5s", sizeof bname - 1, "Game registered user msgs:", "msgid", "size"); for (auto msg : m_list) { - Q_strncpy(bname, msg->m_name, sizeof bname - 1); - bname[sizeof bname - 1] = '\0'; + Q_strlcpy(bname, msg->m_name); META_CONS(" %-*s %3d %3d", sizeof bname - 1, bname, msg->m_msgid, msg->m_size); total_count++; } diff --git a/metamod/src/mutil.cpp b/metamod/src/mutil.cpp index d3bc70e..873b71f 100644 --- a/metamod/src/mutil.cpp +++ b/metamod/src/mutil.cpp @@ -246,8 +246,7 @@ const char* EXT_FUNC mutil_GetPluginPath(plid_t plid) return nullptr; } - Q_strncpy(buf, plug->pathname(), sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, plug->pathname()); return buf; } @@ -281,8 +280,7 @@ const char* EXT_FUNC mutil_GetGameInfo(plid_t plid, ginfo_t type) return nullptr; } - Q_strncpy(buf, cp, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, cp); return buf; } diff --git a/metamod/src/utils.cpp b/metamod/src/utils.cpp index 73621d1..816049b 100644 --- a/metamod/src/utils.cpp +++ b/metamod/src/utils.cpp @@ -132,11 +132,11 @@ bool is_file_exists_in_gamedir(const char* path) return true; if (is_abs_path(path)) { - Q_strncpy(buf, path, sizeof buf); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, path); + } + else { + Q_snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path); } - else - snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path); struct stat64 st; int ret = stat64(buf, &st); @@ -175,8 +175,7 @@ char* full_gamedir_path(const char* path, char* fullpath) // Build pathname from filename, plus gamedir if relative path. if (is_abs_path(path)) { - Q_strncpy(buf, path, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, path); } else snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path);