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 <evgeniy.naydanov@syntacore.com>
This commit is contained in:
Evgeniy Naydanov 2023-12-01 16:02:08 +03:00
parent f5b8862a76
commit 560c338526
1 changed files with 6 additions and 1 deletions

View File

@ -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__)