aboutsummaryrefslogtreecommitdiff
path: root/src/backend/regex/regc_color.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-11-15 18:23:38 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2024-11-15 18:23:38 -0500
commitb69bdcee9c9cfd8550c4e847d035f441fcee7d01 (patch)
tree06b224d38fe0a6faed11dbe2f39b1e39a8aab1a2 /src/backend/regex/regc_color.c
parent9a70f67667da32f8009364e1096e80ce6996c13c (diff)
downloadpostgresql-b69bdcee9c9cfd8550c4e847d035f441fcee7d01.tar.gz
postgresql-b69bdcee9c9cfd8550c4e847d035f441fcee7d01.zip
Avoid assertion due to disconnected NFA sub-graphs in regex parsing.
In commit 08c0d6ad6 which introduced "rainbow" arcs in regex NFAs, I didn't think terribly hard about what to do when creating the color complement of a rainbow arc. Clearly, the complement cannot match any characters, and I took the easy way out by just not building any arcs at all in the complement arc set. That mostly works, but Nikolay Shaplov found a case where it doesn't: if we decide to delete that sub-NFA later because it's inside a "{0}" quantifier, delsub() suffered an assertion failure. That's because delsub() relies on the target sub-NFA being fully connected. That was always true before, and the best fix seems to be to restore that property. Hence, invent a new arc type CANTMATCH that can be generated in place of an empty color complement, and drop it again later when we start NFA optimization. (At that point we don't need to do delsub() any more, and besides there are other cases where NFA optimization can lead to disconnected subgraphs.) It appears that this bug has no consequences in a non-assert-enabled build: there will be some transiently leaked NFA states/arcs, but they'll get cleaned up eventually. Still, we don't like assertion failures, so back-patch to v14 where rainbow arcs were introduced. Per bug #18708 from Nikolay Shaplov. Discussion: https://postgr.es/m/18708-f94f2599c9d2c005@postgresql.org
Diffstat (limited to 'src/backend/regex/regc_color.c')
-rw-r--r--src/backend/regex/regc_color.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/backend/regex/regc_color.c b/src/backend/regex/regc_color.c
index 30bda0e5ad0..8ae788f5195 100644
--- a/src/backend/regex/regc_color.c
+++ b/src/backend/regex/regc_color.c
@@ -1075,9 +1075,19 @@ colorcomplement(struct nfa *nfa,
assert(of != from);
- /* A RAINBOW arc matches all colors, making the complement empty */
+ /*
+ * A RAINBOW arc matches all colors, making the complement empty. But we
+ * can't just return without making any arcs, because that would leave the
+ * NFA disconnected which would break any future delsub(). Instead, make
+ * a CANTMATCH arc. Also set the HASCANTMATCH flag so we know we need to
+ * clean that up at the start of NFA optimization.
+ */
if (findarc(of, PLAIN, RAINBOW) != NULL)
+ {
+ newarc(nfa, CANTMATCH, 0, from, to);
+ nfa->flags |= HASCANTMATCH;
return;
+ }
/* Otherwise, transiently mark the colors that appear in of's out-arcs */
for (a = of->outs; a != NULL; a = a->outchain)
@@ -1089,6 +1099,12 @@ colorcomplement(struct nfa *nfa,
assert(!UNUSEDCOLOR(cd));
cd->flags |= COLMARK;
}
+
+ /*
+ * There's no syntax for re-complementing a color set, so we cannot
+ * see CANTMATCH arcs here.
+ */
+ assert(a->type != CANTMATCH);
}
/* Scan colors, clear transient marks, add arcs for unmarked colors */