2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-03-03 17:15:26 +03:00

Use Q_strlcpy instead Q_strncpy for simplified code

This commit is contained in:
s1lent 2017-11-18 21:36:53 +07:00
parent e343dfe905
commit 8d5c8d2df4
No known key found for this signature in database
GPG Key ID: 0FE401DC73916B5C
8 changed files with 43 additions and 75 deletions

View File

@ -177,8 +177,7 @@ void MConfig::set_directory()
#else #else
Dl_info addrInfo; Dl_info addrInfo;
if (dladdr((void *)GiveFnptrsToDll, &addrInfo)) { if (dladdr((void *)GiveFnptrsToDll, &addrInfo)) {
Q_strncpy(m_directory, addrInfo.dli_fname, sizeof m_directory - 1); Q_strlcpy(m_directory, addrInfo.dli_fname);
m_directory[sizeof m_directory - 1] = '\0';
} }
#endif #endif

View File

@ -79,16 +79,15 @@ void metamod_startup()
META_LOG("Configfile specified via localinfo: %s", cp); META_LOG("Configfile specified via localinfo: %s", cp);
if (is_file_exists_in_gamedir(cp)) { if (is_file_exists_in_gamedir(cp)) {
Q_strncpy(configFile, cp, sizeof configFile - 1); Q_strlcpy(configFile, cp);
configFile[sizeof configFile - 1] = '\0';
} }
else else {
META_ERROR("Empty/missing config.ini file: %s; falling back to %s", cp, configFile); META_ERROR("Empty/missing config.ini file: %s; falling back to %s", cp, configFile);
}
} }
if (!is_file_exists_in_gamedir(configFile)) { if (!is_file_exists_in_gamedir(configFile)) {
Q_strncpy(configFile, g_config->directory(), sizeof configFile - 1); Q_strlcpy(configFile, g_config->directory());
configFile[sizeof configFile - 1] = '\0';
// Get out of sub directory and check // Get out of sub directory and check
char *dir = Q_strrchr(configFile, '/'); char *dir = Q_strrchr(configFile, '/');
@ -182,8 +181,7 @@ void metamod_startup()
// Load plugins file // Load plugins file
if (!is_file_exists_in_gamedir(pluginFile)) { if (!is_file_exists_in_gamedir(pluginFile)) {
Q_strncpy(pluginFile, g_config->directory(), sizeof pluginFile - 1); Q_strlcpy(pluginFile, g_config->directory());
pluginFile[sizeof pluginFile - 1] = '\0';
// Get out of sub directory and check // Get out of sub directory and check
char *dir = Q_strrchr(pluginFile, '/'); char *dir = Q_strrchr(pluginFile, '/');
@ -221,8 +219,7 @@ void metamod_startup()
// avoid confusing users with "couldn't exec exec.cfg" console // avoid confusing users with "couldn't exec exec.cfg" console
// messages. // messages.
if (g_config->m_exec_cfg) { if (g_config->m_exec_cfg) {
Q_strncpy(execFile, g_config->m_exec_cfg, sizeof execFile - 1); Q_strlcpy(execFile, g_config->m_exec_cfg);
execFile[sizeof execFile - 1] = '\0';
} }
if (is_file_exists_in_gamedir(execFile)) { 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 // Old style; GET_GAME_DIR returned full pathname. Copy this into
// our gamedir, and truncate to get the game name. // our gamedir, and truncate to get the game name.
// (note check for both linux and win32 full pathname.) // (note check for both linux and win32 full pathname.)
Q_strncpy(g_GameDLL.gamedir, gamedir, sizeof g_GameDLL.gamedir - 1); Q_strlcpy(g_GameDLL.gamedir, gamedir);
g_GameDLL.gamedir[sizeof g_GameDLL.gamedir - 1] = '\0';
char* cp = Q_strrchr(gamedir, '/') + 1; char *cp = Q_strrchr(gamedir, '/') + 1;
if (cp) {
Q_strncpy(g_GameDLL.name, cp, sizeof g_GameDLL.name - 1); Q_strlcpy(g_GameDLL.name, cp);
g_GameDLL.name[sizeof g_GameDLL.name - 1] = '\0'; }
} }
else { else {
// New style; GET_GAME_DIR returned game name. Copy this into our // 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_snprintf(g_GameDLL.gamedir, sizeof g_GameDLL.gamedir, "%s/%s", buf, gamedir);
Q_strncpy(g_GameDLL.name, gamedir, sizeof g_GameDLL.name - 1); Q_strlcpy(g_GameDLL.name, gamedir);
g_GameDLL.name[sizeof g_GameDLL.name - 1] = '\0';
} }
META_DEBUG(3, "Game: %s", g_GameDLL.name); META_DEBUG(3, "Game: %s", g_GameDLL.name);

View File

@ -4,8 +4,7 @@
MPluginList::MPluginList(const char* ifile) : m_last_index(0), m_plugins() MPluginList::MPluginList(const char* ifile) : m_last_index(0), m_plugins()
{ {
// store filename of ini file // store filename of ini file
Q_strncpy(m_inifile, ifile, sizeof m_inifile - 1); Q_strlcpy(m_inifile, ifile);
m_inifile[sizeof m_inifile - 1] = '\0';
} }
plugins_t* MPluginList::getPlugins() plugins_t* MPluginList::getPlugins()
@ -199,8 +198,7 @@ MPlugin* MPluginList::add(MPlugin* padd)
plug->m_index = ++m_last_index; plug->m_index = ++m_last_index;
// copy filename into this free slot // copy filename into this free slot
Q_strncpy(plug->m_filename, padd->m_filename, sizeof plug->m_filename - 1); Q_strlcpy(plug->m_filename, padd->m_filename);
plug->m_filename[sizeof plug->m_filename - 1] = '\0';
// Copy file offset ptr. // Copy file offset ptr.
// Can't just copy ptr, as it points to offset in padd, which will go // 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); plug->m_file = plug->m_filename + (padd->m_file - padd->m_filename);
// copy description // copy description
Q_strncpy(plug->m_desc, padd->m_desc, sizeof plug->m_desc - 1); Q_strlcpy(plug->m_desc, padd->m_desc);
plug->m_desc[sizeof plug->m_desc - 1] = '\0';
// copy pathname // copy pathname
Q_strncpy(plug->m_pathname, padd->m_pathname, sizeof plug->m_pathname - 1); Q_strlcpy(plug->m_pathname, padd->m_pathname);
plug->m_pathname[sizeof plug->m_pathname - 1] = '\0';
normalize_path(plug->m_pathname); normalize_path(plug->m_pathname);
plug->m_source = padd->m_source; plug->m_source = padd->m_source;
@ -364,11 +360,9 @@ bool MPluginList::ini_refresh()
} }
else { else {
// This plugin is already in the current list of plugins. // This plugin is already in the current list of plugins.
// Pathname already matches. Recopy desc, if specified in // Pathname already matches. Recopy desc, if specified in plugins.ini.
// plugins.ini.
if (pl_temp.m_desc[0] != '<') { if (pl_temp.m_desc[0] != '<') {
Q_strncpy(pl_found->m_desc, pl_temp.m_desc, sizeof pl_found->m_desc - 1); Q_strlcpy(pl_found->m_desc, pl_temp.m_desc);
pl_found->m_desc[sizeof pl_found->m_desc - 1] = '\0';
} }
// Check the file to see if it looks like it's been modified // Check the file to see if it looks like it's been modified

View File

@ -17,8 +17,7 @@ void MPlayer::set_cvar_query(const char* cvar)
} }
m_isQueried = true; m_isQueried = true;
Q_strncpy(g_cvarName, cvar, sizeof g_cvarName - 1); Q_strlcpy(g_cvarName, cvar);
g_cvarName[sizeof g_cvarName - 1] = '\0';
} }
// Unmark player as querying a client cvar // Unmark player as querying a client cvar

