Fix: Patterns matching the empty string rejected

As observed in #86, fingers accepts a pattern accepting an empty string, such as `(token)?`, and this leads the `gawk` subprocess to loop indefinitely, allocated memory until it's eventually killed by the kernel.
This pr rejects such patterns, printing an error message.
This commit is contained in:
Paweł Sacawa 2021-06-10 17:16:39 -04:00
parent eba6ee2d55
commit c2392d0ca3
2 changed files with 18 additions and 2 deletions

View File

@ -128,7 +128,8 @@ set -g @fingers-pattern-1 'yolo'
set -g @fingers-pattern-50 'whatever'
```
Patterns are case insensitive, and grep's extended syntax ( ERE ) should be used.
Patterns are case insensitive, and grep's extended syntax ( ERE ) should be used. Patterns
matching the empty string are disallowed.
`man grep` for more info.
If the introduced regexp contains an error, an error will be shown when

View File

@ -7,7 +7,6 @@ TMUX_PRINTER="$CONF_CURRENT_DIR/../vendor/tmux-printer/tmux-printer"
declare -A fingers_defaults
# TODO empty patterns are invalid
function check_pattern() {
echo "beep beep" | grep -e "$1" 2> /dev/null
@ -18,6 +17,14 @@ function check_pattern() {
fi
}
function check_matches_empty_string() {
if ! grep -e "$1" <<< "" >/dev/null; then
echo 0
else
echo 1
fi
}
function envify() {
echo $1 | tr '[:lower:]' '[:upper:]' | sed "s/-/_/g"
}
@ -109,6 +116,14 @@ for pattern in "${PATTERNS_LIST[@]}" ; do
PATTERNS_LIST[$i]="nope{4000}"
fi
pattern_matches_empty=$(check_matches_empty_string "$pattern")
if [[ $pattern_matches_empty == 0 ]]; then
display_message "fingers-error: user defined pattern $pattern matched the empty string" 5000
PATTERNS_LIST[$i]="nope{4000}"
fi
i=$((i + 1))
done