summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Greffrath2015-03-27 16:26:22 +0100
committerFabian Greffrath2015-03-27 16:30:46 +0100
commitd03ee98d63748ba0e2225be221b45a250c8c5741 (patch)
tree80cf7cfb777c0b8f5776a2fa3fd025a9380d17cb
parentb34294b67473e8d3ad4b8204327e03d91bcd6214 (diff)
downloadchocolate-doom-d03ee98d63748ba0e2225be221b45a250c8c5741.tar.gz
chocolate-doom-d03ee98d63748ba0e2225be221b45a250c8c5741.tar.bz2
chocolate-doom-d03ee98d63748ba0e2225be221b45a250c8c5741.zip
Avoid calling strlen() on a potentially unterminated string
The src string may be unterminated and the call to strncpy() be terminated by reaching dest_size. Instead of calling strlen() on the src string, check if it has a NUL byte at the same position as the dest string -- if not, the string was truncated. Valgrind now gives thumbs up!
-rw-r--r--src/m_misc.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/m_misc.c b/src/m_misc.c
index 2e363412..ab3c7009 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -372,12 +372,15 @@ char *M_StringReplace(const char *haystack, const char *needle,
boolean M_StringCopy(char *dest, const char *src, size_t dest_size)
{
+ size_t len;
+
if (dest_size >= 1)
{
dest[dest_size - 1] = '\0';
strncpy(dest, src, dest_size - 1);
}
- return strlen(dest) == strlen(src);
+ len = strlen(dest);
+ return src[len] == '\0';
}
// Safe string concat function that works like OpenBSD's strlcat().