Fix dropped zero-width assertions in alternations - #134
Open
le0pard wants to merge 1 commit into
Open
Conversation
le0pard
commented
May 3, 2026
le0pard
force-pushed
the
patch-match
branch
2 times, most recently
from
May 3, 2026 13:49
6727e8d to
d5c2926
Compare
dag-erling
requested changes
May 7, 2026
Collaborator
There was a problem hiding this comment.
Please add these test cases to retest.c instead.
| tre_catenation_t *cat = node->obj; | ||
|
|
||
| /* Limit distribution to prevent $O(2^N)$ AST explosion DoS */ | ||
| #define MAX_DISTRIBUTION_DEPTH 10 /* Allows 1024 branches */ |
Collaborator
There was a problem hiding this comment.
This should move to either file scope or tre-compile.h
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; | ||
| } |
Collaborator
There was a problem hiding this comment.
Please reformat to conform to the established style, this is all over the place.
le0pard
force-pushed
the
patch-match
branch
7 times, most recently
from
May 7, 2026 13:07
8fd6147 to
d0afb6a
Compare
le0pard
commented
May 7, 2026
|
|
||
| /* Map all dummy nodes representing final positions to the exact same offset | ||
| * state */ | ||
| if (tree->lastpos && tree->lastpos[0].position >= 0) |
Author
There was a problem hiding this comment.
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
Author
|
@dag-erling please check again |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix for
^|$failing on empty strings and(^|\b)afailing while(\b|^)asucceeds #101 #78It 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 sideHere 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)CintoAC|BC- the compiler never has to choose betweenAandBsimultaneously.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:
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")