summaryrefslogtreecommitdiff
path: root/src/init1.cc
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2015-02-23 09:11:57 +0100
committerBardur Arantsson <bardur@scientician.net>2015-02-23 09:11:57 +0100
commitcf3bec7340563628689e92d3be018a25fa550dbd (patch)
treeda4e0a40be0e1c350388b9fcc8890ad8b7ce7425 /src/init1.cc
parent6047df25fef41fe621a8a9ef5915874516bdec7a (diff)
init1.c: Use safer variant of strdup()
Diffstat (limited to 'src/init1.cc')
-rw-r--r--src/init1.cc21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/init1.cc b/src/init1.cc
index f00e5a54..4a646b51 100644
--- a/src/init1.cc
+++ b/src/init1.cc
@@ -1499,6 +1499,21 @@ static byte monster_ego_modify(char c)
}
/**
+ * Version of strdup() which just aborts if an allocation
+ * error occurs.
+ */
+static char *my_strdup(const char *s)
+{
+ char *p = strdup(s);
+ if (!p)
+ {
+ abort();
+ }
+ return p;
+}
+
+
+/**
* Append one string to the end of another, reallocating if
* necessary.
*/
@@ -1509,7 +1524,7 @@ static void strappend(char **s, char *t)
{
// Costs an extra allocation which could be avoided
// but this leads to simpler code.
- *s = strdup("");
+ *s = my_strdup("");
}
// We should really be preserving the original pointer and
// do something else in case of failure to realloc(), but
@@ -4226,7 +4241,7 @@ errr init_k_info_txt(FILE *fp, char *buf)
/* Advance and Save the name index */
assert(!k_ptr->name);
- k_ptr->name = strdup(s);
+ k_ptr->name = my_strdup(s);
/* Needed hack */
k_ptr->esp = 0;
@@ -7547,7 +7562,7 @@ errr init_r_info_txt(FILE *fp, char *buf)
/* Allocate name string. */
assert(!r_ptr->name); // Sanity check that we aren't overwriting anything
- r_ptr->name = strdup(s);
+ r_ptr->name = my_strdup(s);
/* HACK -- Those ones HAVE to have a set default value */
r_ptr->drops.treasure = OBJ_GENE_TREASURE;