]> granicus.if.org Git - nethack/commitdiff
plug a couple of memory leaks in sp_lev.c, take II
authorPatR <rankin@nethack.org>
Thu, 13 Feb 2020 02:56:41 +0000 (18:56 -0800)
committerPatR <rankin@nethack.org>
Thu, 13 Feb 2020 02:56:41 +0000 (18:56 -0800)
[...]
| Change selection_free(foo) to also free(foo) after freeing foo's
| fields.  Every use was already
|   selection_free(foo);
|   free(foo);
| except for the two instances of memory leak.

And except for the three which aren't in sp_lev.c, one of which was
dealing with memory managed by Lua.  This time it seems to be working
as intended.

include/extern.h
src/do_name.c
src/nhlsel.c
src/sp_lev.c

index b5e8d46e526e280486151654f29e74d7fe43b262..887fff29a511c053c3d0308857c11bd4079aad85 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.6 extern.h        $NHDT-Date: 1581322657 2020/02/10 08:17:37 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.795 $ */
+/* NetHack 3.6 extern.h        $NHDT-Date: 1581562570 2020/02/13 02:56:10 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.796 $ */
 /* Copyright (c) Steve Creps, 1988.                              */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -2471,7 +2471,7 @@ E void FDECL(fill_room, (struct mkroom *, BOOLEAN_P));
 E boolean FDECL(load_special, (const char *));
 E xchar FDECL(selection_getpoint, (int, int, struct selectionvar *));
 E struct selectionvar *NDECL(selection_new);
-E void FDECL(selection_free, (struct selectionvar *));
+E void FDECL(selection_free, (struct selectionvar *, BOOLEAN_P));
 #if !defined(IN_SP_LEV_C)
 E void FDECL(set_selection_floodfillchk, (int FDECL((*), (int,int))));
 #endif
index 97f48c9b11eebce5c0ecd4c88441bc706439f832..791a74fde652815eff60f0a2d332b07674b85446 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.6 do_name.c       $NHDT-Date: 1578764034 2020/01/11 17:33:54 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.169 $ */
+/* NetHack 3.6 do_name.c       $NHDT-Date: 1581562587 2020/02/13 02:56:27 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.172 $ */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /*-Copyright (c) Pasi Kallinen, 2018. */
 /* NetHack may be freely redistributed.  See license for details. */
@@ -326,7 +326,7 @@ void
 gloc_filter_done()
 {
     if (g.gloc_filter_map) {
-        selection_free(g.gloc_filter_map);
+        selection_free(g.gloc_filter_map, TRUE);
         g.gloc_filter_map = (struct selectionvar *) 0;
 
     }
index d53ae07bc95f6a559c7d5e96a456304dce7ec98c..fba73357cf6389fd1bdf12d49cb74ef253248538 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 nhlua.c $NHDT-Date: 1581280068 2020/02/09 20:27:48 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.8 $ */
+/* NetHack 3.7 nhlua.c $NHDT-Date: 1581562591 2020/02/13 02:56:31 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.9 $ */
 /*      Copyright (c) 2018 by Pasi Kallinen */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -54,7 +54,7 @@ int index;
     struct selectionvar *sel;
 
     luaL_checktype(L, index, LUA_TUSERDATA);
-    sel = (struct selectionvar *)luaL_checkudata(L, index, "selection");
+    sel = (struct selectionvar *) luaL_checkudata(L, index, "selection");
     if (!sel)
         nhl_error(L, "Selection error");
     return sel;
@@ -65,8 +65,9 @@ l_selection_gc(L)
 lua_State *L;
 {
     struct selectionvar *sel = l_selection_check(L, 1);
+
     if (sel)
-        selection_free(sel);
+        selection_free(sel, FALSE);
     return 0;
 }
 
@@ -89,15 +90,16 @@ l_selection_push(L)
 lua_State *L;
 {
     struct selectionvar *tmp = selection_new();
-    struct selectionvar *sel = (struct selectionvar *)lua_newuserdata(L, sizeof(struct selectionvar));
+    struct selectionvar
+        *sel = (struct selectionvar *) lua_newuserdata(L, sizeof sel);
+
     luaL_getmetatable(L, "selection");
     lua_setmetatable(L, -2);
 
     sel->wid = tmp->wid;
     sel->hei = tmp->hei;
     sel->map = dupstr(tmp->map);
-    selection_free(tmp);
-    free(tmp);
+    selection_free(tmp, TRUE);
 
     return sel;
 }
index 7317130d61d94b66dc37720295636a5a20511d97..dc2df6a77bb8259037b123e8aaa821efad9671cd 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.6 sp_lev.c        $NHDT-Date: 1580610435 2020/02/02 02:27:15 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.153 $ */
+/* NetHack 3.6 sp_lev.c        $NHDT-Date: 1581562593 2020/02/13 02:56:33 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.157 $ */
 /*      Copyright (c) 1989 by Jean-Christophe Collet */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -3581,12 +3581,11 @@ lua_State *L UNUSED;
 struct selectionvar *
 selection_new()
 {
-    struct selectionvar *
-        tmps = (struct selectionvar *) alloc(sizeof (struct selectionvar));
+    struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof tmps);
 
     tmps->wid = COLNO;
     tmps->hei = ROWNO;
-    tmps->map = (char *)alloc((COLNO * ROWNO) + 1);
+    tmps->map = (char *) alloc((COLNO * ROWNO) + 1);
     (void) memset(tmps->map, 1, (COLNO * ROWNO));
     tmps->map[(COLNO * ROWNO)] = '\0';
 
@@ -3594,14 +3593,18 @@ selection_new()
 }
 
 void
