From 4faf79dccc18264cf42f8fd2e23644aff0c869de Mon Sep 17 00:00:00 2001 From: SHIRAKATA Kentaro Date: Wed, 1 Jun 2022 21:38:51 +0900 Subject: [PATCH 1/2] fix memory leaks related to selection_new() 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 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sp_lev.c b/src/sp_lev.c index ac8b6bddb..e47fa92da 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -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(); From db86ebd9e7c1425b1e0ace421bb4e970a024e4de Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 1 Jun 2022 12:40:17 -0400 Subject: [PATCH 2/2] fixes3-7-0.txt update for pull request 782 --- doc/fixes3-7-0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/fixes3-7-0.txt b/doc/fixes3-7-0.txt index 7f45b032e..ad452ac9f 100644 --- a/doc/fixes3-7-0.txt +++ b/doc/fixes3-7-0.txt @@ -1812,6 +1812,7 @@ mdisplacem stoning and gloves had test backwards (pr #773 by entrez) writing type-named scrolls (pr #551 by entrez) more cases where newcham() gives messages (pr #775 by copperwater) lua fix contributed by ToxicFrog +fix memory leaks related to selection_new() (pr #782 by argrath) Code Cleanup and Reorganization