diff options
author | Simon Howard | 2014-04-01 21:54:53 -0400 |
---|---|---|
committer | Simon Howard | 2014-04-01 21:54:53 -0400 |
commit | 007615bc247f51b8418cbdd62c2c8b8c74a7ee08 (patch) | |
tree | 150d36ebcb4210f4ce4afd8d63cb29b8f2952931 | |
parent | 5afef298d6adf6bd6ef852c455647f662a24c995 (diff) | |
download | chocolate-doom-007615bc247f51b8418cbdd62c2c8b8c74a7ee08.tar.gz chocolate-doom-007615bc247f51b8418cbdd62c2c8b8c74a7ee08.tar.bz2 chocolate-doom-007615bc247f51b8418cbdd62c2c8b8c74a7ee08.zip |
misc: Tweak safe string functions slightly.
Properly handle cases where a very small string buffer is specified.
-rw-r--r-- | src/m_misc.c | 12 |
1 files 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. |