【发布时间】:2019-08-13 22:41:27
【问题描述】:
我有一个脚本正在运行,有时会导致服务器使用 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% 的值
【问题讨论】:
-
不确定它是否能解决问题,但
ticket_regexp='[A-Z][0-9A-Z]*-[1-9][0-9]*'似乎是你想写的。 -
谢谢,但我更好奇哪个输入可能触发错误
标签: regex recursive-backtracking