]> granicus.if.org Git - nethack/commitdiff
region memory
authornethack.allison <nethack.allison>
Mon, 3 Jul 2006 14:30:01 +0000 (14:30 +0000)
committernethack.allison <nethack.allison>
Mon, 3 Jul 2006 14:30:01 +0000 (14:30 +0000)
<Someone> wrote on Tuesday, July 27, 2004 at 06:46:15
> In the region.c function rest_regions allocates storage for the possible
> enter_msg and leave_msg strings. But the function free_region does not relese
> this storage.

Also ensures that some code that is currently ifdef'd out
makes copies of the strings into memory from alloc()
to ensure that no problems with free() result if the function
gets passed a literal string.

src/region.c

index 91ea28df05d4f47b8e75d7babc1afdc8412458f1..8d4e2d4d304243b6c9045e0c272e0d154ab8ebb2 100644 (file)
@@ -274,6 +274,10 @@ NhRegion *reg;
            free((genericptr_t) reg->rects);
        if (reg->monsters)
            free((genericptr_t) reg->monsters);
+       if (reg->enter_msg)
+           free((genericptr_t) reg->enter_msg);
+       if (reg->leave_msg)
+           free((genericptr_t) reg->leave_msg);
        free((genericptr_t) reg);
     }
 }
@@ -791,8 +795,14 @@ const char *msg_leave;
     NhRect tmprect;
     NhRegion *reg = create_region((NhRect *) 0, 0);
 
-    reg->enter_msg = msg_enter;
-    reg->leave_msg = msg_leave;
+    if (msg_enter) {
+       reg->enter_msg = (const char *) alloc(strlen(msg_enter) + 1);
+       Strcpy(reg->enter_msg, msg_enter);
+    }
+    if (msg_leave) {
+       reg->leave_msg = (const char *) alloc(strlen(msg_leave) + 1);
+       Strcpy(reg->leave_msg, msg_leave);
+    }
     tmprect.lx = x;
     tmprect.ly = y;
     tmprect.hx = x + w;