From e6c4838161c517eb22f9fd6605a12112419ed423 Mon Sep 17 00:00:00 2001 From: Mika Kuoppala Date: Mon, 4 Dec 2023 23:08:12 +0200 Subject: [PATCH] sp_lvl: fix memory leak on lspo_region If tutorial is entered, we get following leak on exit: ================================================================= ==81358==ERROR: LeakSanitizer: detected memory leaks Direct leak of 96 byte(s) in 3 object(s) allocated from: #0 0x7f6996edefdf in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0x5601c255bcbb in alloc /home/miku/src/NetHack/src/alloc.c:71 Indirect leak of 5064 byte(s) in 3 object(s) allocated from: #0 0x7f6996edefdf in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0x5601c255be1e in alloc /home/miku/src/NetHack/src/alloc.c:71 #2 0x5601c255be1e in dupstr /home/miku/src/NetHack/src/alloc.c:236 SUMMARY: AddressSanitizer: 5160 byte(s) leaked in 6 allocation(s). Fix this by freeing the cloned selection before returning. --- src/sp_lev.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sp_lev.c b/src/sp_lev.c index 4e96b52d98..c6f9b8b725 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -6191,6 +6191,8 @@ lspo_region(lua_State *L) selection_do_grow(sel, W_ANY); selection_iterate(sel, sel_set_lit, (genericptr_t) &rlit); + selection_free(sel, TRUE); + /* TODO: skip the rest of this function? */ return 0; } else {