Browse code

Avoid VLA in tty_interface

John Hawthorn authored on 28/12/2019 03:39:50
Showing 3 changed files

... ...
@@ -32,8 +32,6 @@ int has_match(const char *needle, const char *haystack) {
32 32
 
33 33
 #define max(a, b) (((a) > (b)) ? (a) : (b))
34 34
 
35
-#define MATCH_MAX_LEN 1024
36
-
37 35
 struct match_struct {
38 36
 	int needle_len;
39 37
 	int haystack_len;
... ...
@@ -7,6 +7,8 @@ typedef double score_t;
7 7
 #define SCORE_MAX INFINITY
8 8
 #define SCORE_MIN -INFINITY
9 9
 
10
+#define MATCH_MAX_LEN 1024
11
+
10 12
 int has_match(const char *needle, const char *haystack);
11 13
 score_t match_positions(const char *needle, const char *haystack, size_t *positions);
12 14
 score_t match(const char *needle, const char *haystack);
... ...
@@ -36,8 +36,8 @@ static void draw_match(tty_interface_t *state, const char *choice, int selected)
36 36
 	char *search = state->last_search;
37 37
 
38 38
 	int n = strlen(search);
39
-	size_t positions[n + 1];
40
-	for (int i = 0; i < n + 1; i++)
39
+	size_t positions[MATCH_MAX_LEN];
40
+	for (int i = 0; i < n + 1 && i < MATCH_MAX_LEN; i++)
41 41
 		positions[i] = -1;
42 42
 
43 43
 	score_t score = match_positions(search, choice, &positions[0]);