Remove strncopy().

This commit is contained in:
David Anderson 2015-09-05 21:44:18 -07:00
parent 67ba703b84
commit e08697ad54
13 changed files with 37 additions and 39 deletions

View File

@ -1,5 +1,5 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod * SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
@ -37,6 +37,7 @@
#include "HalfLife2.h" #include "HalfLife2.h"
#include "logic_bridge.h" #include "logic_bridge.h"
#include "sourcemod.h" #include "sourcemod.h"
#include <amtl/am-string.h>
#if SOURCE_ENGINE == SE_DOTA #if SOURCE_ENGINE == SE_DOTA
SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &); SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
@ -433,7 +434,7 @@ bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args)
*/ */
char new_buf[80]; char new_buf[80];
strcpy(new_buf, "sm_"); strcpy(new_buf, "sm_");
strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3); ke::SafeStrcpy(&new_buf[3], sizeof(new_buf)-3, cmd_buf);
/* Recheck */ /* Recheck */
if (!g_ConCmds.LookForSourceModCommand(new_buf)) if (!g_ConCmds.LookForSourceModCommand(new_buf))
@ -454,10 +455,10 @@ bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args)
{ {
len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args); len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
} else { } else {
len = strncopy(m_ToExecute, args, sizeof(m_ToExecute)); len = ke::SafeStrcpy(m_ToExecute, sizeof(m_ToExecute), args);
} }
} else { } else {
strncopy(m_ToExecute, args, sizeof(m_ToExecute)); ke::SafeStrcpy(m_ToExecute, sizeof(m_ToExecute), args);
} }
return true; return true;

View File

@ -434,7 +434,7 @@ bool SM_ExecuteConfig(IPlugin *pl, AutoConfig *cfg, bool can_create)
char *dptr = descr; char *dptr = descr;
/* Print comments until there is no more */ /* Print comments until there is no more */
strncopy(descr, cvar->GetHelpText(), sizeof(descr)); ke::SafeStrcpy(descr, sizeof(descr), cvar->GetHelpText());
while (*dptr != '\0') while (*dptr != '\0')
{ {
/* Find the next line */ /* Find the next line */

View File

@ -831,7 +831,7 @@ void CHalfLife2::PushCommandStack(const CCommand *cmd)
info.args = cmd; info.args = cmd;
#if SOURCE_ENGINE <= SE_DARKMESSIAH #if SOURCE_ENGINE <= SE_DARKMESSIAH
strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd)); ke::SafeStrcpy(info.cmd, sizeof(info.cmd), cmd->Arg(0));
#endif #endif
m_CommandStack.push(info); m_CommandStack.push(info);
@ -1247,7 +1247,7 @@ SMFindMapResult CHalfLife2::FindMap(char *pMapName, int nMapNameMax)
} }
else else
{ {
strncopy(pMapName, &results[0][helperCmdLen + 1], nMapNameMax); ke::SafeStrcpy(pMapName, nMapNameMax, &results[0][helperCmdLen + 1]);
return SMFindMapResult::FuzzyMatch; return SMFindMapResult::FuzzyMatch;
} }
#elif SOURCE_ENGINE == SE_TF2 #elif SOURCE_ENGINE == SE_TF2
@ -1263,7 +1263,7 @@ bool CHalfLife2::IsMapValid(const char *map)
return false; return false;
static char szTmp[PLATFORM_MAX_PATH]; static char szTmp[PLATFORM_MAX_PATH];
strncopy(szTmp, map, sizeof(szTmp)); ke::SafeStrcpy(szTmp, sizeof(szTmp), map);
return FindMap(szTmp, sizeof(szTmp)) != SMFindMapResult::NotFound; return FindMap(szTmp, sizeof(szTmp)) != SMFindMapResult::NotFound;
} }

View File

