Skip to content

Commit

Permalink
Git issue 530: hangs with fuzzy and optionals
Browse files Browse the repository at this point in the history
It's not hanging, it'll finish eventually. It's just an example of catastrophic backtracking.

The error printed when Ctrl+C is pressed does show a bug, though, which is now fixed.
  • Loading branch information
Matthew Barnett committed May 15, 2024
1 parent be139ff commit 8eabb42
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
14 changes: 14 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
Version: 2024.5.15

Git issue 530: hangs with fuzzy and optionals

It's not hanging, it'll finish eventually. It's just an example of catastrophic backtracking.

The error printed when Ctrl+C is pressed does show a bug, though, which is now fixed.

Version: 2024.5.10

Updated for Python 3.13.

<time.h> now needs to be included explicitly because Python.h no longer includes it.

Version: 2024.4.28

Git issue 527: `VERBOSE`/`X` flag breaks `\N` escapes
Expand Down
23 changes: 16 additions & 7 deletions regex_3/_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -17459,7 +17459,7 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {

/* Has an error occurred, or is it a partial match? */
if (status < 0)
break;
goto error;

if (status == RE_ERROR_FAILURE)
break;
Expand All @@ -17479,19 +17479,19 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {
clear_best_list(&best_list);
if (!add_to_best_list(state, &best_list, state->match_pos,
state->text_pos))
goto error;
goto mem_error;

clear_best_fuzzy_changes(state, &best_changes_list);
if (!add_best_fuzzy_changes(state, &best_changes_list))
goto error;
goto mem_error;
} else if (state->total_errors == fewest_errors) {
/* This match was as good as the previous matches. Remember this
* one.
*/
add_to_best_list(state, &best_list, state->match_pos,
state->text_pos);
if (!add_best_fuzzy_changes(state, &best_changes_list))
goto error;
goto mem_error;
}

start_pos = state->match_pos;
Expand Down Expand Up @@ -17557,6 +17557,9 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {
init_match(state);
status = basic_match(state, FALSE);

if (status < 0)
goto error;

if (status == RE_ERROR_SUCCESS) {
BOOL better = FALSE;

Expand All @@ -17575,12 +17578,12 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {
save_fuzzy_counts(state, best_fuzzy_counts);
if (!save_fuzzy_changes(state,
&best_fuzzy_changes))
goto error;
goto mem_error;

best_groups = save_captures(state,
best_groups);
if (!best_groups)
goto error;
goto mem_error;

best_match_pos = state->match_pos;
best_text_pos = state->text_pos;
Expand Down Expand Up @@ -17645,6 +17648,9 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {
init_match(state);
status = basic_match(state, search);

if (status < 0)
goto error;

list = &best_changes_list.lists[0];
state->fuzzy_changes.count = list->count;
Py_MEMCPY(state->fuzzy_changes.items, list->items,
Expand All @@ -17664,10 +17670,13 @@ Py_LOCAL_INLINE(int) do_best_fuzzy_match(RE_State* state, BOOL search) {

return status;

mem_error:
status = RE_ERROR_MEMORY;

error:
fini_best_list(state, &best_list);
fini_best_changes_list(state, &best_changes_list);
return RE_ERROR_MEMORY;
return status;
}

/* Performs a match or search from the current text position for an enhanced
Expand Down
2 changes: 1 addition & 1 deletion regex_3/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
"VERSION1", "X", "VERBOSE", "W", "WORD", "error", "Regex", "__version__",
"__doc__", "RegexFlag"]

__version__ = "2.5.144"
__version__ = "2.5.145"

# --------------------------------------------------------------------
# Public interface.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='regex',
version='2024.5.10',
version='2024.5.15',
description='Alternative regular expression module, to replace re.',
long_description=long_description,
long_description_content_type='text/x-rst',
Expand Down

0 comments on commit 8eabb42

Please sign in to comment.