From b49e03f77ed890b39274d94ae6267bf07a68ba98 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Sun, 26 May 2024 12:38:43 +0200 Subject: [PATCH] target/arm_tpiu_swo: Fix memory leak on error In case of fail to allocate 'obj->name', the memory allocated for 'obj->out_filename' is not freed, thus leaking. Since 'obj' is allocated with calloc(), thus zeroed, switch to use the common error exit path for both allocations of 'obj->name' and 'obj->out_filename'. Fixes: 2506ccb50915 ("target/arm_tpiu_swo: Fix division by zero") Change-Id: I412f66ddd7bf7d260cee495324058482b26ff0c5 Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/8300 Tested-by: jenkins Reviewed-by: zapb --- src/target/arm_tpiu_swo.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/target/arm_tpiu_swo.c b/src/target/arm_tpiu_swo.c index b5a488201..55a977844 100644 --- a/src/target/arm_tpiu_swo.c +++ b/src/target/arm_tpiu_swo.c @@ -965,8 +965,7 @@ static int jim_arm_tpiu_swo_create(Jim_Interp *interp, int argc, Jim_Obj *const obj->out_filename = strdup("external"); if (!obj->out_filename) { LOG_ERROR("Out of memory"); - free(obj); - return JIM_ERR; + goto err_exit; } Jim_Obj *n; @@ -974,8 +973,7 @@ static int jim_arm_tpiu_swo_create(Jim_Interp *interp, int argc, Jim_Obj *const obj->name = strdup(Jim_GetString(n, NULL)); if (!obj->name) { LOG_ERROR("Out of memory"); - free(obj); - return JIM_ERR; + goto err_exit; } /* Do the rest as "configure" options */