diff options
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | field.c | 13 |
2 files changed, 13 insertions, 3 deletions
@@ -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 <aschorr@telemetry-investments.com> * awk.h (enum opcodeval): For the avoidance of doubt, specify that @@ -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; } |