Commit 26e8df17

26e8df179100Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Merge pull request #1468 from ryanseys/rs-fix-regexp-xmode-uaf

regexp: fix read-after-free in extended-mode error message

Details

Commit
26e8df179100
Parent
55f6d49d53e5
Merge parent
4e3846c669e3

lib/regexp/re_compile.cmodifiedView file

@@ -47,11 +47,29 @@
4747 static void
4848 compile_error(re_compiler *c, const char *msg)
4949 {
50- if (c->stripped) free(c->stripped);
51- c->stripped = NULL;
50+ /* Build the message before freeing: in /x (extended) mode c->src aliases
51+ c->stripped, so reading it after the free would be a use-after-free. */
5252 char buf[1024];
5353 snprintf(buf, sizeof(buf), "%s: /%.*s/",
5454 msg, (int)(c->src_end - c->src), c->src);
55+ /* Free all half-built compiler state: a catchable RegexpError handler
56+ longjmps out and `c` never returns to re_compile, so the bytecode,
57+ char-class ranges, and named-capture names would otherwise leak. (On
58+ success these transfer to the compiled pattern instead.) */
59+ free(c->code);
60+ c->code = NULL;
61+ if (c->classes) {
62+ for (uint16_t i = 0; i < c->num_classes; i++) free(c->classes[i].ranges);
63+ free(c->classes);
64+ c->classes = NULL;
65+ }
66+ if (c->named_captures) {
67+ for (uint16_t i = 0; i < c->num_named; i++) free((void *)c->named_captures[i].name);
68+ free(c->named_captures);
69+ c->named_captures = NULL;
70+ }
71+ if (c->stripped) free(c->stripped);
72+ c->stripped = NULL;
5573 if (sp_re_error_handler) {
5674 sp_re_error_handler(buf);
5775 /* shouldn't return; fall through to exit as a safety net */