From 2c7e34bb958ac5d083b2bab4c89907cd0669196d Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Sat, 1 Apr 2017 01:18:12 -0500 Subject: filter: Output correct #line value for current file. A #line pre-processor directive specifies the line number and source file of the following lines. If the source file _is_ the current file, the line number should be that of the line following the directive. So the specified line number should be the current line number plus 1. --- src/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/filter.c') diff --git a/src/filter.c b/src/filter.c index 71f3635..ed3bfe3 100644 --- a/src/filter.c +++ b/src/filter.c @@ -391,7 +391,7 @@ int filter_fix_linedirs (struct filter *chain) /* Adjust the line directives. */ in_gen = true; snprintf (buf, readsz, "#line %d \"%s\"\n", - lineno, filename); + lineno + 1, filename); } else { /* it's a #line directive for code we didn't write */ -- cgit v1.2.3 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(-) (limited to 'src/filter.c') 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 From 0f370436e1cf0c731e74bcee806df6c7a43a3094 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Sat, 8 Jul 2017 18:54:30 +0800 Subject: filter: new internal function is_blank_line() It's simply to return (regexec(®ex_blank_line, str, 0, NULL, 0) == 0); The reason for encapsulation is to allow replacing this with a non-regex method if necessary. --- src/filter.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/filter.c') diff --git a/src/filter.c b/src/filter.c index 35b80a9..fd1bb40 100644 --- a/src/filter.c +++ b/src/filter.c @@ -324,6 +324,11 @@ int filter_tee_header (struct filter *chain) return 0; } +static bool is_blank_line (const char *str) +{ + return (regexec (®ex_blank_line, str, 0, NULL, 0) == 0); +} + /** Adjust the line numbers in the #line directives of the generated scanner. * After the m4 expansion, the line numbers are incorrect since the m4 macros * can add or remove lines. This only adjusts line numbers for generated code, @@ -395,9 +400,7 @@ int filter_fix_linedirs (struct filter *chain) } /* squeeze blank lines from generated code */ - else if (in_gen - && regexec (®ex_blank_line, buf, 0, NULL, - 0) == 0) { + else if (in_gen && is_blank_line(buf)) { if (last_was_blank) continue; else -- cgit v1.2.3 From e784a805ef8f33ab4de544eee1876f41d2bd9dbc Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Sat, 8 Jul 2017 20:21:37 +0800 Subject: filter: faster is_blank_line implementation Using regex_t regex_blank_line is *slow*. --- src/filter.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/filter.c') diff --git a/src/filter.c b/src/filter.c index fd1bb40..ccdcdd6 100644 --- a/src/filter.c +++ b/src/filter.c @@ -326,7 +326,9 @@ int filter_tee_header (struct filter *chain) static bool is_blank_line (const char *str) { - return (regexec (®ex_blank_line, str, 0, NULL, 0) == 0); + while (isspace(*str)) + str++; + return (*str == '\0'); } /** Adjust the line numbers in the #line directives of the generated scanner. -- cgit v1.2.3 From 08e1b25b0ec17d4312f838efc6f910b64900b009 Mon Sep 17 00:00:00 2001 From: "Michael W. Bombardieri" Date: Wed, 29 Nov 2017 08:11:22 -0500 Subject: scanner: use calloc to allocate new filter memory --- src/filter.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/filter.c') diff --git a/src/filter.c b/src/filter.c index ccdcdd6..a7e69ec 100644 --- a/src/filter.c +++ b/src/filter.c @@ -47,10 +47,9 @@ struct filter *filter_create_ext (struct filter *chain, const char *cmd, va_list ap; /* allocate and initialize new filter */ - f = malloc(sizeof(struct filter)); + f = calloc(sizeof(struct filter), 1); if (!f) - flexerror(_("malloc failed (f) in filter_create_ext")); - memset (f, 0, sizeof (*f)); + flexerror(_("calloc failed (f) in filter_create_ext")); f->filter_func = NULL; f->extra = NULL; f->next = NULL; @@ -100,10 +99,9 @@ struct filter *filter_create_int (struct filter *chain, struct filter *f; /* allocate and initialize new filter */ - f = malloc(sizeof(struct filter)); + f = calloc(sizeof(struct filter), 1); if (!f) - flexerror(_("malloc failed in filter_create_int")); - memset (f, 0, sizeof (*f)); + flexerror(_("calloc failed in filter_create_int")); f->next = NULL; f->argc = 0; f->argv = NULL; -- cgit v1.2.3