From 77df97b59b228bab1fc509dfd83bb3dbe431bb41 Mon Sep 17 00:00:00 2001 From: Danrich Parrol Date: Tue, 3 Feb 2015 22:02:04 -0800 Subject: [PATCH] Fix potential memory leak. If the second memory allocation fails, then we need to free the first. While we're at it, the zeroing of allocated memory can be replaced by a call to calloc(). --- cjellyfish/damerau_levenshtein.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cjellyfish/damerau_levenshtein.c b/cjellyfish/damerau_levenshtein.c index 99ba1d4..c24e604 100644 --- a/cjellyfish/damerau_levenshtein.c +++ b/cjellyfish/damerau_levenshtein.c @@ -14,16 +14,14 @@ int damerau_levenshtein_distance(const char *s1, const char *s2) size_t d1, d2, d3, d4, result; unsigned short cost; - size_t *da = malloc(256 * sizeof(size_t)); + size_t *da = calloc(256, sizeof(size_t)); if (!da) { return -1; } - for(i = 0; i < 256; i++) { - da[i] = 0; - } size_t *dist = malloc((len1 + 2) * cols * sizeof(size_t)); if (!dist) { + free(da); return -1; }