fix regexp parsing

This commit is contained in:
2026-02-04 14:19:39 -06:00
parent 2fc7d333ad
commit 611fba2b6f

View File

@@ -9509,21 +9509,28 @@ static inline BOOL token_is_pseudo_keyword (JSParseState *s, JSValue key) {
&& !s->token.u.ident.has_escape;
}
/* Write a unicode codepoint as UTF-8 to a DynBuf */
static int dbuf_put_utf8 (DynBuf *buf, uint32_t c) {
uint8_t tmp[UTF8_CHAR_LEN_MAX];
int len = unicode_to_utf8 (tmp, c);
return dbuf_put (buf, tmp, len);
}
static __exception int js_parse_regexp (JSParseState *s) {
const uint8_t *p;
BOOL in_class;
JSText *b;
JSText *b2;
DynBuf body_buf, flags_buf;
uint32_t c;
JSValue body_str, flags_str;
p = s->buf_ptr;
p++;
in_class = FALSE;
b = pretext_init (s->ctx, 32);
if (!b) return -1;
b2 = pretext_init (s->ctx, 1);
if (!b2) goto fail;
/* Use DynBuf with standard malloc to avoid GC issues during parsing */
dbuf_init (&body_buf);
dbuf_init (&flags_buf);
for (;;) {
if (p >= s->buf_end) {
eof_error:
@@ -9541,8 +9548,7 @@ static __exception int js_parse_regexp (JSParseState *s) {
/* XXX: incorrect as the first character in a class */
in_class = FALSE;
} else if (c == '\\') {
b = pretext_putc (s->ctx, b, c);
if (!b) goto fail;
if (dbuf_putc (&body_buf, c)) goto fail;
c = *p++;
if (c == '\n' || c == '\r')
goto eol_error;
@@ -9571,8 +9577,7 @@ static __exception int js_parse_regexp (JSParseState *s) {
}
p = p_next;
}
b = pretext_putc (s->ctx, b, c);
if (!b) goto fail;
if (dbuf_put_utf8 (&body_buf, c)) goto fail;
}
/* flags */
@@ -9587,13 +9592,16 @@ static __exception int js_parse_regexp (JSParseState *s) {
}
}
if (!lre_js_is_ident_next (c)) break;
b2 = pretext_putc (s->ctx, b2, c);
if (!b2) goto fail;
if (dbuf_put_utf8 (&flags_buf, c)) goto fail;
p = p_next;
}
body_str = pretext_end (s->ctx, b);
flags_str = pretext_end (s->ctx, b2);
/* Convert DynBuf contents to JS strings - this is the only GC point */
body_str = JS_NewStringLen (s->ctx, (const char *)body_buf.buf, body_buf.size);
dbuf_free (&body_buf);
flags_str = JS_NewStringLen (s->ctx, (const char *)flags_buf.buf, flags_buf.size);
dbuf_free (&flags_buf);
if (JS_IsException (body_str) || JS_IsException (flags_str)) {
return -1;
}
@@ -9603,6 +9611,8 @@ static __exception int js_parse_regexp (JSParseState *s) {
s->buf_ptr = p;
return 0;
fail:
dbuf_free (&body_buf);
dbuf_free (&flags_buf);
return -1;
}