diff options
author | Colin Ian King <colin.king@canonical.com> | 2014-02-18 08:51:30 +0100 |
---|---|---|
committer | Daniel Lezcano <daniel.lezcano@free.fr> | 2014-02-18 08:51:30 +0100 |
commit | 69dde53657504f92e1f13c321a791917e79249cf (patch) | |
tree | a8731597109f2bda36a55766de6613ce0a076928 | |
parent | 7ea55129d4947203b9177e47ecc66631a427e950 (diff) |
Free memory on realloc failure.
Fix a common coding error where realloc fails and the original
allocated data is not free'd.
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
-rw-r--r-- | idlestat.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -188,6 +188,7 @@ static struct cpuidle_cstate *inter(struct cpuidle_cstate *c1, for (i = 0, index = 0; i < c1->nrdata; i++) { for (j = index; j < c2->nrdata; j++) { + struct cpuidle_data *tmp; /* intervals are ordered, no need to go further */ if (c1->data[i].end < c2->data[j].begin) @@ -216,10 +217,13 @@ static struct cpuidle_cstate *inter(struct cpuidle_cstate *c1, result->nrdata++; - data = realloc(data, sizeof(*data) * + tmp = realloc(data, sizeof(*data) * (result->nrdata + 1)); - if (!data) + if (!tmp) { + free(data); return NULL; + } + data = tmp; result->data = data; result->data[result->nrdata - 1] = *interval; @@ -236,7 +240,7 @@ static int store_data(double time, int state, int cpu, { struct cpuidle_cstates *cstates = &datas->cstates[cpu]; struct cpuidle_cstate *cstate; - struct cpuidle_data *data; + struct cpuidle_data *data, *tmp; int nrdata, last_cstate = cstates->last_cstate; /* ignore when we got a "closing" state first */ @@ -280,9 +284,12 @@ static int store_data(double time, int state, int cpu, return 0; } - data = realloc(data, sizeof(*data) * (nrdata + 1)); - if (!data) + tmp = realloc(data, sizeof(*data) * (nrdata + 1)); + if (!tmp) { + free(data); return error("realloc data");; + } + data = tmp; data[nrdata].begin = time; |