]> granicus.if.org Git - nethack/commitdiff
fix memory leaks related to selection_new()
authorSHIRAKATA Kentaro <argrath@ub32.org>
Wed, 1 Jun 2022 12:38:51 +0000 (21:38 +0900)
committerSHIRAKATA Kentaro <argrath@ub32.org>
Wed, 1 Jun 2022 12:38:51 +0000 (21:38 +0900)
selection_new() returns an address of malloc()'ed buffer.
If ov is null, this value is discarded without freeing the buffer.
To avoid this, move null-checks before calling selection_new().

Also, remove null-check of the return value of selection_new()
because it always returns non-null.

src/sp_lev.c

index ac8b6bddba3f4348c4a3eda0857e25d331c2bb1f..e47fa92da814a4d5d10b4dbcb97546217b856808 100644 (file)
@@ -4369,11 +4369,13 @@ struct selectionvar *
 selection_filter_mapchar(struct selectionvar* ov,  xchar typ, int lit)
 {
     int x, y;
-    struct selectionvar *ret = selection_new();
+    struct selectionvar *ret;
 
-    if (!ov || !ret)
+    if (!ov)
         return NULL;
 
+    ret = selection_new();
+
     for (x = 1; x < ret->wid; x++)
         for (y = 0; y < ret->hei; y++)
             if (selection_getpoint(x, y, ov)
@@ -4452,11 +4454,13 @@ void
 selection_do_grow(struct selectionvar* ov, int dir)
 {
     int x, y;
-    struct selectionvar *tmp = selection_new();
+    struct selectionvar *tmp;
 
-    if (!ov || !tmp)
+    if (!ov)
         return;
 
+    tmp = selection_new();
+
     if (dir == W_RANDOM)
         dir = random_wdir();