From 80ee176dccf6954c8cc6493283ddadba42b8f694 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 24 Aug 2017 19:14:10 +0200 Subject: scanner: Fix for memleak due to unclosed file pointer When including a file, it is opened by fopen() and therefore needs to be closed after scanning has finished using fclose(), otherwise valgrind will report a memleak. This patch changes struct input_descriptor to track the opened FILE pointer instead of the file descriptor so the pointer is available for closing in scanner_destroy(). While at it, change erec_print() to work on the open FILE pointer so it doesn't have to call fileno() in beforehand. And as a little bonus, use C99 initializer of the buffer to get rid of the call to memset(). Note that it is necessary to call erec_print_list() prior to destroying the scanner, otherwise it will start manipulating an already freed FILE pointer (and therefore crash the program). Signed-off-by: Phil Sutter --- src/scanner.l | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/scanner.l') diff --git a/src/scanner.l b/src/scanner.l index d50e2b67..25e4eb1c 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -634,7 +634,7 @@ static struct error_record *scanner_push_file(void *scanner, const char *filenam state->indesc->location = *loc; state->indesc->type = INDESC_FILE; state->indesc->name = xstrdup(filename); - state->indesc->fd = fileno(f); + state->indesc->fp = f; init_pos(state); return NULL; } @@ -866,6 +866,7 @@ void scanner_destroy(struct parser_state *scanner) if (inpdesc && inpdesc->name) { xfree(inpdesc->name); inpdesc->name = NULL; + fclose(inpdesc->fp); } yypop_buffer_state(scanner); } while (state->indesc_idx--); -- cgit v1.2.3