From d4a599edead399bc3fd7261d7c58fe14f66d8a47 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 8 Jul 2021 18:13:35 +0200 Subject: [PATCH] Issue 150: Fix regexp.exec bugs. --- jsregexp.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jsregexp.c b/jsregexp.c index 818be3b..eadf6fe 100644 --- a/jsregexp.c +++ b/jsregexp.c @@ -53,25 +53,27 @@ void js_newregexp(js_State *J, const char *pattern, int flags) void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text) { + const char *haystack; int result; int i; int opts; Resub m; + haystack = text; opts = 0; if (re->flags & JS_REGEXP_G) { - if (re->last > strlen(text)) { + if (re->last > strlen(haystack)) { re->last = 0; js_pushnull(J); return; } if (re->last > 0) { - text += re->last; + haystack = text + re->last; opts |= REG_NOTBOL; } } - result = js_regexec(re->prog, text, &m, opts); + result = js_regexec(re->prog, haystack, &m, opts); if (result < 0) js_error(J, "regexec failed"); if (result == 0) { @@ -85,7 +87,7 @@ void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text) js_setindex(J, -2, i); } if (re->flags & JS_REGEXP_G) - re->last = re->last + (m.sub[0].ep - text); + re->last = m.sub[0].ep - text; return; }