@ -1,5 +1,5 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod * SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
@ -382,7 +382,7 @@ bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
{ {
if (option == MenuOption_IntroMessage) if (option == MenuOption_IntroMessage)
{ {
strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg)); ke::SafeStrcpy(m_IntroMsg, sizeof(m_IntroMsg), (const char *)valuePtr);
return true; return true;
} else if (option == MenuOption_IntroColor) { } else if (option == MenuOption_IntroColor) {
unsigned int *array = (unsigned int *)valuePtr; unsigned int *array = (unsigned int *)valuePtr;

View File

@ -1657,7 +1657,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
{ {
info->targets[0] = client; info->targets[0] = client;
info->num_targets = 1; info->num_targets = 1;
strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pTarget->GetName());
info->target_name_style = COMMAND_TARGETNAME_RAW; info->target_name_style = COMMAND_TARGETNAME_RAW;
} }
else else
@ -1731,7 +1731,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
{ {
info->targets[0] = i; info->targets[0] = i;
info->num_targets = 1; info->num_targets = 1;
strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pTarget->GetName());
info->target_name_style = COMMAND_TARGETNAME_RAW; info->target_name_style = COMMAND_TARGETNAME_RAW;
} }
else else
@ -1761,7 +1761,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
{ {
info->targets[0] = i; info->targets[0] = i;
info->num_targets = 1; info->num_targets = 1;
strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pTarget->GetName());
info->target_name_style = COMMAND_TARGETNAME_RAW; info->target_name_style = COMMAND_TARGETNAME_RAW;
} }
else else
@ -1780,7 +1780,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
{ {
info->targets[0] = info->admin; info->targets[0] = info->admin;
info->num_targets = 1; info->num_targets = 1;
strncopy(info->target_name, pAdmin->GetName(), info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pAdmin->GetName());
info->target_name_style = COMMAND_TARGETNAME_RAW; info->target_name_style = COMMAND_TARGETNAME_RAW;
} }
else else
@ -1799,7 +1799,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
if (strcmp(info->pattern, "@all") == 0) if (strcmp(info->pattern, "@all") == 0)
{ {
is_multi = true; is_multi = true;
strncopy(info->target_name, "all players", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all players");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
} }
else if (strcmp(info->pattern, "@dead") == 0) else if (strcmp(info->pattern, "@dead") == 0)
@ -1812,7 +1812,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
return; return;
} }
info->flags |= COMMAND_FILTER_DEAD; info->flags |= COMMAND_FILTER_DEAD;
strncopy(info->target_name, "all dead players", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all dead players");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
} }
else if (strcmp(info->pattern, "@alive") == 0) else if (strcmp(info->pattern, "@alive") == 0)
@ -1824,7 +1824,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
info->reason = COMMAND_TARGET_NOT_DEAD; info->reason = COMMAND_TARGET_NOT_DEAD;
return; return;
} }
strncopy(info->target_name, "all alive players", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all alive players");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
info->flags |= COMMAND_FILTER_ALIVE; info->flags |= COMMAND_FILTER_ALIVE;
} }
@ -1837,21 +1837,21 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
info->reason = COMMAND_TARGET_NOT_HUMAN; info->reason = COMMAND_TARGET_NOT_HUMAN;
return; return;
} }
strncopy(info->target_name, "all bots", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all bots");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
bots_only = true; bots_only = true;
} }
else if (strcmp(info->pattern, "@humans") == 0) else if (strcmp(info->pattern, "@humans") == 0)
{ {
is_multi = true; is_multi = true;
strncopy(info->target_name, "all humans", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all humans");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
info->flags |= COMMAND_FILTER_NO_BOTS; info->flags |= COMMAND_FILTER_NO_BOTS;
} }
else if (strcmp(info->pattern, "@!me") == 0) else if (strcmp(info->pattern, "@!me") == 0)
{ {
is_multi = true; is_multi = true;
strncopy(info->target_name, "all players", info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all players");
info->target_name_style = COMMAND_TARGETNAME_ML; info->target_name_style = COMMAND_TARGETNAME_ML;
skip_client = info->admin; skip_client = info->admin;
} }
@ -1923,7 +1923,7 @@ void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
{ {
info->targets[0] = found_client; info->targets[0] = found_client;
info->num_targets = 1; info->num_targets = 1;
strncopy(info->target_name, pFoundClient->GetName(), info->target_name_maxlength); ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pFoundClient->GetName());
info->target_name_style = COMMAND_TARGETNAME_RAW; info->target_name_style = COMMAND_TARGETNAME_RAW;
} }
else else
@ -2071,7 +2071,7 @@ void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
m_Serial.bits.serial = g_PlayerSerialCount++; m_Serial.bits.serial = g_PlayerSerialCount++;
char ip2[24], *ptr; char ip2[24], *ptr;
strncopy(ip2, ip, sizeof(ip2)); ke::SafeStrcpy(ip2, sizeof(ip2), ip);
if ((ptr = strchr(ip2, ':')) != NULL) if ((ptr = strchr(ip2, ':')) != NULL)
{ {
*ptr = '\0'; *ptr = '\0';

View File

@ -1,5 +1,5 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod * SourceMod
* Copyright (C) 2013 AlliedModders LLC. All rights reserved. * Copyright (C) 2013 AlliedModders LLC. All rights reserved.
@ -34,6 +34,7 @@
#include <sh_list.h> #include <sh_list.h>
#include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.h>
#include <amtl/am-string.h>
#define GETCHECK_FIELD() \ #define GETCHECK_FIELD() \
const protobuf::FieldDescriptor *field = msg->GetDescriptor()->FindFieldByName(pszFieldName); \ const protobuf::FieldDescriptor *field = msg->GetDescriptor()->FindFieldByName(pszFieldName); \
@ -672,7 +673,7 @@ public:
CHECK_FIELD_NOT_REPEATED(); CHECK_FIELD_NOT_REPEATED();
std::string scratch; std::string scratch;
strncopy(out, msg->GetReflection()->GetStringReference(*msg, field, &scratch).c_str(), size); ke::SafeStrcpy(out, size, msg->GetReflection()->GetStringReference(*msg, field, &scratch).c_str());
return true; return true;
} }
@ -695,7 +696,7 @@ public:
CHECK_REPEATED_ELEMENT(index); CHECK_REPEATED_ELEMENT(index);
std::string scratch; std::string scratch;
strncopy(out, msg->GetReflection()->GetRepeatedStringReference(*msg, field, index, &scratch).c_str(), size); ke::SafeStrcpy(out, size, msg->GetReflection()->GetRepeatedStringReference(*msg, field, index, &scratch).c_str());
return true; return true;
} }

