From f20e7c6b7413f111f0f58a268face74e9a8fe6d8 Mon Sep 17 00:00:00 2001 From: Nick Law Date: Wed, 21 Nov 2018 00:27:12 +0000 Subject: [PATCH] Common realloc mistake: nulled but not freed --- src/logging.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/logging.c b/src/logging.c index 26c7a49..2a9eb97 100644 --- a/src/logging.c +++ b/src/logging.c @@ -42,6 +42,7 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) * */ + char **result; /* A time buffer. */ time_t t; @@ -58,7 +59,13 @@ void nwipe_log( nwipe_log_t level, const char* format, ... ) /* Increase the current log element pointer - we will write here */ if (log_current_element == log_elements_allocated) { log_elements_allocated++; - log_lines = (char **) realloc (log_lines, (log_elements_allocated) * sizeof(char *)); + result = realloc (log_lines, (log_elements_allocated) * sizeof(char *)); + if ( result == NULL ) + { + fprintf( stderr, "nwipe_log: realloc failed when adding a line.\n" ); + return; + } + log_lines = result; log_lines[log_current_element] = malloc(MAX_LOG_LINE_CHARS * sizeof(char)); }