From 1abd3c5b0bfe93d7a21113f3dad070512ee119c6 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 20 Sep 2015 15:24:22 +0200 Subject: [PATCH] navigate march: Plug memory leaks When the player aborts the command at the movement prompt, or declines to abandon a sector, unit_move() returns without freeing the list. Found with valgrind. Broken in commit 24000b4 and commit 7c1b166, both v4.3.33. Free the list on these returns, too. Signed-off-by: Markus Armbruster --- src/lib/subs/unitsub.c | 15 ++++++++++++--- tests/navi-march/journal.log | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/subs/unitsub.c b/src/lib/subs/unitsub.c index b1ead5ca..7b903d4d 100644 --- a/src/lib/subs/unitsub.c +++ b/src/lib/subs/unitsub.c @@ -406,8 +406,13 @@ unit_move(struct emp_qelem *list) */ if (!*cp) { cp = unit_move_getpath(list, suppress_map, path); - if (!cp) + if (!cp) { + if (type == EF_SHIP) + shp_nav_put(list, player->cnum); + else + lnd_mar_put(list, player->cnum); return RET_FAIL; + } cp = unit_move_route(leader, path, sizeof(path)); if (!cp || !*cp) cp = "h"; @@ -418,13 +423,17 @@ unit_move(struct emp_qelem *list) stopping = shp_nav_dir(list, dir, player->cnum) || shp_nav_gauntlet(list, 1, player->cnum); else { - if (!moved && !lnd_abandon_askyn(list)) + if (!moved && !lnd_abandon_askyn(list)) { + lnd_mar_put(list, player->cnum); return RET_FAIL; + } stopping = lnd_mar_dir(list, dir, player->cnum) || lnd_mar_gauntlet(list, 1, player->cnum); } - if (dir == DIR_STOP) + if (dir == DIR_STOP) { + CANT_HAPPEN(!QEMPTY(list)); return RET_OK; + } moved = 1; if (stopping) cp = ""; diff --git a/tests/navi-march/journal.log b/tests/navi-march/journal.log index 13985d96..78a87b7a 100644 --- a/tests/navi-march/journal.log +++ b/tests/navi-march/journal.log @@ -1013,6 +1013,8 @@ Play#1 output Play#1 1 Leader is cav cavalry #78 Play#1 output Play#1 4 Do you really want to abandon 3,7 [yn]? Play#1 input n + Play#1 output Play#1 1 cav cavalry #78 stopped at 3,7 + Play#1 output Play#1 1 cav cavalry #79 stopped at 3,7 Play#1 output Play#1 1 command failed Play#1 output Play#1 6 0 567 Play#1 input march 84/83/80/81/82