View File

@ -38,6 +38,7 @@
#elif SOURCE_ENGINE == SE_CSGO #elif SOURCE_ENGINE == SE_CSGO
#include <cstrike15_usermessage_helpers.h> #include <cstrike15_usermessage_helpers.h>
#endif #endif
#include <amtl/am-string.h>
UserMessages g_UserMsgs; UserMessages g_UserMsgs;
@ -158,7 +159,7 @@ bool UserMessages::GetMessageName(int msgid, char *buffer, size_t maxlength) con
if (!pszName) if (!pszName)
return false; return false;
strncopy(buffer, pszName, maxlength); ke::SafeStrcpy(buffer, maxlength, pszName);
return true; return true;
#else #else
if (m_FallbackSearch) if (m_FallbackSearch)
@ -171,7 +172,7 @@ bool UserMessages::GetMessageName(int msgid, char *buffer, size_t maxlength) con
if (msg) if (msg)
{ {
strncopy(buffer, msg, maxlength); ke::SafeStrcpy(buffer, maxlength, msg);
return true; return true;
} }

View File

@ -1,5 +1,5 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod * SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
@ -36,6 +36,7 @@
#include "sm_stringutil.h" #include "sm_stringutil.h"
#include "sourcemm_api.h" #include "sourcemm_api.h"
#include "compat_wrappers.h" #include "compat_wrappers.h"
#include <amtl/am-string.h>
#if SOURCE_ENGINE >= SE_ORANGEBOX #if SOURCE_ENGINE >= SE_ORANGEBOX
SH_DECL_HOOK1_void(ICvar, UnregisterConCommand, SH_NOATTRIB, 0, ConCommandBase *); SH_DECL_HOOK1_void(ICvar, UnregisterConCommand, SH_NOATTRIB, 0, ConCommandBase *);
@ -146,7 +147,7 @@ public:
info->pBase = pBase; info->pBase = pBase;
info->cls = cls; info->cls = cls;
strncopy(info->name, pBase->GetName(), sizeof(info->name)); ke::SafeStrcpy(info->name, sizeof(info->name), pBase->GetName());
tracked_bases.push_back(info); tracked_bases.push_back(info);
} }

View File

@ -469,7 +469,7 @@ bool CoreProviderImpl::GetGameName(char *buffer, size_t maxlength)
const char *str; const char *str;
if ((str = pGameInfo->GetString("game", NULL)) != NULL) if ((str = pGameInfo->GetString("game", NULL)) != NULL)
{ {
strncopy(buffer, str, maxlength); ke::SafeStrcpy(buffer, maxlength, str);
pGameInfo->deleteThis(); pGameInfo->deleteThis();
return true; return true;
} }

View File

@ -41,11 +41,6 @@
#include <am-utility.h> #include <am-utility.h>
#include <am-float.h> #include <am-float.h>
unsigned int strncopy(char *dest, const char *src, size_t count)
{
return ke::SafeStrcpy(dest, count, src);
}
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...) size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
{ {
va_list ap; va_list ap;

View File

@ -42,7 +42,6 @@ using namespace SourceMod;
#define IS_STR_FILLED(var) (var[0] != '\0') #define IS_STR_FILLED(var) (var[0] != '\0')
unsigned int strncopy(char *dest, const char *src, size_t count);
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...); size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap); size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap);
char *sm_strdup(const char *str); char *sm_strdup(const char *str);

View File

@ -1003,7 +1003,7 @@ static cell_t SetEntDataString(IPluginContext *pContext, const cell_t *params)
char *dest = (char *)((uint8_t *)pEntity + offset); char *dest = (char *)((uint8_t *)pEntity + offset);
pContext->LocalToString(params[3], &src); pContext->LocalToString(params[3], &src);
size_t len = strncopy(dest, src, params[4]); size_t len = ke::SafeStrcpy(dest, params[4], src);
if (params[5] && (pEdict != NULL)) if (params[5] && (pEdict != NULL))
{ {
@ -2165,7 +2165,7 @@ static cell_t SetEntPropString(IPluginContext *pContext, const cell_t *params)
else else
{ {
char *dest = (char *) ((uint8_t *) pEntity + offset); char *dest = (char *) ((uint8_t *) pEntity + offset);
len = strncopy(dest, src, maxlen); len = ke::SafeStrcpy(dest, maxlen, src);
} }
if (params[2] == Prop_Send && (pEdict != NULL)) if (params[2] == Prop_Send && (pEdict != NULL))

View File

@ -152,7 +152,7 @@ bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late
{ {
if (gamepath[i] == PLATFORM_SEP_CHAR) if (gamepath[i] == PLATFORM_SEP_CHAR)
{ {
strncopy(m_ModDir, &gamepath[++i], sizeof(m_ModDir)); ke::SafeStrcpy(m_ModDir, sizeof(m_ModDir), &gamepath[++i]);
break; break;
} }
} }