From db34f6f0a71baa9c97794cb347dc3c74b3ef729d Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Fri, 26 Jul 2024 20:39:20 +1200 Subject: [PATCH] target: arm_dap: Fix crash in 'dap info' command The 'dap info' command was not checking that the target was an ARM before dereferencing the `arm` pointer. This would cause a crash if the current target was (say) a mem_ap. Add 'target_to_dap' function to safely get the dap Change-Id: I0c765f915f2ef7b9a7d20c934e144559ca4e5f1c Signed-off-by: Grant Ramsay Reviewed-on: https://review.openocd.org/c/openocd/+/8415 Reviewed-by: Antonio Borneo Tested-by: jenkins Reviewed-by: Tomas Vanek --- src/target/arm_dap.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/target/arm_dap.c b/src/target/arm_dap.c index 5ba5e1c38..532f192b7 100644 --- a/src/target/arm_dap.c +++ b/src/target/arm_dap.c @@ -412,6 +412,16 @@ err: return retval; } +static struct adiv5_dap *target_to_dap(const struct target *target) +{ + struct adiv5_private_config *pc = target->private_config; + + if (!target->has_dap || !target->dap_configured || !pc) + return NULL; + + return pc->dap; +} + COMMAND_HANDLER(handle_dap_names) { if (CMD_ARGC != 0) @@ -432,12 +442,11 @@ COMMAND_HANDLER(handle_dap_init) COMMAND_HANDLER(handle_dap_info_command) { struct target *target = get_current_target(CMD_CTX); - struct arm *arm = target_to_arm(target); - struct adiv5_dap *dap = arm->dap; + struct adiv5_dap *dap = target_to_dap(target); uint64_t apsel; if (!dap) { - LOG_ERROR("DAP instance not available. Probably a HLA target..."); + command_print(CMD, "target %s has no DAP", target_name(target)); return ERROR_TARGET_RESOURCE_NOT_AVAILABLE; }