Commit b9cf648439 for perl
commit b9cf64843928812232c797a69c0ee62c09a3d678
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date: Thu Nov 12 12:42:20 2026 +0000
S_flatten_inner_branches: handle empty branches rather than skipping
Empty inner branches were previously skipped over, which blocked
flattening. They are now not skipped over, allowing them to form
part of a series of flattened branches.
diff --git a/regcomp.c b/regcomp.c
index 0868381a7e..287b436066 100644
--- a/regcomp.c
+++ b/regcomp.c
@@ -3108,29 +3108,19 @@ S_flatten_inner_branches(pTHX_ RExC_state_t *pRExC_state, regnode *first_outer_b
/* Proceed if there is a nested branch with in-range offsets */
if (OP(first_inner_br) == BRANCH && ender - outer_br <= U16_MAX) {
- /* There is a set of inner BRANCH regnodes. Walk them to find the
- * final one in the chain, checking for conditions (such as empty
- * branch contents) that could corrupt the regex if flattened. */
+ /* A set of inner BRANCH regnodes exists. We need the end BRANCH. */
last_inner_br = first_inner_br;
/* Note: This loop could check if flattening has a good chance of
* building bigger tries, but with the anticipated move to 32 bit
* offsets, it shouldn't matter. */
- while (1) {
- regnode *inner_content = S_content_head(aTHX_ last_inner_br);
- if (REGNODE_TYPE(OP(inner_content)) == NOTHING) {
- last_inner_br = NULL;
- break;
- }
- if (OP(regnext(last_inner_br)) != BRANCH)
- break;
+ while (OP(regnext(last_inner_br)) == BRANCH)
last_inner_br = regnext(last_inner_br);
- }
/* It may not be safe to flatten if the regnode following the final
* inner BRANCH isn't a TAIL. Flattening is definitely not safe if
* the tail doesn't point to the expected outer TAIL. */
- if(last_inner_br && ( UNLIKELY(OP(regnext(last_inner_br)) != TAIL)
- || regnext(regnext(last_inner_br)) != ender))
+ if(UNLIKELY(OP(regnext(last_inner_br)) != TAIL)
+ || regnext(regnext(last_inner_br)) != ender)
last_inner_br = NULL;
}
if (!last_inner_br) {
@@ -3158,7 +3148,9 @@ S_flatten_inner_branches(pTHX_ RExC_state_t *pRExC_state, regnode *first_outer_b
#endif
/* Walk the inner BRANCH regnodes in turn. */
for (regnode *inner_br = first_inner_br; OP(inner_br) == BRANCH; inner_br = regnext(inner_br)) {
- regnode *content_tail = S_content_head(aTHX_ inner_br);
+ /* In the unlikely event of a branch being completely empty,
+ * we can't skip over NOTHING/OPTMIZED regnodes. */
+ regnode *content_tail = REGNODE_AFTER(inner_br);
#ifdef DEBUGGING
regnode *content_head = content_tail;
#endif
diff --git a/t/re/re_tests b/t/re/re_tests
index 482d6c63a8..7401e6c484 100644
--- a/t/re/re_tests
+++ b/t/re/re_tests
@@ -2217,6 +2217,8 @@ ABCF|BCD[Ee]|C[Gg] ABCDE y $& BCDE - # GH 22892 - AHO-CORASICK bug
^(?:|(?:)a+)$ aa y $& aa - NOTHING branch followed by valid nodes
^(?:|(?:(?:a+|b)|))$ aa y $& aa - NOTHING branch followed by valid nodes
^(?:|(?:(?:(a)|b)|))$ a y $1 a - NOTHING branch followed by valid nodes
+^(?:(?:a|)|b)c$ c y $& c - flattening handles a trailing NOTHING branch
+^(?:(?:|a+)|b)c$ aac y $& aac - flattening handes a leading NOTHING branch
# Prefixes extracted from jump tries must remain attached to those tries.
# Otherwise an enclosing trie pass can discard the branch structure needed