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().
This commit is contained in:
Danrich Parrol 2015-02-03 22:02:04 -08:00
parent b04eca213e
commit 77df97b59b
1 changed files with 2 additions and 4 deletions

View File

@ -14,16 +14,14 @@ int damerau_levenshtein_distance(const char *s1, const char *s2)
size_t d1, d2, d3, d4, result; size_t d1, d2, d3, d4, result;
unsigned short cost; unsigned short cost;
size_t *da = malloc(256 * sizeof(size_t)); size_t *da = calloc(256, sizeof(size_t));
if (!da) { if (!da) {
return -1; return -1;
} }
for(i = 0; i < 256; i++) {
da[i] = 0;
}
size_t *dist = malloc((len1 + 2) * cols * sizeof(size_t)); size_t *dist = malloc((len1 + 2) * cols * sizeof(size_t));
if (!dist) { if (!dist) {
free(da);
return -1; return -1;
} }