Skip to content

Commit db34f6f

Browse files
grantramsayborneoa
authored andcommitted
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 <grant.ramsay@hotmail.com> Reviewed-on: https://review.openocd.org/c/openocd/+/8415 Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com> Tested-by: jenkins Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
1 parent 92b482a commit db34f6f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/target/arm_dap.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ COMMAND_HANDLER(handle_dap_create)
412412
return retval;
413413
}
414414

415+
static struct adiv5_dap *target_to_dap(const struct target *target)
416+
{
417+
struct adiv5_private_config *pc = target->private_config;
418+
419+
if (!target->has_dap || !target->dap_configured || !pc)
420+
return NULL;
421+
422+
return pc->dap;
423+
}
424+
415425
COMMAND_HANDLER(handle_dap_names)
416426
{
417427
if (CMD_ARGC != 0)
@@ -432,12 +442,11 @@ COMMAND_HANDLER(handle_dap_init)
432442
COMMAND_HANDLER(handle_dap_info_command)
433443
{
434444
struct target *target = get_current_target(CMD_CTX);
435-
struct arm *arm = target_to_arm(target);
436-
struct adiv5_dap *dap = arm->dap;
445+
struct adiv5_dap *dap = target_to_dap(target);
437446
uint64_t apsel;
438447

439448
if (!dap) {
440-
LOG_ERROR("DAP instance not available. Probably a HLA target...");
449+
command_print(CMD, "target %s has no DAP", target_name(target));
441450
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
442451
}
443452

0 commit comments

Comments
 (0)