Skip to content

Fix dropped zero-width assertions in alternations - #134

Open
le0pard wants to merge 1 commit into
laurikari:masterfrom
le0pard:patch-match
Open

Fix dropped zero-width assertions in alternations#134
le0pard wants to merge 1 commit into
laurikari:masterfrom
le0pard:patch-match

Conversation

@le0pard

@le0pard le0pard commented May 3, 2026

Copy link
Copy Markdown

Fix for ^|$ failing on empty strings and (^|\b)a failing while (\b|^)a succeeds #101 #78

It is because when AST compiler hits a UNION (an alternation like |) where both sides are zero-width assertions, it greedily traverses the left side and completely ignores the right side

Here I applied a mathematical AST rewrite during compilation (without rewriting the entire formal language compiler). If we distribute the catenation over the union—rewriting (A|B)C into AC|BC - the compiler never has to choose between A and B simultaneously.

But such fix added another problem - AST explosion ($O(2^N)$ exponential growth), so I added a safeguard that caps the number of times the AST is allowed to duplicate itself. If an attacker submits a malicious regex that tries to duplicate 20 times, the compiler should catch it and safely bail out. I hope no practical case to chain 11 zero-width assertions like this for regex:

(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)(^|\b)word

Maybe better fix will be change int assertions; into an array of bitmasks, completely rewrite the TNFA generator to populate this array, and evaluate arrays of assertions instead of doing a single fast bitwise check, but it will be too big work for me.

P.S. C language is not my strong side, I am Ruby/JS/Golang dev, so sorry, if it contain stupid newbie stuff for c, like formatting or incorrect usage of C best practices (added "allow edits by maintainers")

Comment thread win32/config.h
@le0pard
le0pard force-pushed the patch-match branch 2 times, most recently from 6727e8d to d5c2926 Compare May 3, 2026 13:49
Comment thread win32/config.h
Comment thread tests/test-branch-drop.c Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add these test cases to retest.c instead.

Comment thread lib/tre-compile.c Outdated
tre_catenation_t *cat = node->obj;

/* Limit distribution to prevent $O(2^N)$ AST explosion DoS */
#define MAX_DISTRIBUTION_DEPTH 10 /* Allows 1024 branches */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should move to either file scope or tre-compile.h

Comment thread lib/tre-compile.c Outdated
Comment on lines +891 to +944
if (cat->left->type == UNION && tre_is_empty_node(cat->left))
{

if (*dist_depth >= MAX_DISTRIBUTION_DEPTH) {
status = REG_ESPACE; /* Safely abort compilation */
break;
}
(*dist_depth)++;

tre_union_t *uni = cat->left->obj;
tre_ast_node_t *cat1, *cat2, *right_copy;
status = tre_copy_ast(mem, stack, cat->right, 0, &pos_add, tag_directions, &right_copy, &max_pos);
if (status != REG_OK) break;

cat1 = tre_ast_new_catenation(mem, uni->left, cat->right);
cat2 = tre_ast_new_catenation(mem, uni->right, right_copy);
if (!cat1 || !cat2) { status = REG_ESPACE; break; }

tre_union_t *new_uni = tre_mem_alloc(mem, sizeof(*new_uni));
if (!new_uni) { status = REG_ESPACE; break; }
new_uni->left = cat1; new_uni->right = cat2;

node->type = UNION; node->obj = new_uni;
STACK_PUSHX(stack, voidptr, node);
STACK_PUSHX(stack, int, EXPAND_RECURSE);
break;
}

if (cat->right->type == UNION && tre_is_empty_node(cat->right))
{
if (*dist_depth >= MAX_DISTRIBUTION_DEPTH) {
status = REG_ESPACE; /* Safely abort compilation */
break;
}
(*dist_depth)++;

tre_union_t *uni = cat->right->obj;
tre_ast_node_t *cat1, *cat2, *left_copy;
status = tre_copy_ast(mem, stack, cat->left, 0, &pos_add, tag_directions, &left_copy, &max_pos);
if (status != REG_OK) break;

cat1 = tre_ast_new_catenation(mem, cat->left, uni->left);
cat2 = tre_ast_new_catenation(mem, left_copy, uni->right);
if (!cat1 || !cat2) { status = REG_ESPACE; break; }

tre_union_t *new_uni = tre_mem_alloc(mem, sizeof(*new_uni));
if (!new_uni) { status = REG_ESPACE; break; }
new_uni->left = cat1; new_uni->right = cat2;

node->type = UNION; node->obj = new_uni;
STACK_PUSHX(stack, voidptr, node);
STACK_PUSHX(stack, int, EXPAND_RECURSE);
break;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reformat to conform to the established style, this is all over the place.

@le0pard
le0pard force-pushed the patch-match branch 7 times, most recently from 8fd6147 to d0afb6a Compare May 7, 2026 13:07
Comment thread lib/tre-compile.c

/* Map all dummy nodes representing final positions to the exact same offset
* state */
if (tree->lastpos && tree->lastpos[0].position >= 0)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code takes all those duplicated dummy nodes and artificially merges them back into a single state in the compiled NFA (offs array). Because with this fix dummy node gets duplicated across all the empty paths

@le0pard
le0pard requested a review from dag-erling May 7, 2026 13:16
@le0pard

le0pard commented May 7, 2026

Copy link
Copy Markdown
Author

@dag-erling please check again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants