From 5dad8a59d81496f30188364b9513cf2e5c6919f6 Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Thu, 23 Oct 2025 16:09:57 +0200 Subject: [PATCH] target/cortex_m: fix segfault on setting HW BP on not examined target Check cortex_m->fp_comparator_list and if NULL log and return error. Change-Id: Icf53f1bbc60de3486a285ef1f16bb98a5596913b Signed-off-by: Tomas Vanek Reviewed-on: https://review.openocd.org/c/openocd/+/9182 Tested-by: jenkins Reviewed-by: Antonio Borneo --- src/target/cortex_m.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/target/cortex_m.c b/src/target/cortex_m.c index 0f46ed7fc..fc12a1770 100644 --- a/src/target/cortex_m.c +++ b/src/target/cortex_m.c @@ -1900,7 +1900,6 @@ int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint int retval; unsigned int fp_num = 0; struct cortex_m_common *cortex_m = target_to_cm(target); - struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list; if (breakpoint->is_set) { LOG_TARGET_WARNING(target, "breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id); @@ -1909,6 +1908,12 @@ int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint if (breakpoint->type == BKPT_HARD) { uint32_t fpcr_value; + struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list; + if (!comparator_list) { + LOG_TARGET_ERROR(target, "No comparator list. Not examined?"); + return ERROR_FAIL; + } + while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code)) fp_num++; if (fp_num >= cortex_m->fp_num_code) { @@ -1997,7 +2002,6 @@ int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoi { int retval; struct cortex_m_common *cortex_m = target_to_cm(target); - struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list; if (!breakpoint->is_set) { LOG_TARGET_WARNING(target, "breakpoint not set"); @@ -2017,6 +2021,13 @@ int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoi LOG_TARGET_DEBUG(target, "Invalid FP Comparator number in breakpoint"); return ERROR_OK; } + + struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list; + if (!comparator_list) { + LOG_TARGET_ERROR(target, "No comparator list. Not examined?"); + return ERROR_FAIL; + } + comparator_list[fp_num].used = false; comparator_list[fp_num].fpcr_value = 0; target_write_u32(target, comparator_list[fp_num].fpcr_address,