From 8ebc48041ab1c6e2b5a5532304ab6acdb3a2b63d Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Fri, 27 Dec 2019 17:33:57 -0800 Subject: Avoid VLA for lower_{needle,haystack} --- src/match.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/match.c b/src/match.c index 2a0b058..1081be3 100644 --- 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]); -- cgit v1.2.3