From 8727fcc341272a7e57390d78bd2b69f6d63445a6 Mon Sep 17 00:00:00 2001 From: alessiosavi <31842468+alessiosavi@users.noreply.github.com> Date: Mon, 12 Aug 2019 14:29:22 +0200 Subject: [PATCH] Fix memory leak in case of early return Closes GH-7. Reviewed-by: Titus Wormer --- levenshtein.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/levenshtein.c b/levenshtein.c index b816aa7..efed1ac 100644 --- a/levenshtein.c +++ b/levenshtein.c @@ -10,7 +10,7 @@ // See for more information. size_t levenshtein_n(const char *a, const size_t length, const char *b, const size_t bLength) { - size_t *cache = calloc(length, sizeof(size_t)); + size_t index = 0; size_t bIndex = 0; size_t distance; @@ -30,6 +30,7 @@ levenshtein_n(const char *a, const size_t length, const char *b, const size_t bL if (bLength == 0) { return length; } + size_t *cache = calloc(length, sizeof(size_t)); // initialize the vector. while (index < length) {