replace most strncpy calls (several of them wrong) by Q_strlcpy calls

This commit is contained in:
svdijk 2013-05-11 14:44:36 +02:00
parent 4682c888f0
commit e07294b6b1
26 changed files with 64 additions and 76 deletions

View file

@ -393,7 +393,7 @@ CL_FixUpGender(void)
return;
}
strncpy(sk, skin->string, sizeof(sk) - 1);
Q_strlcpy(sk, skin->string, sizeof(sk));
if ((p = strchr(sk, '/')) != NULL)
{

View file

@ -149,7 +149,7 @@ CL_CheckForResend(void)
if ((cls.state == ca_disconnected) && Com_ServerState())
{
cls.state = ca_connecting;
strncpy(cls.servername, "localhost", sizeof(cls.servername) - 1);
Q_strlcpy(cls.servername, "localhost", sizeof(cls.servername));
/* we don't need a challenge on the localhost */
CL_SendConnectPacket();
return;
@ -215,7 +215,7 @@ CL_Connect_f(void)
CL_Disconnect();
cls.state = ca_connecting;
strncpy(cls.servername, server, sizeof(cls.servername) - 1);
Q_strlcpy(cls.servername, server, sizeof(cls.servername));
cls.connect_time = -99999; /* HACK: CL_CheckForResend() will fire immediately */
}

View file

@ -843,7 +843,7 @@ CL_ParseServerData(void)
/* game directory */
str = MSG_ReadString(&net_message);
strncpy(cl.gamedir, str, sizeof(cl.gamedir) - 1);
Q_strlcpy(cl.gamedir, str, sizeof(cl.gamedir));
/* set gamedir */
if ((*str && (!fs_gamedirvar->string || !*fs_gamedirvar->string ||
@ -902,12 +902,10 @@ CL_LoadClientinfo(clientinfo_t *ci, char *s)
char skin_filename[MAX_QPATH];
char weapon_filename[MAX_QPATH];
strncpy(ci->cinfo, s, sizeof(ci->cinfo));
ci->cinfo[sizeof(ci->cinfo) - 1] = 0;
Q_strlcpy(ci->cinfo, s, sizeof(ci->cinfo));
/* isolate the player's name */
strncpy(ci->name, s, sizeof(ci->name));
ci->name[sizeof(ci->name) - 1] = 0;
Q_strlcpy(ci->name, s, sizeof(ci->name));
t = strstr(s, "\\");
if (t)
@ -1063,8 +1061,7 @@ CL_ParseConfigString(void)
s = MSG_ReadString(&net_message);
strncpy(olds, cl.configstrings[i], sizeof(olds));
olds[sizeof(olds) - 1] = 0;
Q_strlcpy(olds, cl.configstrings[i], sizeof(olds));
strcpy(cl.configstrings[i], s);
@ -1377,7 +1374,7 @@ CL_ParseServerMessage(void)
case svc_layout:
s = MSG_ReadString(&net_message);
strncpy(cl.layout, s, sizeof(cl.layout) - 1);
Q_strlcpy(cl.layout, s, sizeof(cl.layout));
break;
case svc_playerinfo:

View file

@ -173,7 +173,7 @@ SCR_CenterPrint(char *str)
char line[64];
int i, j, l;
strncpy(scr_centerstring, str, sizeof(scr_centerstring) - 1);
Q_strlcpy(scr_centerstring, str, sizeof(scr_centerstring));
scr_centertime_off = scr_centertime->value;
scr_centertime_start = cl.time;

View file

@ -282,9 +282,9 @@ CL_PrepRefresh(void)
/* special player weapon model */
if (num_cl_weaponmodels < MAX_CLIENTWEAPONMODELS)
{
strncpy(cl_weaponmodels[num_cl_weaponmodels],
Q_strlcpy(cl_weaponmodels[num_cl_weaponmodels],
cl.configstrings[CS_MODELS + i] + 1,
sizeof(cl_weaponmodels[num_cl_weaponmodels]) - 1);
sizeof(cl_weaponmodels[num_cl_weaponmodels]));
num_cl_weaponmodels++;
}
}

View file

@ -2530,10 +2530,10 @@ M_AddToServerList(netadr_t adr, char *info)
}
local_server_netadr[m_num_servers] = adr;
strncpy(local_server_names[m_num_servers], info,
sizeof(local_server_names[0])-1);
strncpy(local_server_netadr_strings[m_num_servers], s,
sizeof(local_server_netadr_strings[0])-1);
Q_strlcpy(local_server_names[m_num_servers], info,
sizeof(local_server_names[m_num_servers]));
Q_strlcpy(local_server_netadr_strings[m_num_servers], s,
sizeof(local_server_netadr_strings[m_num_servers]));
m_num_servers++;
}
@ -3960,9 +3960,10 @@ PlayerConfig_ScanDirectories(void)
c = b;
}
strncpy(s_pmi[s_numplayermodels].displayname,
c + 1, MAX_DISPLAYNAME - 1);
strcpy(s_pmi[s_numplayermodels].directory, c + 1);
Q_strlcpy(s_pmi[s_numplayermodels].displayname, c + 1,
sizeof(s_pmi[s_numplayermodels].displayname));
Q_strlcpy(s_pmi[s_numplayermodels].directory, c + 1,
sizeof(s_pmi[s_numplayermodels].directory));
FreeFileList(pcxnames, npcxfiles);

