From c2392d0ca30b8982185a3821be5b327d5edaa712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Sacawa?= Date: Thu, 10 Jun 2021 17:16:39 -0400 Subject: [PATCH] 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. --- README.md | 3 ++- scripts/config.sh | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d6612b6..639e52a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/scripts/config.sh b/scripts/config.sh index b90df94..1939fcf 100755 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -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