From: Jean-Philippe Orsini Date: Sat, 22 Feb 2014 09:52:40 +0000 (+0000) Subject: fixed mem leak X-Git-Tag: v1.3.0~39 X-Git-Url: https://git.wpitchoune.net/gitweb/?p=ppastats.git;a=commitdiff_plain;h=ffb8a406a1cf1b501dd3e3051e6028486c76f1e7 fixed mem leak --- diff --git a/src/lp.c b/src/lp.c index 11866d7..47d024e 100644 --- a/src/lp.c +++ b/src/lp.c @@ -237,11 +237,44 @@ int ddts_length(struct daily_download_total **ddts) return n; } +static struct daily_download_total *ddt_clone(struct daily_download_total *ddt) +{ + struct daily_download_total *new; + + new = malloc(sizeof(struct daily_download_total)); + + new->date = ddt->date; + new->count = ddt->count; + + return new; +} + +static struct daily_download_total ** +ddts_clone(struct daily_download_total **ddts) +{ + int n, i; + struct daily_download_total **new; + + n = ddts_length(ddts); + + new = malloc((n + 1) * sizeof(struct daily_download_total *)); + + for (i = 0; i < n; i++) + new[i] = ddt_clone(ddts[i]); + + new[n] = NULL; -struct daily_download_total **add_total + return new; +} + +/* + Return a newly allocated list with an additional ddt. + All ddts are cloned. + */ +static struct daily_download_total **add_ddt (struct daily_download_total **totals, struct daily_download_total *total) { - struct daily_download_total **cur; + struct daily_download_total **cur, **ddts, **result; struct daily_download_total *item; if (totals) { @@ -260,13 +293,15 @@ struct daily_download_total **add_total } } - item = malloc(sizeof(struct daily_download_total)); - memcpy(item, total, sizeof(struct daily_download_total)); + ddts = ddts_clone(totals); - return (struct daily_download_total **) - list_add((void **)totals, (void *)item); -} + result = (struct daily_download_total **) + list_add((void **)ddts, ddt_clone((void *)total)); + free(ddts); + + return result; +} struct daily_download_total ** ddts_merge(struct daily_download_total **ddts1, @@ -275,9 +310,7 @@ ddts_merge(struct daily_download_total **ddts1, struct daily_download_total **ddts, **cur, **tmp; if (ddts1) { - ddts = malloc((ddts_length(ddts1) + 1) - * sizeof(struct daily_download_total *)); - memcpy(ddts, ddts1, (ddts_length(ddts1) + 1) * sizeof(void *)); + ddts = ddts_clone(ddts1); } else { ddts = malloc(sizeof(struct daily_download_total *)); ddts[0] = NULL; @@ -285,9 +318,11 @@ ddts_merge(struct daily_download_total **ddts1, if (ddts2) for (cur = ddts2; *cur; cur++) { - tmp = add_total(ddts, *cur); - if (tmp != ddts) + tmp = add_ddt(ddts, *cur); + if (tmp != ddts) { + daily_download_total_list_free(ddts); ddts = tmp; + } } return ddts; diff --git a/src/lp_ws.c b/src/lp_ws.c index 74bcde5..3905a28 100644 --- a/src/lp_ws.c +++ b/src/lp_ws.c @@ -393,10 +393,12 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url) key = get_ddts_list_cache_key(binary_url); content = fcache_get(key); - if (content) + if (content) { json = json_tokener_parse(content); - else + free(content); + } else { json = NULL; + } if (json) { cached_ddts = json_object_to_daily_download_totals(json); @@ -426,7 +428,8 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url) } free(key); - free(cached_ddts); + if (ddts != cached_ddts) + free(cached_ddts); free(retrieved_ddts); return ddts;