shithub: choc

Download patch

ref: 007615bc247f51b8418cbdd62c2c8b8c74a7ee08
parent: 5afef298d6adf6bd6ef852c455647f662a24c995
author: Simon Howard <[email protected]>
date: Tue Apr 1 17:54:53 EDT 2014

misc: Tweak safe string functions slightly.

Properly handle cases where a very small string buffer is specified.

--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -358,8 +358,11 @@
 
 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 @@
         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.