From 007615bc247f51b8418cbdd62c2c8b8c74a7ee08 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 1 Apr 2014 21:54:53 -0400 Subject: misc: Tweak safe string functions slightly. Properly handle cases where a very small string buffer is specified. --- src/m_misc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index c280661f..4abd7ebf 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -358,8 +358,11 @@ char *M_StringReplace(const char *haystack, const char *needle, boolean M_StringCopy(char *dest, const char *src, size_t dest_size) { - strncpy(dest, src, dest_size); - dest[dest_size - 1] = '\0'; + if (dest_size >= 1) + { + dest[dest_size - 1] = '\0'; + strncpy(dest, src, dest_size - 1); + } return strlen(dest) == strlen(src); } @@ -376,10 +379,7 @@ boolean M_StringConcat(char *dest, const char *src, size_t dest_size) offset = dest_size; } - dest += offset; - dest_size -= offset; - - return M_StringCopy(dest, src, dest_size); + return M_StringCopy(dest + offset, src, dest_size - offset); } // Returns true if 's' begins with the specified prefix. -- cgit v1.2.3