From 1b36bbfca28c2a4ef5c451fbd5dd1930f8cc2708 Mon Sep 17 00:00:00 2001 From: ALTracer <11005378+ALTracer@users.noreply.github.com> Date: Sun, 14 Jul 2024 22:33:44 +0300 Subject: [PATCH 1/2] cortexm, cortexar: Report malloc failures in target_description --- src/target/cortexar.c | 7 ++++--- src/target/cortexm.c | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/target/cortexar.c b/src/target/cortexar.c index 3982e983454..0ab91a46c17 100644 --- a/src/target/cortexar.c +++ b/src/target/cortexar.c @@ -1864,8 +1864,9 @@ static const char *cortexar_target_description(target_s *const target) const size_t description_length = cortexar_build_target_description(NULL, 0, target->target_options & TOPT_FLAVOUR_FLOAT) + 1U; char *const description = malloc(description_length); - if (description) - (void)cortexar_build_target_description( - description, description_length, target->target_options & TOPT_FLAVOUR_FLOAT); + if (!description) /* malloc failed: heap exhaustion */ + DEBUG_ERROR("malloc: failed in %s\n", __func__); + else + cortexar_build_target_description(description, description_length, target->target_options & TOPT_FLAVOUR_FLOAT); return description; } diff --git a/src/target/cortexm.c b/src/target/cortexm.c index 77ca32c7d4c..8460113bc0c 100644 --- a/src/target/cortexm.c +++ b/src/target/cortexm.c @@ -1521,7 +1521,9 @@ static const char *cortexm_target_description(target_s *const target) { const size_t description_length = cortexm_build_target_description(NULL, 0, target->target_options) + 1U; char *const description = malloc(description_length); - if (description) + if (!description) /* malloc failed: heap exhaustion */ + DEBUG_ERROR("malloc: failed in %s\n", __func__); + else cortexm_build_target_description(description, description_length, target->target_options); return description; } From 62b08446f77e5671566906d4e2d8514b606ee081 Mon Sep 17 00:00:00 2001 From: ALTracer <11005378+ALTracer@users.noreply.github.com> Date: Sat, 31 Aug 2024 11:24:54 +0300 Subject: [PATCH 2/2] riscv_debug: Report malloc failure in target_description --- src/target/riscv_debug.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/target/riscv_debug.c b/src/target/riscv_debug.c index 14d28b35926..2a03989fc84 100644 --- a/src/target/riscv_debug.c +++ b/src/target/riscv_debug.c @@ -1287,7 +1287,9 @@ static const char *riscv_target_description(target_s *const target) const size_t description_length = riscv_build_target_description(NULL, 0, hart->address_width, hart->extensions) + 1U; char *const description = malloc(description_length); - if (description) - (void)riscv_build_target_description(description, description_length, hart->address_width, hart->extensions); + if (!description) /* malloc failed: heap exhaustion */ + DEBUG_ERROR("malloc: failed in %s\n", __func__); + else + riscv_build_target_description(description, description_length, hart->address_width, hart->extensions); return description; }