-selection_free(sel)
+selection_free(sel, freesel)
 struct selectionvar *sel;
+boolean freesel;
 {
-    if (!sel)
-        return;
-    Free(sel->map);
-    sel->map = NULL;
-    sel->wid = sel->hei = 0;
+    if (sel) {
+        Free(sel->map);
+        sel->map = NULL;
+        if (freesel)
+            free((genericptr_t) sel);
+        else
+            sel->wid = sel->hei = 0;
+    }
 }
 
 struct selectionvar *
@@ -3811,8 +3814,7 @@ int dir;
             if (selection_getpoint(x, y, tmp))
                 selection_setpoint(x, y, ov, 1);
 
-    selection_free(tmp);
-    free(tmp);
+    selection_free(tmp, TRUE);
 }
 
 static int FDECL((*selection_flood_check_func), (int, int));
@@ -3897,7 +3899,7 @@ boolean diagonals;
     xchar dy[SEL_FLOOD_STACK];
 
     if (selection_flood_check_func == (int FDECL((*), (int, int))) 0) {
-        selection_free(tmp);
+        selection_free(tmp, TRUE);
         return;
     }
     SEL_FLOOD(x, y);
@@ -3923,8 +3925,7 @@ boolean diagonals;
 #undef SEL_FLOOD
 #undef SEL_FLOOD_STACK
 #undef SEL_FLOOD_CHKDIR
-    selection_free(tmp);
-    free(tmp);
+    selection_free(tmp, TRUE);
 }
 
 /* McIlroy's Ellipse Algorithm */
@@ -4554,7 +4555,7 @@ struct selectionvar *ov;
 
     /* try to make a hole or a trapdoor */
     if (Can_fall_thru(&u.uz)) {
-        selection_free(ov3);
+        selection_free(ov3, TRUE);
         ov3 = selection_clone(ov2);
         while (selection_rndcoord(ov3, &x, &y, TRUE)) {
             if (maketrap(x,y, rn2(2) ? HOLE : TRAPDOOR))
@@ -4570,10 +4571,8 @@ struct selectionvar *ov;
 
     res = FALSE;
  gotitdone:
-    selection_free(ov2);
-    free(ov2);
-    selection_free(ov3);
-    free(ov3);
+    selection_free(ov2, TRUE);
+    selection_free(ov3, TRUE);
     return res;
 }
 
@@ -4606,19 +4605,19 @@ ensure_way_out()
 
     do {
         ret = TRUE;
-        for (x = 0; x < COLNO; x++)
+        for (x = 1; x < COLNO; x++)
             for (y = 0; y < ROWNO; y++)
                 if (ACCESSIBLE(levl[x][y].typ)
                     && !selection_getpoint(x, y, ov)) {
-                    if (generate_way_out_method(x,y, ov))
-                        selection_floodfill(ov, x,y, TRUE);
+                    if (generate_way_out_method(x, y, ov))
+                        selection_floodfill(ov, x, y, TRUE);
                     ret = FALSE;
                     goto outhere;
                 }
- outhere: ;
+ outhere:
+        ;
     } while (!ret);
-    selection_free(ov);
-    free(ov);
+    selection_free(ov, TRUE);
 }
 
 int
@@ -5181,9 +5180,9 @@ int prop;
 
     create_des_coder();
 
-    if (argc == 1)
+    if (argc == 1) {
         sel = l_selection_check(L, -1);
-    else if (argc == 0) {
+    else if (argc == 0) {
         freesel = TRUE;
         sel = selection_new();
         selection_not(sel);
@@ -5191,10 +5190,8 @@ int prop;
 
     if (sel) {
         selection_iterate(sel, sel_set_wall_property, (genericptr_t) &prop);
-        if (freesel) {
-            selection_free(sel);
-            free(sel);
-        }
+        if (freesel)
+            selection_free(sel, TRUE);
     }
 }