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
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

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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++;
}

View File

@ -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;
}

View File

@ -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);