ref: e63a8d5e3b2ebc848e01c139756cd2096efe7c80
parent: 62b5c602821cee8ac703ebe6362f3e1fc6d9ad3c
author: Simon Howard <fraggle@gmail.com>
date: Sat Mar 29 16:37:11 EDT 2014
misc: Add M_StringConcat. Just as M_StringCopy behaves the same as strlcpy(), M_StringConcat behaves the same as strlcat(). Use this in one location.
--- a/src/doom/d_main.c
+++ b/src/doom/d_main.c
@@ -1002,10 +1002,11 @@
if (sep != NULL)
{
- chex_deh = malloc(strlen(iwadfile) + 9);
- strcpy(chex_deh, iwadfile);
+ size_t chex_deh_len = strlen(iwadfile) + 9;
+ chex_deh = malloc(chex_deh_len);
+ M_StringCopy(chex_deh, iwadfile, chex_deh_len);
chex_deh[sep - iwadfile + 1] = '\0';
- strcat(chex_deh, "chex.deh");
+ M_StringConcat(chex_deh, "chex.deh", chex_deh_len);
}
else
{
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -366,6 +366,25 @@
return strlen(dest) == strlen(src);
}
+// Safe string concat function that works like OpenBSD's strlcat().
+// Returns true if string not truncated.
+
+boolean M_StringConcat(char *dest, char *src, size_t dest_size)
+{
+ size_t offset;
+
+ offset = strlen(dest);
+ if (offset > dest_size)
+ {
+ offset = dest_size;
+ }
+
+ dest += offset;
+ dest_size -= offset;
+
+ return M_StringCopy(dest, src, dest_size);
+}
+
// Returns true if 's' begins with the specified prefix.
boolean M_StringStartsWith(char *s, char *prefix)
@@ -425,7 +444,7 @@
break;
}
- strncat(result, v, result_len);
+ M_StringConcat(result, v, result_len);
}
va_end(args);
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -44,6 +44,7 @@
void M_ForceUppercase(char *text);
char *M_StrCaseStr(char *haystack, char *needle);
boolean M_StringCopy(char *dest, char *src, size_t dest_size);
+boolean M_StringConcat(char *dest, char *src, size_t dest_size);
char *M_StringReplace(char *haystack, char *needle, char *replacement);
char *M_StringJoin(char *s, ...);
boolean M_StringStartsWith(char *s, char *prefix);