log: fix memory leak when log to file is enabled 89/6789/2
authorAntonio Borneo <borneo.antonio@gmail.com>
Fri, 24 Dec 2021 14:15:18 +0000 (15:15 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 22 Jan 2022 10:15:48 +0000 (10:15 +0000)
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.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6789
Tested-by: jenkins
src/helper/log.c
src/helper/log.h
src/openocd.c

index caa0a66bf64707b2d94d3519d489d9687fa328b9..6865607423f1e025771ab42803dc15608f8c5cf1 100644 (file)
@@ -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;
index 621d467b49e9e4697ae686562f1fceb0cc7ee648..f0378ae7946c64b17aa7152e74e5601086767372 100644 (file)
@@ -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);
index 0292ba445b28f89559e565fe276673c3b9273f74..3c96d321456a053d2e6eb35457c64e68061e9fab 100644 (file)
@@ -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)

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)