Import Q_strlcpy() and replace several dangerous strncpy() with it.

This commit is contained in:
Yamagi Burmeister 2018-10-25 19:26:17 +02:00
parent 949e9aa008
commit 433a1423d9
4 changed files with 41 additions and 3 deletions

View file

@ -1101,11 +1101,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

@ -119,7 +119,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);

View file

@ -184,6 +184,8 @@ void Com_sprintf(char *dest, int size, char *fmt, ...);
void Com_PageInMemory(byte *buffer, int size);
char *strlwr ( char *s );
int Q_strlcpy(char *dst, const char *src, int size);
int Q_strlcat(char *dst, const char *src, int size);
/* ============================================= */

View file

@ -1098,6 +1098,42 @@ strlwr ( char *s )
return ( p );
}
int
Q_strlcpy(char *dst, const char *src, int size)
{
const char *s = src;
while (*s)
{
if (size > 1)
{
*dst++ = *s;
size--;
}
s++;
}
if (size > 0)
{
*dst = '\0';
}
return s - src;
}
int
Q_strlcat(char *dst, const char *src, int size)
{
char *d = dst;
while (size > 0 && *d)
{
size--;
d++;
}
return (d - dst) + Q_strlcpy(d, src, size);
}
/*
* =====================================================================
*