diff --git a/builtin.c b/builtin.c index e394cc34..3a0f10dd 100644 --- a/builtin.c +++ b/builtin.c @@ -2791,10 +2791,17 @@ do_match(int nargs) size_t *wc_indices = NULL; rlength = REEND(rp, t1->stptr) - RESTART(rp, t1->stptr); /* byte length */ - if (rlength > 0 && gawk_mb_cur_max > 1) { - t1 = str2wstr(t1, & wc_indices); - rlength = wc_indices[rstart + rlength - 1] - wc_indices[rstart] + 1; - rstart = wc_indices[rstart]; + if (gawk_mb_cur_max > 1) { + if (rlength > 0) { + t1 = str2wstr(t1, & wc_indices); + rlength = wc_indices[rstart + rlength - 1] - wc_indices[rstart] + 1; + rstart = wc_indices[rstart]; + } else if (rstart == t1->stlen) { + // zero length match at end of string, use character index, not byte index + t1 = str2wstr(t1, NULL); + rstart = t1->wstlen; + } /* else + some other weird case, leave it alone, I guess. */ } rstart++; /* now it's 1-based indexing */