updated code to avoid memory leaks.

This commit is contained in:
Nikos Mavrogiannopoulos
2013-11-05 20:07:09 +01:00
parent 74f0ba5e72
commit b1633b2eb1
5 changed files with 22 additions and 8 deletions

View File

@@ -42,6 +42,18 @@ void str_clear(str_st * str)
str->length = 0;
}
void *safe_realloc(void *ptr, size_t size)
{
void* tmp, *ret;
tmp = ptr;
ret = realloc(ptr, size);
if (ret == NULL) {
free(tmp);
}
return ret;
}
#define MIN_CHUNK 64
/* This function makes sure there is an additional byte in dest;
*/
@@ -70,7 +82,7 @@ int str_append_size(str_st * dest, size_t data_size)
MAX(data_size, MIN_CHUNK) + MAX(dest->max_length,
MIN_CHUNK);
dest->allocd = realloc(dest->allocd, new_len+1);
dest->allocd = safe_realloc(dest->allocd, new_len+1);
if (dest->allocd == NULL)
return ERR_MEM;
dest->max_length = new_len;