From 92f4d54776e4183a81209a9ce3a28f052f9249e4 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 12 Apr 2017 23:25:51 +0300 Subject: Fix valgrind invalid read issues in new fpat parsing. --- ChangeLog | 3 +++ field.c | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index fd759803..882f7f4f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,9 @@ * field.c (fpat_parse_field): Restructure the code to reduce complexity and document the new structure. + * field.c (fpat_parse_field): Further restructuring to avoid + invalid reads as reported by valgrind. + 2017-04-10 Andrew J. Schorr * awk.h (enum opcodeval): For the avoidance of doubt, specify that diff --git a/field.c b/field.c index 8145141c..608be7da 100644 --- a/field.c +++ b/field.c @@ -1581,7 +1581,7 @@ fpat_parse_field(long up_to, /* parse only up to this field number */ int regex_flags = RE_NEED_START; mbstate_t mbs; char* field_start; - bool field_found; + bool field_found = false; memset(&mbs, 0, sizeof(mbstate_t)); @@ -1594,7 +1594,7 @@ fpat_parse_field(long up_to, /* parse only up to this field number */ if (rp == NULL) /* use FPAT */ rp = FPAT_regexp; - while (scan <= end && nf < up_to) { /* still something to parse */ + while (scan < end && nf < up_to) { /* still something to parse */ /* first attempt to match the next field */ start = scan; @@ -1632,10 +1632,17 @@ fpat_parse_field(long up_to, /* parse only up to this field number */ */ if (sep_arr != NULL) set_element(nf, start, (long) (end - start), sep_arr); - scan = end + 1; + scan = end; } } + /* + * If the last field extends up to the end of the record, generate + * a null trailing separator + */ + if (sep_arr != NULL && scan == end && field_found) + set_element(nf, scan, 0L, sep_arr); + *buf = scan; return nf; } -- cgit v1.2.3 From 9fff07da8c25183f53934c0155d1fa49bc97198e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 12 Apr 2017 23:28:55 +0300 Subject: Fix a memory leak in mpfr formatting values. --- ChangeLog | 5 +++++ mpfr.c | 1 + 2 files changed, 6 insertions(+) diff --git a/ChangeLog b/ChangeLog index 882f7f4f..6e053f7e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2017-04-12 Arnold D. Robbins + + * mpfr.c (mpg_format_val): Set STRCUR flag when we're done. + Fixes a memory leak. Thanks to valgrind for the report. + 2017-04-12 Manuel Collado Fix the FPAT bug reported by Ed Morton in the gawk-bug mailing list. diff --git a/mpfr.c b/mpfr.c index ec8d5561..5a2a35d3 100644 --- a/mpfr.c +++ b/mpfr.c @@ -384,6 +384,7 @@ mpg_format_val(const char *format, int index, NODE *s) if ((s->flags & (MALLOC|STRCUR)) == (MALLOC|STRCUR)) efree(s->stptr); s->stptr = r->stptr; + s->flags |= STRCUR; freenode(r); /* Do not unref(r)! We want to keep s->stptr == r->stpr. */ free_wstr(s); return s; -- cgit v1.2.3