View File

@ -80,8 +80,7 @@ bool MPlugin::ini_parseline(char *line)
return false; return false;
} }
Q_strncpy(m_filename, token, sizeof m_filename - 1); Q_strlcpy(m_filename, token);
m_filename[sizeof m_filename - 1] = '\0';
normalize_path(m_filename); normalize_path(m_filename);
// Store name of just the actual _file_, without dir components. // 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); token = strtok_r(nullptr, "\n\r", &ptr_token);
if (token) { if (token) {
token = token + strspn(token, " \t"); // skip whitespace token = token + strspn(token, " \t"); // skip whitespace
Q_strncpy(m_desc, token, sizeof m_desc - 1); Q_strlcpy(m_desc, token);
m_desc[sizeof m_desc - 1] = '\0';
} }
else { else {
// If no description is specified, temporarily use plugin file, // 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 buf[NAME_MAX + PATH_MAX + MAX_DESC_LEN];
char *ptr_token; char *ptr_token;
Q_strncpy(buf, line, sizeof buf - 1); Q_strlcpy(buf, line);
buf[sizeof buf - 1] = '\0';
// remove "load" // remove "load"
char* token = strtok_r(buf, " \t", &ptr_token); char* token = strtok_r(buf, " \t", &ptr_token);
@ -133,8 +130,7 @@ bool MPlugin::cmd_parseline(const char *line)
if (!token) if (!token)
return false; return false;
Q_strncpy(m_filename, token, sizeof m_filename - 1); Q_strlcpy(m_filename, token);
m_filename[sizeof m_filename - 1] = '\0';
normalize_path(m_filename); normalize_path(m_filename);
// store name of just the actual _file_, without dir components // 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); token = strtok_r(nullptr, "", &ptr_token);
if (token) { if (token) {
token = token + strspn(token, " \t"); // skip whitespace token = token + strspn(token, " \t"); // skip whitespace
Q_strncpy(m_desc, token, sizeof m_desc - 1); Q_strlcpy(m_desc, token);
m_desc[sizeof m_desc - 1] = '\0';
} }
else { else {
// if no description is specified, temporarily use plugin file, // 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; m_source_plugin_index = loader_index;
Q_strncpy(m_filename, fname, sizeof m_filename - 1); Q_strlcpy(m_filename, fname);
m_filename[sizeof m_filename - 1] = '\0';
normalize_path(m_filename); normalize_path(m_filename);
//store just name of the actual _file, without path //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); META_DEBUG(2, "Resolved '%s' to file '%s'", m_filename, found);
// store pathname: the resolved path (should be absolute) // store pathname: the resolved path (should be absolute)
Q_strncpy(m_pathname, found, sizeof m_pathname - 1); Q_strlcpy(m_pathname, found);
m_pathname[sizeof m_pathname - 1] = '\0';
// store file: the name of the file (without dir) // store file: the name of the file (without dir)
char* cp = Q_strrchr(m_pathname, '/'); char* cp = Q_strrchr(m_pathname, '/');
@ -271,8 +264,7 @@ bool MPlugin::resolve()
if (!Q_strnicmp(m_pathname, g_GameDLL.gamedir, len)) if (!Q_strnicmp(m_pathname, g_GameDLL.gamedir, len))
filename += len + 1; filename += len + 1;
Q_strncpy(m_filename, filename, sizeof m_filename - 1); Q_strlcpy(m_filename, filename);
m_filename[sizeof m_filename - 1] = '\0';
return true; return true;
} }
@ -875,8 +867,7 @@ bool MPlugin::query()
// Replace temporary desc with plugin's internal name. // Replace temporary desc with plugin's internal name.
if (m_desc[0] == '<') { if (m_desc[0] == '<') {
Q_strncpy(m_desc, m_info->name, sizeof m_desc - 1); Q_strlcpy(m_desc, m_info->name);
m_desc[sizeof m_desc - 1] = '\0';
} }
META_DEBUG(6, "dll: Plugin '%s': Query successful", m_desc); META_DEBUG(6, "dll: Plugin '%s': Query successful", m_desc);

View File

@ -110,12 +110,10 @@ void MRegCmdList::show() const
if (reg->m_status == RG_VALID) { if (reg->m_status == RG_VALID) {
auto iplug = g_plugins->find(reg->m_plugid); auto iplug = g_plugins->find(reg->m_plugid);
Q_strncpy(bplug, iplug ? iplug->description() : "(unknown)", sizeof bplug - 1); Q_strlcpy(bplug, iplug ? iplug->description() : "(unknown)");
bplug[sizeof bplug - 1] = '\0';
} }
else { else {
Q_strncpy(bplug, "(unloaded)", sizeof bplug - 1); Q_strlcpy(bplug, "(unloaded)");
bplug[sizeof bplug - 1] = '\0';
} }
META_CONS(" [%*d] %-*s %-s", WIDTH_MAX_REG, ++total_count, sizeof bplug - 1, bplug, reg->m_name); 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) { for (auto reg : m_list) {
if (reg->m_status == RG_VALID) { if (reg->m_status == RG_VALID) {
auto plug = g_plugins->find(reg->m_plugid); auto plug = g_plugins->find(reg->m_plugid);
Q_strncpy(bplug, plug ? plug->description() : "(unknown)", sizeof bplug - 1); Q_strlcpy(bplug, plug ? plug->description() : "(unknown)");
bplug[sizeof bplug - 1] = '\0';
} }
else { else {
Q_strncpy(bplug, "(unloaded)", sizeof bplug - 1); Q_strlcpy(bplug, "(unloaded)");
bplug[sizeof bplug - 1] = '\0';
} }
Q_strncpy(bname, reg->m_cvar->name, sizeof bname - 1); Q_strlcpy(bname, reg->m_cvar->name);
bname[sizeof bname - 1] = '\0';
Q_snprintf(bval, sizeof bval, "%f", reg->m_cvar->value); 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); 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) if (reg->m_plugid != plugin_id)
continue; continue;
Q_strncpy(bname, reg->m_cvar->name, sizeof bname - 1); Q_strlcpy(bname, reg->m_cvar->name);
bname[sizeof bname - 1] = '\0';
Q_snprintf(bval, sizeof bval, "%f", reg->m_cvar->value); 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); META_CONS(" %-*s %*s %s", sizeof bname - 1, bname, sizeof bval - 1, bval, reg->m_cvar->string);
total_count++; total_count++;
@ -330,8 +324,7 @@ void MRegMsgList::show()
META_CONS("%-*s %5s %5s", sizeof bname - 1, "Game registered user msgs:", "msgid", "size"); META_CONS("%-*s %5s %5s", sizeof bname - 1, "Game registered user msgs:", "msgid", "size");
for (auto msg : m_list) { for (auto msg : m_list) {
Q_strncpy(bname, msg->m_name, sizeof bname - 1); Q_strlcpy(bname, msg->m_name);
bname[sizeof bname - 1] = '\0';
META_CONS(" %-*s %3d %3d", sizeof bname - 1, bname, msg->m_msgid, msg->m_size); META_CONS(" %-*s %3d %3d", sizeof bname - 1, bname, msg->m_msgid, msg->m_size);
total_count++; total_count++;
} }

