This is an automated email from Gerrit.

"Antonio Borneo <borneo.anto...@gmail.com>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9090

-- gerrit

commit 6330906ff8f9ca8a1cf1832636476fddfad659a9
Author: Antonio Borneo <borneo.anto...@gmail.com>
Date:   Mon Aug 18 11:49:49 2025 +0200

    helper: command: rewrite common command_print()
    
    Avoid code duplication by merging command_print() and
    command_print_sameline().
    
    Detect the allocation error and let the two function return error.
    
    Add a FIXME as the functions should always have 'cmd' properly
    set. Should this be an assert()?
    
    Change-Id: Iff704c42969a7ca9ea884520942adecd40bebbd6
    Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com>

diff --git a/src/helper/command.c b/src/helper/command.c
index a3ceed0e2e..4aaeacdcba 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -349,46 +349,63 @@ void command_output_text(struct command_context *context, 
const char *data)
                context->output_handler(context, data);
 }
 
-void command_print_sameline(struct command_invocation *cmd, const char 
*format, ...)
+static int command_vprint(struct command_invocation *cmd,
+               va_list ap, const char *format, bool add_lf)
 {
-       char *string;
+       /*
+        * FIXME: Why this check on !cmd ?
+        * Commit 7f260f5009a7 that introduces it, does not explain why!
+        * Was author not confident on the code change?
+        */
+       if (!cmd)
+               return ERROR_OK;
+
+       char *string = alloc_vprintf(format, ap);
+       if (!string) {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
+
+       // alloc_vprintf guarantees the buffer is at least one char longer
+       if (add_lf)
+               strcat(string, "\n");
+
+       char *output = cmd->output ? cmd->output : "";
+       output = alloc_printf("%s%s", output, string);
+       free(string);
+       if (!output) {
+               LOG_ERROR("Out of memory");
+               return ERROR_FAIL;
+       }
+
+       free(cmd->output);
+       cmd->output = output;
+
+       return ERROR_OK;
+}
 
+int command_print_sameline(struct command_invocation *cmd, const char *format, 
...)
+{
        va_list ap;
        va_start(ap, format);
 
-       string = alloc_vprintf(format, ap);
-       if (string && cmd) {
-               char *output = cmd->output ? cmd->output : "";
-               output = alloc_printf("%s%s", output, string);
-               if (output) {
-                       free(cmd->output);
-                       cmd->output = output;
-               }
-               free(string);
-       }
+       int retval = command_vprint(cmd, ap, format, false);
 
        va_end(ap);
+
+       return retval;
 }
 
-void command_print(struct command_invocation *cmd, const char *format, ...)
+int command_print(struct command_invocation *cmd, const char *format, ...)
 {
-       char *string;
-
        va_list ap;
        va_start(ap, format);
 
-       string = alloc_vprintf(format, ap);
-       if (string && cmd) {
-               char *output = cmd->output ? cmd->output : "";
-               output = alloc_printf("%s%s\n", output, string);
-               if (output) {
-                       free(cmd->output);
-                       cmd->output = output;
-               }
-               free(string);
-       }
+       int retval = command_vprint(cmd, ap, format, true);
 
        va_end(ap);
+
+       return retval;
 }
 
 static bool command_can_run(struct command_context *cmd_ctx, struct command 
*c, const char *full_name)
diff --git a/src/helper/command.h b/src/helper/command.h
index 8ce45473f2..cbe64c9a94 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -384,9 +384,9 @@ void command_done(struct command_context *context);
  * it explicitly with either an empty command_print() or with a '\n' in the
  * last command_print() and add a comment to document it.
  */
-void command_print(struct command_invocation *cmd, const char *format, ...)
+int command_print(struct command_invocation *cmd, const char *format, ...)
 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
-void command_print_sameline(struct command_invocation *cmd, const char 
*format, ...)
+int command_print_sameline(struct command_invocation *cmd, const char *format, 
...)
 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
 
 int command_run_line(struct command_context *context, char *line);

-- 

Reply via email to