fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
commit 9faa68973ad784c9f3b6327160eea05fe9be1e78
parent b4b111bd8c280607303091aecb2784344785d864
Author: John Hawthorn <john@hawthorn.email>
Date: Fri, 27 Dec 2019 17:33:57 -0800
Avoid VLA for lower_{needle,haystack}
Diffstat:
| M | src/match.c | | | 24 | +++++++++++++----------- |
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/src/match.c b/src/match.c @@ -56,6 +56,8 @@ void mat_print(score_t *mat, char name, const char *needle, const char *haystack } #endif +#define MATCH_MAX_LEN 1024 + static void precompute_bonus(const char *haystack, score_t *match_bonus) { /* Which positions are beginning of words */ int m = strlen(haystack); @@ -74,6 +76,15 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi int n = strlen(needle); int m = strlen(haystack); + if (m > MATCH_MAX_LEN || n > MATCH_MAX_LEN) { + /* + * Unreasonably large candidate: return no score + * If it is a valid match it will still be returned, it will + * just be ranked below any reasonably sized candidates + */ + return SCORE_MIN; + } + if (n == m) { /* Since this method can only be called with a haystack which * matches needle. If the lengths of the strings are equal the @@ -85,17 +96,8 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi return SCORE_MAX; } - if (m > 1024) { - /* - * Unreasonably large candidate: return no score - * If it is a valid match it will still be returned, it will - * just be ranked below any reasonably sized candidates - */ - return SCORE_MIN; - } - - char lower_needle[n]; - char lower_haystack[m]; + char lower_needle[MATCH_MAX_LEN]; + char lower_haystack[MATCH_MAX_LEN]; for (int i = 0; i < n; i++) lower_needle[i] = tolower(needle[i]);