summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2019-12-27 17:33:57 -0800
committerJohn Hawthorn <john@hawthorn.email>2019-12-27 17:33:57 -0800
commit8ebc48041ab1c6e2b5a5532304ab6acdb3a2b63d (patch)
treeeb4b29340fb1a8c5664eeb311baf36b7b0a6b892
parentf770d7da17850036df887b2f41a86c38ba255135 (diff)
Avoid VLA for lower_{needle,haystack}
-rw-r--r--src/match.c24
1 files 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]);