diff options
author | viktor.shepel <shepelvictor@bigmir.net> | 2017-06-20 17:03:42 +0300 |
---|---|---|
committer | Will Estes <westes575@gmail.com> | 2017-07-03 11:45:37 -0400 |
commit | 8a044dbe6d03877c3d8c205ae76be9c41f442237 (patch) | |
tree | 7ae388ded81844ba7ca5492e72034bda3887feed | |
parent | ef32155253a100a774c087ae4a6be3fb5c6a375c (diff) |
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.
-rw-r--r-- | src/filter.c | 16 |
1 files 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 : "<stdout>"); - 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]; |