Browse code

Use malloc in match_positions to avoid VLA

John Hawthorn authored on 28/12/2019 02:43:46
Showing 1 changed files

... ...
@@ -186,7 +186,9 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
186 186
 	 * D[][] Stores the best score for this position ending with a match.
187 187
 	 * M[][] Stores the best possible score at this position.
188 188
 	 */
189
-	score_t D[n][m], M[n][m];
189
+	score_t (*D)[MATCH_MAX_LEN], (*M)[MATCH_MAX_LEN];
190
+	M = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
191
+	D = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
190 192
 
191 193
 	score_t *last_D, *last_M;
192 194
 	score_t *curr_D, *curr_M;
... ...
@@ -230,5 +232,10 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
230 232
 		}
231 233
 	}
232 234
 
233
-	return M[n - 1][m - 1];
235
+	score_t result = M[n - 1][m - 1];
236
+
237
+	free(M);
238
+	free(D);
239
+
240
+	return result;
234 241
 }