View file

@ -126,7 +126,14 @@ Field_Draw(menufield_s *f)
f->generic.name);
}
strncpy(tempbuffer, f->buffer + f->visible_offset, f->visible_length);
if (f->visible_length < sizeof(tempbuffer))
{
Q_strlcpy(tempbuffer, f->buffer + f->visible_offset, f->visible_length + 1);
}
else
{
Q_strlcpy(tempbuffer, f->buffer + f->visible_offset, sizeof(tempbuffer));
}
Draw_Char(f->generic.x + f->generic.parent->x + 16,
f->generic.y + f->generic.parent->y - 4, 18);

View file

@ -890,12 +890,12 @@ S_Play(void)
{
if (!strrchr(Cmd_Argv(i), '.'))
{
strncpy(name, Cmd_Argv(i), sizeof(name) - 5);
strcat(name, ".wav");
Q_strlcpy(name, Cmd_Argv(i), sizeof(name) - 4);
Q_strlcat(name, ".wav", sizeof(name));
}
else
{
strncpy(name, Cmd_Argv(i), sizeof(name) - 1);
Q_strlcpy(name, Cmd_Argv(i), sizeof(name));
}
if (strstr(name, "..") || (name[0] == '/') || (name[0] == '\\'))

View file

@ -1250,8 +1250,8 @@ CMod_LoadSurfaces(lump_t *l)
for (i = 0; i < count; i++, in++, out++)
{
strncpy(out->c.name, in->texture, sizeof(out->c.name) - 1);
strncpy(out->rname, in->texture, sizeof(out->rname) - 1);
Q_strlcpy(out->c.name, in->texture, sizeof(out->c.name));
Q_strlcpy(out->rname, in->texture, sizeof(out->rname));
out->c.flags = LittleLong(in->flags);
out->c.value = LittleLong(in->value);
}

View file

@ -183,7 +183,7 @@ Com_FilePath(const char *path, char *dst, int dstSize)
}
else
{
strncpy(dst, path, dstSize);
Q_strlcpy(dst, path, dstSize);
}
}

View file

@ -202,6 +202,7 @@ void RotatePointAroundVector(vec3_t dst,
char *COM_SkipPath(char *pathname);
void COM_StripExtension(char *in, char *out);
char *COM_FileExtension(char *in);
void COM_FileBase(char *in, char *out);
void COM_FilePath(const char *in, char *out);
void COM_DefaultExtension(char *path, const char *extension);

View file

@ -455,7 +455,7 @@ ED_ParseEdict(char *data, edict_t *ent)
gi.error("ED_ParseEntity: EOF without closing brace");
}
strncpy(keyname, com_token, sizeof(keyname) - 1);
Q_strlcpy(keyname, com_token, sizeof(keyname));
/* parse value */
com_token = COM_Parse(&data);
@ -602,8 +602,8 @@ SpawnEntities(const char *mapname, char *entities, const char *spawnpoint)
memset(&level, 0, sizeof(level));
memset(g_edicts, 0, game.maxentities * sizeof(g_edicts[0]));
strncpy(level.mapname, mapname, sizeof(level.mapname) - 1);
strncpy(game.spawnpoint, spawnpoint, sizeof(game.spawnpoint) - 1);
Q_strlcpy(level.mapname, mapname, sizeof(level.mapname));
Q_strlcpy(game.spawnpoint, spawnpoint, sizeof(game.spawnpoint));
/* set client fields on player ents */
for (i = 0; i < game.maxclients; i++)
@ -875,11 +875,11 @@ SP_worldspawn(edict_t *ent)
if (ent->message && ent->message[0])
{
gi.configstring(CS_NAME, ent->message);
strncpy(level.level_name, ent->message, sizeof(level.level_name));
Q_strlcpy(level.level_name, ent->message, sizeof(level.level_name));
}
else
{
strncpy(level.level_name, level.mapname, sizeof(level.level_name));
Q_strlcpy(level.level_name, level.mapname, sizeof(level.level_name));
}
if (st.sky && st.sky[0])

