Skip to content

Commit

Permalink
lib/data.c: avoid memleak if realloc fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Villemoes committed Jun 8, 2017
1 parent 77a4c15 commit 8d807b2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/data.c
Expand Up @@ -111,15 +111,16 @@ struct nl_data *nl_data_clone(const struct nl_data *src)
int nl_data_append(struct nl_data *data, const void *buf, size_t size)
{
if (size > 0) {
data->d_data = realloc(data->d_data, data->d_size + size);
if (!data->d_data)
void *d_data = realloc(data->d_data, data->d_size + size);
if (!d_data)
return -NLE_NOMEM;

if (buf)
memcpy(data->d_data + data->d_size, buf, size);
memcpy(d_data + data->d_size, buf, size);
else
memset(data->d_data + data->d_size, 0, size);
memset(d_data + data->d_size, 0, size);

data->d_data = d_data;
data->d_size += size;
}

Expand Down

0 comments on commit 8d807b2

Please sign in to comment.