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:
parent
b04eca213e
commit
77df97b59b
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue