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/+/6789

-- gerrit

commit 0bd05704e1331f635418f56b8c425830e3e1610c
Author: Antonio Borneo <borneo.anto...@gmail.com>
Date:   Fri Dec 24 15:15:18 2021 +0100

    log: fix memory leak when log to file is enabled
    
    When log to file is enabled, the file is not closed by OpenOCD at
    exit. This is reported by Valgrind as a memory leak that is still
    reachable, as the internal buffers of 'FILE *log_output' are freed
    by the automatic fclose() at exit.
    
    Close the log file before exit.
    
    Change-Id: Id472c0d04462035254a9b49ecb0a4037263c6f6f
    Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com>

diff --git a/src/helper/log.c b/src/helper/log.c
index caa0a66bf..686560742 100644
--- a/src/helper/log.c
+++ b/src/helper/log.c
@@ -302,6 +302,15 @@ void log_init(void)
        start = last_time = timeval_ms();
 }
 
+void log_exit(void)
+{
+       if (log_output && log_output != stderr) {
+               /* Close log file, if it was open and wasn't stderr. */
+               fclose(log_output);
+       }
+       log_output = NULL;
+}
+
 int set_log_output(struct command_context *cmd_ctx, FILE *output)
 {
        log_output = output;
diff --git a/src/helper/log.h b/src/helper/log.h
index 621d467b4..f0378ae79 100644
--- a/src/helper/log.h
+++ b/src/helper/log.h
@@ -72,6 +72,7 @@ __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6)));
  * Initialize logging module.  Call during program startup.
  */
 void log_init(void);
+void log_exit(void);
 int set_log_output(struct command_context *cmd_ctx, FILE *output);
 
 int log_register_commands(struct command_context *cmd_ctx);
diff --git a/src/openocd.c b/src/openocd.c
index 0292ba445..3c96d3214 100644
--- a/src/openocd.c
+++ b/src/openocd.c
@@ -437,6 +437,8 @@ int openocd_main(int argc, char *argv[])
        rtt_exit();
        free_config();
 
+       log_exit();
+
        if (ret == ERROR_FAIL)
                return EXIT_FAILURE;
        else if (ret != ERROR_OK)

-- 

Reply via email to