From 8a044dbe6d03877c3d8c205ae76be9c41f442237 Mon Sep 17 00:00:00 2001 From: "viktor.shepel" Date: Tue, 20 Jun 2017 17:03:42 +0300 Subject: filter: memory leak free scanner postprocessing. **Issue:** Scanner postprocessing leaks memory during correction of `#line` directives values and generation of C header file. **Root cause:** `filter_fix_linedirs` and `filter_tee_header` functions do not dispose allocated memory. **Solution:** Automatically reclaim affected memory by allocating it on stack insted of heap. Stack allocation should not be a problem as its only 512 bytes and there is no recursive calls. --- src/filter.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/filter.c b/src/filter.c index ed3bfe3..35b80a9 100644 --- a/src/filter.c +++ b/src/filter.c @@ -230,8 +230,7 @@ int filter_tee_header (struct filter *chain) * header file at the same time. */ - const int readsz = 512; - char *buf; + char buf[512]; int to_cfd = -1; FILE *to_c = NULL, *to_h = NULL; bool write_header; @@ -283,10 +282,7 @@ int filter_tee_header (struct filter *chain) fprintf (to_c, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n", outfilename ? outfilename : ""); - buf = malloc((size_t) readsz); - if (!buf) - flexerror(_("malloc failed in filter_tee_header")); - while (fgets (buf, readsz, stdin)) { + while (fgets (buf, sizeof buf, stdin)) { fputs (buf, to_c); if (write_header) fputs (buf, to_h); @@ -336,8 +332,8 @@ int filter_tee_header (struct filter *chain) */ int filter_fix_linedirs (struct filter *chain) { - char *buf; - const size_t readsz = 512; + char buf[512]; + const size_t readsz = sizeof buf; int lineno = 1; bool in_gen = true; /* in generated code */ bool last_was_blank = false; @@ -345,10 +341,6 @@ int filter_fix_linedirs (struct filter *chain) if (!chain) return 0; - buf = malloc(readsz); - if (!buf) - flexerror(_("malloc failed in filter_fix_linedirs")); - while (fgets (buf, (int) readsz, stdin)) { regmatch_t m[10]; -- cgit v1.2.3