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

This commit is contained in:
Yamagi Burmeister 2018-10-25 19:37:41 +02:00
parent 1e37bf132d
commit 08a9c4fdc1
4 changed files with 42 additions and 3 deletions

View File

@ -813,11 +813,11 @@ SP_worldspawn(edict_t *ent)
if (ent->message && ent->message[0]) if (ent->message && ent->message[0])
{ {
gi.configstring(CS_NAME, ent->message); 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 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]) if (st.sky && st.sky[0])

View File

@ -121,7 +121,7 @@ SP_target_speaker(edict_t *ent)
} }
else else
{ {
strncpy(buffer, st.noise, sizeof(buffer)); Q_strlcpy(buffer, st.noise, sizeof(buffer));
} }
ent->noise_index = gi.soundindex(buffer); ent->noise_index = gi.soundindex(buffer);

View File

@ -202,6 +202,8 @@ void Com_PageInMemory(byte *buffer, int size);
int Q_stricmp(const char *s1, const char *s2); int Q_stricmp(const char *s1, const char *s2);
int Q_strcasecmp(char *s1, char *s2); int Q_strcasecmp(char *s1, char *s2);
int Q_strncasecmp(char *s1, char *s2, int n); int Q_strncasecmp(char *s1, char *s2, int n);
int Q_strlcpy(char *dst, const char *src, int size);
int Q_strlcat(char *dst, const char *src, int size);
/* ============================================= */ /* ============================================= */

View File

@ -1072,6 +1072,43 @@ Q_strcasecmp(char *s1, char *s2)
return Q_strncasecmp(s1, s2, 99999); return Q_strncasecmp(s1, s2, 99999);
} }
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);
}
void void
Com_sprintf(char *dest, int size, char *fmt, ...) Com_sprintf(char *dest, int size, char *fmt, ...)
{ {