From 560c338526b88d4ba5d472c628a3c990551fcb91 Mon Sep 17 00:00:00 2001 From: Evgeniy Naydanov Date: Fri, 1 Dec 2023 16:02:08 +0300 Subject: [PATCH] target/riscv: avoid using VLA in `log_debug_reg()` OpenOCD style guide(`doc/manual/style.txt`) prohibits use of VLA: > - use malloc() to create dynamic arrays. Do @b not use @c alloca > or variable length arrays on the stack. non-MMU hosts(uClinux) and > pthreads require modest and predictable stack usage. Change-Id: I12e4a5087fd056d69866137237af6deca27f5d33 Signed-off-by: Evgeniy Naydanov --- src/target/riscv/riscv-013.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 58ebad78d..d3da9fcff 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -302,9 +302,14 @@ static void log_debug_reg(struct target *target, enum riscv_debug_reg_ordinal re if (debug_level < LOG_LVL_DEBUG) return; const riscv_debug_reg_ctx_t context = get_riscv_debug_reg_ctx(target); - char buf[riscv_debug_reg_to_s(NULL, reg, context, value) + 1]; + char * const buf = malloc(riscv_debug_reg_to_s(NULL, reg, context, value) + 1); + if (!buf) { + LOG_ERROR("Unable to allocate memory."); + return; + } riscv_debug_reg_to_s(buf, reg, context, value); log_printf_lf(LOG_LVL_DEBUG, file, line, func, "[%s] %s", target_name(target), buf); + free(buf); } #define LOG_DEBUG_REG(t, r, v) log_debug_reg(t, r##_ORDINAL, v, __FILE__, __LINE__, __func__)