灾难性回溯 [A-Z]*([0-9A-Z])-[1-9]*([0-9])

Catastrophic backtracking [A-Z]*([0-9A-Z])-[1-9]*([0-9])

我有一个脚本 运行 有时会导致服务器运行 100% cpu。我怀疑这是因为一些正则表达式。 是否有任何示例输入可能导致此正则表达式的灾难性回溯:[A-Z]([0-9A-Z])-[1-9]([0-9])

它用于将 ABC-123 等 jira 票据模式匹配到提交消息中

readonly ticket_regexp='[A-Z]*([0-9A-Z])-[1-9]*([0-9])'
readonly commit_msg="""
* ABC-123 Added some content to the file
* DEF-456 Added some content to the file
"""

if [[ ! "${commit_msg}" =~ ${ticket_regexp} ]]; then
    echo "Does not contain the required Jira ticket reference" >&2
    exit 1
fi

找到可能导致正则表达式回溯的变量 commit_msg 的值,并将 cpu 设为 100%

据我所知,正则表达式不是问题 - 它可能在您的代码中的其他地方。

Catastrophic backtracking 通常与背靠背或嵌套的所有格量词一起出现,而您的正则表达式中没有这些量词的任何示例。 regex101.com 上的一些模糊测试还表明,无效的正则表达式没有任何变化,这使得确定失败的步骤呈指数增长。

话虽这么说,如果您仍然担心回溯并且不相信您的正则表达式,您知道 Atlassian 自己已经发布了 official regexes to match JIRA ids 吗?