summaryrefslogtreecommitdiff
path: root/src/m_misc.c
diff options
context:
space:
mode:
authorSimon Howard2014-10-24 20:29:56 -0400
committerSimon Howard2014-10-24 20:29:56 -0400
commit9d01d090c48c74a29b4ef67e0cd204772a2193c3 (patch)
tree3a8dce09c81c6cd9db8adf266d5cc6eae366af98 /src/m_misc.c
parentb42b5269e0ad5b22acd6043429ec4013a4e76ddd (diff)
downloadchocolate-doom-9d01d090c48c74a29b4ef67e0cd204772a2193c3.tar.gz
chocolate-doom-9d01d090c48c74a29b4ef67e0cd204772a2193c3.tar.bz2
chocolate-doom-9d01d090c48c74a29b4ef67e0cd204772a2193c3.zip
Replace strdup() with M_StringDuplicate().
strdup() can theoretically fail and return NULL. This could lead to a crash or undesirable behavior. Add M_StringDuplicate() which does the same thing but exits with an error if a string cannot be allocated. This fixes #456. Thanks to Quasar for the suggestion.
Diffstat (limited to 'src/m_misc.c')
-rw-r--r--src/m_misc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/m_misc.c b/src/m_misc.c
index 8fa526dc..2e363412 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -285,6 +285,26 @@ char *M_StrCaseStr(char *haystack, char *needle)
}
//
+// Safe version of strdup() that checks the string was successfully
+// allocated.
+//
+
+char *M_StringDuplicate(const char *orig)
+{
+ char *result;
+
+ result = strdup(orig);
+
+ if (result == NULL)
+ {
+ I_Error("Failed to duplicate string (length %i)\n",
+ strlen(orig));
+ }
+
+ return result;
+}
+
+//
// String replace function.
//