View file

@ -142,7 +142,7 @@ SP_target_speaker(edict_t *ent)
}
else
{
strncpy(buffer, st.noise, sizeof(buffer));
Q_strlcpy(buffer, st.noise, sizeof(buffer));
}
ent->noise_index = gi.soundindex(buffer);
@ -186,11 +186,11 @@ Use_Target_Help(edict_t *ent, edict_t *other /* unused */, edict_t *activator /*
if (ent->spawnflags & 1)
{
strncpy(game.helpmessage1, ent->message, sizeof(game.helpmessage2) - 1);
Q_strlcpy(game.helpmessage1, ent->message, sizeof(game.helpmessage1));
}
else
{
strncpy(game.helpmessage2, ent->message, sizeof(game.helpmessage1) - 1);
Q_strlcpy(game.helpmessage2, ent->message, sizeof(game.helpmessage2));
}
game.helpchanged++;

View file

@ -1947,7 +1947,7 @@ ClientUserinfoChanged(edict_t *ent, char *userinfo)
/* set name */
s = Info_ValueForKey(userinfo, "name");
strncpy(ent->client->pers.netname, s, sizeof(ent->client->pers.netname) - 1);
Q_strlcpy(ent->client->pers.netname, s, sizeof(ent->client->pers.netname));
/* set spectator */
s = Info_ValueForKey(userinfo, "spectator");
@ -1999,8 +1999,7 @@ ClientUserinfoChanged(edict_t *ent, char *userinfo)
}
/* save off the userinfo in case we want to check something later */
strncpy(ent->client->pers.userinfo, userinfo,
sizeof(ent->client->pers.userinfo) - 1);
Q_strlcpy(ent->client->pers.userinfo, userinfo, sizeof(ent->client->pers.userinfo));
}
/*

View file

@ -775,10 +775,10 @@ WriteGame(const char *filename, qboolean autosave)
memset(str_os, 0, sizeof(str_os));
memset(str_arch, 0, sizeof(str_arch));
strncpy(str_ver, SAVEGAMEVER, sizeof(str_ver));
strncpy(str_game, GAMEVERSION, sizeof(str_game));
strncpy(str_os, OS, sizeof(str_os));
strncpy(str_arch, ARCH, sizeof(str_arch));
Q_strlcpy(str_ver, SAVEGAMEVER, sizeof(str_ver));
Q_strlcpy(str_game, GAMEVERSION, sizeof(str_game));
Q_strlcpy(str_os, OS, sizeof(str_os));
Q_strlcpy(str_arch, ARCH, sizeof(str_arch));
fwrite(str_ver, sizeof(str_ver), 1, f);
fwrite(str_game, sizeof(str_game), 1, f);

View file

@ -81,16 +81,13 @@ LoadJPG(char *origname, byte **pic, int *width, int *height)
struct jpeg_decompress_struct cinfo;
char filename[256];
struct jpeg_error_mgr jerr;
int len;
byte *rawdata, *rgbadata, *scanline, *p, *q;
unsigned int rawsize, i;
strncpy(filename, origname, sizeof(filename) - 1);
filename[sizeof(filename) - 1] = '\0';
Q_strlcpy(filename, origname, sizeof(filename));
/* Add the extension */
len = strlen(filename);
if (len >= 4 && strcmp(filename + len - 4, ".jpg"))
if (strcmp(COM_FileExtension(filename), "jpg"))
{
Q_strlcat(filename, ".jpg", sizeof(filename));
}

View file

@ -33,17 +33,14 @@ LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height)
pcx_t *pcx;
int x, y;
int len;
int filelen;
int dataByte, runLength;
byte *out, *pix;
char filename[256];
strncpy(filename, origname, sizeof(filename) - 1);
filename[sizeof(filename) - 1] = '\0';
Q_strlcpy(filename, origname, sizeof(filename));
/* Add the extension */
filelen = strlen(filename);
if (filelen >= 4 && strcmp(filename + filelen - 4, ".pcx"))
if (strcmp(COM_FileExtension(filename), "pcx"))
{
Q_strlcat(filename, ".pcx", sizeof(filename));
}

View file

