From c83bd69b3966a52e3c3b5a634ac9de2e729d996f Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Sun, 11 Feb 2024 18:29:46 +0100 Subject: [PATCH] target/riscv: free dm and target_list structures Fix memory leak on exit. Signed-off-by: Tomas Vanek Change-Id: I6a89ac0c93b11da35b90eec3abcc5b6fd5d1be68 --- src/target/riscv/riscv-013.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index cf318c974..ec686287d 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -280,6 +280,29 @@ static dm013_info_t *get_dm(struct target *target) return dm; } +static void riscv013_dm_free(struct target *target) +{ + RISCV013_INFO(info); + dm013_info_t *dm = info->dm; + if (!dm) + return; + + target_list_t *target_entry; + list_for_each_entry(target_entry, &dm->target_list, list) { + if (target_entry->target == target) { + list_del(&target_entry->list); + free(target_entry); + break; + } + } + + if (list_empty(&dm->target_list)) { + list_del(&dm->list); + free(dm); + } + info->dm = NULL; +} + static riscv_debug_reg_ctx_t get_riscv_debug_reg_ctx(const struct target *target) { if (!target_was_examined(target)) { @@ -1864,6 +1887,8 @@ static void deinit_target(struct target *target) if (!info) return; + riscv013_dm_free(target); + free(info->version_specific); /* TODO: free register arch_info */ info->version_specific = NULL;