View File

@ -246,8 +246,7 @@ const char* EXT_FUNC mutil_GetPluginPath(plid_t plid)
return nullptr; return nullptr;
} }
Q_strncpy(buf, plug->pathname(), sizeof buf - 1); Q_strlcpy(buf, plug->pathname());
buf[sizeof buf - 1] = '\0';
return buf; return buf;
} }
@ -281,8 +280,7 @@ const char* EXT_FUNC mutil_GetGameInfo(plid_t plid, ginfo_t type)
return nullptr; return nullptr;
} }
Q_strncpy(buf, cp, sizeof buf - 1); Q_strlcpy(buf, cp);
buf[sizeof buf - 1] = '\0';
return buf; return buf;
} }

View File

@ -132,11 +132,11 @@ bool is_file_exists_in_gamedir(const char* path)
return true; return true;
if (is_abs_path(path)) { if (is_abs_path(path)) {
Q_strncpy(buf, path, sizeof buf); Q_strlcpy(buf, path);
buf[sizeof buf - 1] = '\0'; }
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; struct stat64 st;
int ret = stat64(buf, &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. // Build pathname from filename, plus gamedir if relative path.
if (is_abs_path(path)) { if (is_abs_path(path)) {
Q_strncpy(buf, path, sizeof buf - 1); Q_strlcpy(buf, path);
buf[sizeof buf - 1] = '\0';
} }
else snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path); else snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path);