@ -48,14 +48,11 @@ LoadTGA(char *origname, byte **pic, int *width, int *height)
int length;
int pixel_size;
char name[256];
int len;
strncpy(name, origname, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
len = strlen(name);
if (len >= 4 && strcmp(name + len - 4, ".tga"))
if (strcmp(COM_FileExtension(name), "tga"))
{
Q_strlcat(name, ".tga", sizeof(name));
}

View file

@ -32,15 +32,12 @@ LoadWal(char *origname)
miptex_t *mt;
int width, height, ofs;
image_t *image;
int len;
char name[256];
strncpy(name, origname, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
Q_strlcpy(name, origname, sizeof(name));
/* Add the extension */
len = strlen(name);
if (len >= 4 && strcmp(name + len - 4, ".wal"))
if (strcmp(COM_FileExtension(name), "wal"))
{
Q_strlcat(name, ".wal", sizeof(name));
}

View file

@ -1184,12 +1184,10 @@ R_Init(void *hinstance, void *hWnd)
gl_config.extensions_string = (char *)qglGetString(GL_EXTENSIONS);
ri.Con_Printf(PRINT_ALL, "GL_EXTENSIONS: %s\n", gl_config.extensions_string);
strncpy(renderer_buffer, gl_config.renderer_string, sizeof(renderer_buffer));
renderer_buffer[sizeof(renderer_buffer) - 1] = 0;
Q_strlcpy(renderer_buffer, gl_config.renderer_string, sizeof(renderer_buffer));
Q_strlwr(renderer_buffer);
strncpy(vendor_buffer, gl_config.vendor_string, sizeof(vendor_buffer));
vendor_buffer[sizeof(vendor_buffer) - 1] = 0;
Q_strlcpy(vendor_buffer, gl_config.vendor_string, sizeof(vendor_buffer));
Q_strlwr(vendor_buffer);
ri.Cvar_Set("scr_drawall", "0");

View file

@ -699,7 +699,7 @@ R_SetSky(char *name, float rotate, vec3_t axis)
int i;
char pathname[MAX_QPATH];
strncpy(skyname, name, sizeof(skyname) - 1);
Q_strlcpy(skyname, name, sizeof(skyname));
skyrotate = rotate;
VectorCopy(axis, skyaxis);

View file

@ -230,7 +230,7 @@ SV_GameMap_f(void)
SV_Map(false, Cmd_Argv(1), false);
/* archive server state */
strncpy(svs.mapcmd, Cmd_Argv(1), sizeof(svs.mapcmd) - 1);
Q_strlcpy(svs.mapcmd, Cmd_Argv(1), sizeof(svs.mapcmd));
/* copy off the level to the autosave slot */
if (!dedicated->value)

View file

@ -178,8 +178,7 @@ SVC_DirectConnect(void)
challenge = (int)strtol(Cmd_Argv(3), (char **)NULL, 10);
strncpy(userinfo, Cmd_Argv(4), sizeof(userinfo) - 1);
userinfo[sizeof(userinfo) - 1] = 0;
Q_strlcpy(userinfo, Cmd_Argv(4), sizeof(userinfo));
/* force the IP key/value pair so the game can filter based on ip */
Info_SetValueForKey(userinfo, "ip", NET_AdrToString(net_from));

View file

@ -57,7 +57,7 @@ SV_FindIndex(char *name, int start, int max, qboolean create)
Com_Error(ERR_DROP, "*Index: overflow");
}
strncpy(sv.configstrings[start + i], name, sizeof(sv.configstrings[i]));
Q_strlcpy(sv.configstrings[start + i], name, sizeof(sv.configstrings[start + i]));
if (sv.state != ss_loading)
{

View file

@ -536,8 +536,7 @@ SV_UserinfoChanged(client_t *cl)
ge->ClientUserinfoChanged(cl->edict, cl->userinfo);
/* name for C code */
strncpy(cl->name, Info_ValueForKey(cl->userinfo,
"name"), sizeof(cl->name) - 1);
Q_strlcpy(cl->name, Info_ValueForKey(cl->userinfo, "name"), sizeof(cl->name));
/* mask off high bit */
for (i = 0; i < sizeof(cl->name); i++)

View file

@ -544,8 +544,7 @@ SV_ExecuteClientMessage(client_t *cl)
break;
case clc_userinfo:
strncpy(cl->userinfo, MSG_ReadString(&net_message),
sizeof(cl->userinfo) - 1);
Q_strlcpy(cl->userinfo, MSG_ReadString(&net_message), sizeof(cl->userinfo));
SV_UserinfoChanged(cl);
break;