Skip to content

re hangs on forever for this pattern #123809

@dg-pb

Description

@dg-pb

Bug report

Bug description:

Text:

with open('./cpython/Tools/ssl/multissltests.py') as f:
    text = f.read()

By doing multiline search on CPython codebase (which works fine):

REG1 = r'''
    (\s*)try\:\n
    (
        (\1)[^\n]*\n
    )*
    (\1)
    else\:\s*if
'''
CMP1 = re.compile(REG1, re.VERBOSE)
CMP1.search(text)

I did one extra addition:

REG2 = r'''
    (\s*)try\:\n
    (
        ((\1)[^\n]*)?\n
    )*
    (\1)
    else\:\s*if
'''
CMP2 = re.compile(REG2, re.VERBOSE)
CMP2.search(text)    # Hangs indefinitely

I minimised regex so that it still hangs. Although it does not make sense anymore, but it seems to be a minimal example propagating this behaviour:

REG3 = r'([^\n]*)*l'
CMP3 = re.compile(REG3)
CMP3.search(text)

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions