]> granicus.if.org Git - nethack/commitdiff
strdup/dupstr (trunk only)
authornethack.rankin <nethack.rankin>
Sun, 29 Jan 2012 03:00:17 +0000 (03:00 +0000)
committernethack.rankin <nethack.rankin>
Sun, 29 Jan 2012 03:00:17 +0000 (03:00 +0000)
     Add dupstr() as a substitute for strdup() so that out-of-memory
handling will be consistent with the rest of nethack, and make it aware
of nethack's heap logging.  It's treated like alloc() so that its caller
can be logged for NH_HEAPLOG.

     I put it into use in a few places, but there are lots more candidates
besides the existing calls to strdup() that should be replaced.

include/global.h
src/alloc.c
src/do.c
src/do_name.c
src/eat.c

index f7514f44f0de6a9201bd07e1b80eae5835974822..3eb12b86cee93a9848ae698d0fab688548485828 100644 (file)
@@ -297,6 +297,7 @@ typedef char nhptext;
 #ifdef MONITOR_HEAP
 extern long *FDECL(nhalloc, (unsigned int,const char *,int));
 extern void FDECL(nhfree, (genericptr_t,const char *,int));
+extern char *FDECL(nhdupstr, (const char *,const char *,int));
 # ifndef __FILE__
 #  define __FILE__ ""
 # endif
@@ -305,8 +306,10 @@ extern void FDECL(nhfree, (genericptr_t,const char *,int));
 # endif
 # define alloc(a) nhalloc(a,__FILE__,(int)__LINE__)
 # define free(a) nhfree(a,__FILE__,(int)__LINE__)
+# define dupstr(s) nhdupstr(s,__FILE__,(int)__LINE__)
 #else  /* !MONITOR_HEAP */
 extern long *FDECL(alloc, (unsigned int));             /* alloc.c */
+extern char *FDECL(dupstr, (const char *));            /* ditto */
 #endif
 
 /* Used for consistency checks of various data files; declare it here so
index 54ad080ec37394914651be18b6d67d42fabf4184..08066565b211a8adaea6513b00b26e6bbed105dc 100644 (file)
@@ -1,5 +1,4 @@
 /* NetHack 3.5 alloc.c $Date$  $Revision$ */
-/*     SCCS Id: @(#)alloc.c    3.5     2006/07/07      */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -135,6 +134,28 @@ int line;
        free(ptr);
 }
 
+/* strdup() which uses our alloc() rather than libc's malloc(),
+   with caller tracking */
+char *
+nhdupstr(string, file, line)
+const char *string;
+const char *file;
+int line;
+{
+    return strcpy((char *)nhalloc(strlen(string) + 1, file, line), string);
+}
+#undef dupstr
+
 #endif /* MONITOR_HEAP */
 
+/* strdup() which uses our alloc() rather than libc's malloc();
+   not used when MONITOR_HEAP is enabled, but included unconditionally
+   in case utility programs get built using a different setting for that */
+char *
+dupstr(string)
+const char *string;
+{
+    return strcpy((char *)alloc(strlen(string) + 1), string);
+}
+
 /*alloc.c*/
index d8699db828cd2b004910e2052dfcb626d9eeaa96..4681760f8e02d84a0198907ecf890ad0ddd8fecd 100644 (file)
--- a/src/do.c
+++ b/src/do.c
@@ -1444,9 +1444,9 @@ const char *pre_msg, *post_msg;
        assign_level(&u.utolev, tolev);
 
        if (pre_msg)
-           dfr_pre_msg = strcpy((char *)alloc(strlen(pre_msg) + 1), pre_msg);
+           dfr_pre_msg = dupstr(pre_msg);
        if (post_msg)
-           dfr_post_msg = strcpy((char *)alloc(strlen(post_msg)+1), post_msg);
+           dfr_post_msg = dupstr(post_msg);
 }
 
 /* handle something like portal ejection */
@@ -1572,7 +1572,7 @@ struct obj *corpse;
 void
 revive_mon(arg, timeout)
 anything *arg;
-long timeout;
+long timeout UNUSED;
 {
     struct obj *body = arg->a_obj;
     struct permonst *mptr = &mons[body->corpsenm];
index eb234ce948c209dcc10defdfafa5431e2a5954d0..9d19a1997269049161ca5d66e3c83cd204edb1ee 100644 (file)
@@ -602,7 +602,7 @@ register struct obj *obj;
                undiscover_object(obj->otyp);
            }
        } else {
-           *str1 = strcpy((char *) alloc((unsigned)strlen(buf)+1), buf);
+           *str1 = dupstr(buf);
            discover_object(obj->otyp, FALSE, TRUE); /* possibly add to disco[] */
        }
 }
index 58eafcd2cab372a03c7e3b8cd8c18dcaf55f123a..90efd56380923bea76dac49a525afa2aab647898 100644 (file)
--- a/src/eat.c
+++ b/src/eat.c
@@ -1025,7 +1025,7 @@ register int pm;
                        "You suddenly dread being peeled and mimic %s again!" :
                        "You now prefer mimicking %s again.",
                        an(Upolyd ? youmonst.data->mname : urace.noun));
-                   eatmbuf = strcpy((char *) alloc(strlen(buf) + 1), buf);
+                   eatmbuf = dupstr(buf);
                    nomovemsg = eatmbuf;
                    afternmv = eatmdone;
                    /* ??? what if this was set before? */