This is an automated email from Gerrit. "Antonio Borneo <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9475
-- gerrit commit 84cc8760ceb1e20e5f6513689df78b790098b0e7 Author: Antonio Borneo <[email protected]> Date: Sat Feb 21 19:28:01 2026 +0100 log: add specific log for out of memory errors OpenOCD code always checks for memory allocation failure, either for good coding practice and for keeping static code analyzers happy. Using the standard logging functions to log such allocation failures has no guarantee to work, as logging functions involve further memory allocations. Create a minimalist logging code to be used only for all memory allocation failures through the macro LOG_ERROR_OUT_OF_MEMORY(). E.g.: m = malloc(1000); if (!m) { LOG_ERROR_OUT_OF_MEMORY(); return ERROR_FAIL; } It doesn't check the log level, and has a simple output "Error: Out of memory at file:line\n" If the developer wants to add further information, like context or function name, this should go in a following LOG_ERROR() that is not guaranteed to be printed out. The macro is organized to minimize the footprint when used several times in the same file, helping the compiler to keep only one instance of the string "file:" per file. Each call only creates an instance of the string "line\n". Change-Id: I50dbb5293a0b788a9cd94304c8c5ca38dc710427 Signed-off-by: Antonio Borneo <[email protected]> diff --git a/src/helper/log.c b/src/helper/log.c index 06d3810557..1dd45a61b0 100644 --- a/src/helper/log.c +++ b/src/helper/log.c @@ -49,6 +49,19 @@ static const char * const log_strings[7] = { static unsigned int count; +/** + * Simplified log without any further memory allocation + * @param s1 filename concatenated with ":" + * @param s2 line number as string concatenated with "\n" + */ +void log_error_out_of_memory(const char *s1, const char *s2) +{ + fputs("Error: Out of memory at ", log_output); + fputs(s1, log_output); + fputs(s2, log_output); + fflush(log_output); +} + /* forward the log to the listeners */ static void log_forward(const char *file, unsigned int line, const char *function, const char *string) { diff --git a/src/helper/log.h b/src/helper/log.h index 01917abe23..32603aa7d7 100644 --- a/src/helper/log.h +++ b/src/helper/log.h @@ -16,6 +16,12 @@ #include <helper/command.h> #include <helper/compiler.h> +#include <helper/types.h> + +// __FILE_NAME__ is a GNU extension +#ifndef __FILE_NAME__ +#define __FILE_NAME__ __FILE__ +#endif /* To achieve C99 printf compatibility in MinGW, gnu_printf should be * used for __attribute__((format( ... ))), with GCC v4.4 or later @@ -104,8 +110,18 @@ char *alloc_printf(const char *fmt, ...) const char *find_nonprint_char(const char *buf, unsigned int buf_len); +void log_error_out_of_memory(const char *s1, const char *s2); + extern int debug_level; +/* + * General logging functions involve memory allocations. + * We cannot guarantee they will work after a memory allocation failure. + * Write it as a special case that minimizes allocations. + */ +#define LOG_ERROR_OUT_OF_MEMORY() \ + log_error_out_of_memory(__FILE_NAME__ ":", stringify(__LINE__) "\n") + /* Avoid fn call and building parameter list if we're not outputting the information. * Matters on feeble CPUs for DEBUG/INFO statements that are involved frequently */ --
