From be083909b709e05da7c2891bc0da59059e99f758 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Tue, 7 Oct 2025 12:06:54 +0200 Subject: [PATCH] target: riscv: fix memory leak in riscv_openocd_step_impl() The array 'wps_to_enable' is never freed. Scan build reports: src/target/riscv/riscv.c:4271:6: warning: Potential leak of memory pointed to by 'wps_to_enable' [unix.Malloc] Add the needed free(). While there, check if the allocation is successful. Change-Id: I00e7ade37a43a97dcc245113ad93c48784fce609 Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/9163 Reviewed-by: Evgeniy Naydanov Reviewed-by: Tomas Vanek Tested-by: jenkins --- src/target/riscv/riscv.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index 7ad695c20..9d858276b 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -4210,9 +4210,15 @@ static int riscv_openocd_step_impl(struct target *target, bool current, RISCV_INFO(r); bool *wps_to_enable = calloc(r->trigger_count, sizeof(*wps_to_enable)); + if (!wps_to_enable) { + LOG_ERROR("Out of memory"); + return ERROR_FAIL; + } + if (disable_watchpoints(target, wps_to_enable) != ERROR_OK) { LOG_TARGET_ERROR(target, "Failed to temporarily disable " "watchpoints before single-step."); + free(wps_to_enable); return ERROR_FAIL; } @@ -4257,6 +4263,8 @@ _exit: "after single-step."); } + free(wps_to_enable); + if (breakpoint && (riscv_add_breakpoint(target, breakpoint) != ERROR_OK)) { success = false; LOG_TARGET_ERROR(target, "Unable to restore the disabled breakpoint.");