From fd0d1582f7bb1d7a8ffe4b38b0e4d1bfce1bd063 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 19 Mar 2014 19:34:51 +0100 Subject: [PATCH] ical: fix memory leak in case of read error When EFREAD() does a "goto error", the code leaks the "r_trans" memory. Found with cppcheck. Adding cleanup code fixes that. To avoid a false cppcheck warning, the free() call must use the same variable name as the calloc() assignment. It is a bit more readable that way, too. --- src/syncevo/icaltz-util.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/syncevo/icaltz-util.c b/src/syncevo/icaltz-util.c index a30c925f..299499b6 100644 --- a/src/syncevo/icaltz-util.c +++ b/src/syncevo/icaltz-util.c @@ -325,7 +325,7 @@ icaltzutil_fetch_timezone (const char *location) time_t trans; int *trans_idx = NULL, dstidx = -1, stdidx = -1, pos, sign, zidx, zp_idx; ttinfo *types = NULL; - char *znames = NULL, *full_path, *tzid, *r_trans, *temp; + char *znames = NULL, *full_path, *tzid, *r_trans, *temp = NULL; leap *leaps = NULL; icalcomponent *tz_comp = NULL, *dst_comp = NULL, *std_comp = NULL; icalproperty *icalprop; @@ -363,9 +363,9 @@ icaltzutil_fetch_timezone (const char *location) num_types = decode (type_cnts.typecnt); transitions = calloc (num_trans, sizeof (time_t)); - r_trans = calloc (num_trans, 4); + temp = calloc (num_trans, 4); + r_trans = temp; EFREAD(r_trans, 4, num_trans, f); - temp = r_trans; if (num_trans) { trans_idx = calloc (num_trans, sizeof (int)); @@ -377,6 +377,7 @@ icaltzutil_fetch_timezone (const char *location) } free (temp); + temp = NULL; types = calloc (num_types, sizeof (ttinfo)); for (i = 0; i < num_types; i++) { @@ -556,6 +557,8 @@ error: free (full_path); if (leaps) free (leaps); + if (temp) + free (temp); return tz_comp; }