from BZFlag r17264: rewrite StringCopy. wcscpy is not a standard function; we have logic for doing it ourselves already (minus the len = 0 case), so handle len = 0 and do it all ourselves.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/FTFont/FTBufferFont.cpp b/src/FTFont/FTBufferFont.cpp
index 1ea6a4b..04855e5 100644
--- a/src/FTFont/FTBufferFont.cpp
+++ b/src/FTFont/FTBufferFont.cpp
@@ -180,29 +180,37 @@ inline int StringCompare(void const *a, wchar_t const *b, int len)
inline char *StringCopy(char const *s, int len)
{
- if(len >= 0)
- {
- char *s2 = (char *)malloc((len + 1) * sizeof(char));
- memcpy(s2, s, len * sizeof(char));
- s2[len] = 0;
- return s2;
- }
+ if (!s) return NULL;
+
+ if (len < 0)
+ {
+ len = (int)strlen(s);
+ }
+
+ char *s2 = (char *)malloc((len + 1) * sizeof(char));
+ if (!s2) return NULL;
- return strdup(s);
+ memcpy(s2, s, len * sizeof(char));
+ s2[len] = 0;
+ return s2;
}
inline wchar_t *StringCopy(wchar_t const *s, int len)
{
- if(len >= 0)
- {
- wchar_t *s2 = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
- memcpy(s2, s, len * sizeof(wchar_t));
- s2[len] = 0;
- return s2;
- }
+ if (!s) return NULL;
+
+ if (len < 0)
+ {
+ len = (int)wcslen(s);
+ }
+
+ wchar_t *s2 = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
+ if (!s2) return NULL;
- return wcsdup(s);
+ memcpy(s2, s, len * sizeof(wchar_t));
+ s2[len] = 0;
+ return s2;
}