summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllen Winter <allen.winter@kdab.com>2015-05-20 18:08:55 -0400
committerAllen Winter <allen.winter@kdab.com>2015-05-20 18:08:55 -0400
commit82a368195b73a8338ac6df48404544aee1e2b3ba (patch)
tree112fa44a010a8d448304c440af6018d9e4739f83
parentf4641bf7c666e3f2130462c721513e5c359cb105 (diff)
downloadlibical-git-82a368195b73a8338ac6df48404544aee1e2b3ba.tar.gz
icaltz-util.c - major cleanup, and test malloc and calloc failures
-rw-r--r--src/libical/icaltz-util.c373
1 files changed, 208 insertions, 165 deletions
diff --git a/src/libical/icaltz-util.c b/src/libical/icaltz-util.c
index a281bcd9..902a4b82 100644
--- a/src/libical/icaltz-util.c
+++ b/src/libical/icaltz-util.c
@@ -57,18 +57,22 @@
#ifdef _MSC_VER
#if !defined(HAVE_BYTESWAP_H) && !defined(HAVE_SYS_ENDIAN_H) && !defined(HAVE_ENDIAN_H)
#define bswap_16(x) (((x) << 8) & 0xff00) | (((x) >> 8 ) & 0xff)
-#define bswap_32(x) (((x) << 24) & 0xff000000) \
- | (((x) << 8) & 0xff0000) \
- | (((x) >> 8) & 0xff00) \
- | (((x) >> 24) & 0xff )
-#define bswap_64(x) ((((x) & 0xff00000000000000ull) >> 56) \
- | (((x) & 0x00ff000000000000ull) >> 40) \
- | (((x) & 0x0000ff0000000000ull) >> 24) \
- | (((x) & 0x000000ff00000000ull) >> 8) \
- | (((x) & 0x00000000ff000000ull) << 8) \
- | (((x) & 0x0000000000ff0000ull) << 24) \
- | (((x) & 0x000000000000ff00ull) << 40) \
- | (((x) & 0x00000000000000ffull) << 56))
+
+#define bswap_32(x) \
+(((x) << 24) & 0xff000000) | \
+(((x) << 8) & 0xff0000) | \
+(((x) >> 8) & 0xff00) | \
+(((x) >> 24) & 0xff)
+
+#define bswap_64(x) \
+((((x) & 0xff00000000000000ull) >> 56) | \
+(((x) & 0x00ff000000000000ull) >> 40) | \
+(((x) & 0x0000ff0000000000ull) >> 24) | \
+(((x) & 0x000000ff00000000ull) >> 8) | \
+(((x) & 0x00000000ff000000ull) << 8) | \
+(((x) & 0x0000000000ff0000ull) << 24) | \
+(((x) & 0x000000000000ff00ull) << 40) | \
+(((x) & 0x00000000000000ffull) << 56))
#endif
#include <io.h>
#endif
@@ -81,7 +85,7 @@
typedef struct
{
- char ttisgmtcnt [4];
+ char ttisgmtcnt[4];
char ttisstdcnt[4];
char leapcnt[4];
char timecnt[4];
@@ -98,10 +102,10 @@ static char *search_paths[] = {
};
#define EFREAD(buf,size,num,fs) \
- if (fread (buf, size, num, fs) == 0 && ferror (fs)) {\
- icalerror_set_errno (ICAL_FILE_ERROR); \
- goto error; \
- } \
+if (fread(buf, size, num, fs) < num && ferror (fs)) { \
+ icalerror_set_errno(ICAL_FILE_ERROR); \
+ goto error; \
+}
typedef struct
{
@@ -123,30 +127,29 @@ typedef struct
static int decode(const void *ptr)
{
#if defined(sun) && defined(__SVR4)
- if (sizeof (int) == 4)
+ if (sizeof (int) == 4) {
#ifdef _BIG_ENDIAN
return *(const int *) ptr;
#else
return BSWAP_32 (*(const int *) ptr);
#endif
#else
- if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
+ if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4) {
return *(const int *) ptr;
- else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
+ } else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4) {
return (int)bswap_32(*(const unsigned int *) ptr);
#endif
- else
- {
- const unsigned char *p = ptr;
- int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
+ } else {
+ const unsigned char *p = ptr;
+ int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
- result = (result << 8) | *p++;
- result = (result << 8) | *p++;
- result = (result << 8) | *p++;
- result = (result << 8) | *p++;
+ result = (result << 8) | *p++;
+ result = (result << 8) | *p++;
+ result = (result << 8) | *p++;
+ result = (result << 8) | *p++;
- return result;
- }
+ return result;
+ }
}
static char *zname_from_stridx(char *str, long idx)
@@ -177,9 +180,9 @@ static void set_zonedir(void)
num_search_paths = sizeof(search_paths) / sizeof(search_paths[0]);
for (i = 0; i < num_search_paths; i++) {
- snprintf(file_path, MAXPATHLEN, "%s/%s", search_paths [i], fname);
- if (!access (file_path, F_OK|R_OK)) {
- zdir = search_paths [i];
+ snprintf(file_path, MAXPATHLEN, "%s/%s", search_paths[i], fname);
+ if (!access(file_path, F_OK|R_OK)) {
+ zdir = search_paths[i];
break;
}
}
@@ -195,44 +198,52 @@ const char *icaltzutil_get_zone_directory(void)
icalcomponent *icaltzutil_fetch_timezone(const char *location)
{
- int ret = 0;
- FILE *f;
tzinfo type_cnts;
size_t i, num_trans, num_types, num_chars, num_leaps, num_isstd, num_isgmt;
size_t size;
+
+ const char *zonedir;
+ FILE *f = NULL;
+ char *full_path = NULL;
time_t *transitions = NULL;
- time_t start, end;
- int *trans_idx = NULL, idx, prev_idx;
+ char *r_trans = NULL, *temp;
+ int *trans_idx = NULL;
ttinfo *types = NULL;
- char *znames = NULL, *full_path, *tzid, *r_trans, *temp;
+ char *znames = NULL;
leap *leaps = NULL;
+ char *tzid = NULL;
+
+ time_t start, end;
+ int idx, prev_idx;
icalcomponent *tz_comp = NULL, *comp = NULL;
icalproperty *icalprop;
icaltimetype dtstart;
- const char *basedir;
- if(icaltimezone_get_builtin_tzdata()) {
- return NULL;
+ if (icaltimezone_get_builtin_tzdata()) {
+ goto error;
}
- basedir = icaltzutil_get_zone_directory();
- if (!basedir) {
- icalerror_set_errno (ICAL_FILE_ERROR);
- return NULL;
+ zonedir = icaltzutil_get_zone_directory();
+ if (!zonedir) {
+ icalerror_set_errno(ICAL_FILE_ERROR);
+ goto error;
}
- size = strlen(basedir) + strlen(location) + 2;
+ size = strlen(zonedir) + strlen(location) + 2;
full_path = (char *)malloc(size);
- snprintf(full_path, size, "%s/%s", basedir, location);
- if ((f = fopen (full_path, "rb")) == 0) {
- icalerror_set_errno (ICAL_FILE_ERROR);
- free (full_path);
- return NULL;
+ if (full_path == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
+ snprintf(full_path, size, "%s/%s", zonedir, location);
+ if ((f = fopen(full_path, "rb")) == 0) {
+ icalerror_set_errno(ICAL_FILE_ERROR);
+ goto error;
}
- if ((ret = fseek (f, 20, SEEK_SET)) != 0) {
- icalerror_set_errno (ICAL_FILE_ERROR);
- goto error;
+ if (fseek(f, 20, SEEK_SET) != 0) {
+ icalerror_set_errno(ICAL_FILE_ERROR);
+ goto error;
}
EFREAD(&type_cnts, 24, 1, f);
@@ -245,163 +256,195 @@ icalcomponent *icaltzutil_fetch_timezone(const char *location)
num_types = (size_t)decode(type_cnts.typecnt);
transitions = calloc(num_trans, sizeof(time_t));
+ if (transitions == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
r_trans = calloc(num_trans, 4);
+ if (r_trans == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
+
EFREAD(r_trans, 4, num_trans, f);
temp = r_trans;
-
if (num_trans) {
- trans_idx = calloc (num_trans, sizeof (int));
- for (i = 0; i < num_trans; i++) {
- trans_idx [i] = fgetc (f);
- transitions[i] = (time_t)decode(r_trans);
- r_trans += 4;
- }
+ trans_idx = calloc(num_trans, sizeof(int));
+ if (trans_idx == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
+ for (i = 0; i < num_trans; i++) {
+ trans_idx[i] = fgetc(f);
+ transitions[i] = (time_t)decode(r_trans);
+ r_trans += 4;
+ }
}
+ r_trans = temp;
- free (temp);
-
- types = calloc (num_types, sizeof (ttinfo));
+ types = calloc(num_types, sizeof(ttinfo));
+ if (types == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
for (i = 0; i < num_types; i++) {
- unsigned char a [4];
- int c;
-
- EFREAD(a, 4, 1, f);
- c = fgetc (f);
- types[i].isdst = (unsigned char)c;
- if((c = fgetc (f)) < 0) {
- c = 0;
- break;
- }
- types[i].abbr = (unsigned int)c;
- types[i].gmtoff = decode (a);
+ unsigned char a[4];
+ int c;
+
+ EFREAD(a, 4, 1, f);
+ c = fgetc(f);
+ types[i].isdst = (unsigned char)c;
+ if ((c = fgetc(f)) < 0) {
+ c = 0;
+ break;
+ }
+ types[i].abbr = (unsigned int)c;
+ types[i].gmtoff = decode(a);
}
- znames = (char *) malloc (num_chars);
+ znames = (char *)malloc(num_chars);
+ if (znames == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
EFREAD(znames, num_chars, 1, f);
/* We got all the information which we need */
- leaps = calloc (num_leaps, sizeof (leap));
+ leaps = calloc(num_leaps, sizeof(leap));
+ if (leaps == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
for (i = 0; i < num_leaps; i++) {
- char c [4];
+ char c[4];
- EFREAD (c, 4, 1, f);
- leaps [i].transition = (time_t)decode(c);
+ EFREAD(c, 4, 1, f);
+ leaps[i].transition = (time_t)decode(c);
- EFREAD (c, 4, 1, f);
- leaps [i].change = decode (c);
+ EFREAD(c, 4, 1, f);
+ leaps[i].change = decode(c);
}
for (i = 0; i < num_isstd; ++i) {
- int c = getc (f);
- types [i].isstd = c != 0;
+ int c = getc(f);
+ types[i].isstd = c != 0;
}
- while (i < num_types)
- types [i++].isstd = 0;
+ while (i < num_types) {
+ types[i++].isstd = 0;
+ }
for (i = 0; i < num_isgmt; ++i) {
- int c = getc (f);
- types [i].isgmt = c != 0;
+ int c = getc(f);
+ types[i].isgmt = c != 0;
}
- while (i < num_types)
- types [i++].isgmt = 0;
+ while (i < num_types) {
+ types[i++].isgmt = 0;
+ }
/* Read all the contents now */
- for (i = 0; i < num_types; i++)
- types [i].zname = zname_from_stridx(znames, (long)types[i].abbr);
+ for (i = 0; i < num_types; i++) {
+ types[i].zname = zname_from_stridx(znames, (long)types[i].abbr);
+ }
- tz_comp = icalcomponent_new (ICAL_VTIMEZONE_COMPONENT);
+ tz_comp = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT);
/* Add tzid property */
- size = strlen(icaltimezone_tzid_prefix()) + strlen (location) + 1;
+ size = strlen(icaltimezone_tzid_prefix()) + strlen(location) + 1;
tzid = (char *)malloc(size);
+ if (tzid == NULL) {
+ icalerror_set_errno(ICAL_NEWFAILED_ERROR);
+ goto error;
+ }
snprintf(tzid, size, "%s%s", icaltimezone_tzid_prefix(), location);
- icalprop = icalproperty_new_tzid (tzid);
- icalcomponent_add_property (tz_comp, icalprop);
- free (tzid);
+ icalprop = icalproperty_new_tzid(tzid);
+ icalcomponent_add_property(tz_comp, icalprop);
- icalprop = icalproperty_new_x (location);
- icalproperty_set_x_name (icalprop, "X-LIC-LOCATION");
- icalcomponent_add_property (tz_comp, icalprop);
+ icalprop = icalproperty_new_x(location);
+ icalproperty_set_x_name(icalprop, "X-LIC-LOCATION");
+ icalcomponent_add_property(tz_comp, icalprop);
prev_idx = 0;
if (num_trans == 0) {
- prev_idx = idx = 0;
-
+ prev_idx = idx = 0;
} else {
- idx = trans_idx[0];
+ idx = trans_idx[0];
}
start = 0;
for (i = 1; i < num_trans; i++, start = end) {
- prev_idx = idx;
- idx = trans_idx [i];
- end = transitions [i] + types [prev_idx].gmtoff;
- /* don't bother starting until the epoch */
- if (0 > end)
- continue;
-
- if (types [prev_idx].isdst)
- comp = icalcomponent_new (ICAL_XDAYLIGHT_COMPONENT);
- else
- comp = icalcomponent_new (ICAL_XSTANDARD_COMPONENT);
- icalprop = icalproperty_new_tzname (types [prev_idx].zname);
- icalcomponent_add_property (comp, icalprop);
- dtstart = icaltime_from_timet(start, 0);
- icalprop = icalproperty_new_dtstart (dtstart);
- icalcomponent_add_property (comp, icalprop);
- icalprop = icalproperty_new_tzoffsetfrom (types [idx].gmtoff);
- icalcomponent_add_property (comp, icalprop);
- icalprop = icalproperty_new_tzoffsetto (types [prev_idx].gmtoff);
- icalcomponent_add_property (comp, icalprop);
- icalcomponent_add_component (tz_comp, comp);
+ prev_idx = idx;
+ idx = trans_idx[i];
+ end = transitions[i] + types[prev_idx].gmtoff;
+ /* don't bother starting until the epoch */
+ if (0 > end)
+ continue;
+
+ if (types[prev_idx].isdst) {
+ comp = icalcomponent_new(ICAL_XDAYLIGHT_COMPONENT);
+ } else {
+ comp = icalcomponent_new(ICAL_XSTANDARD_COMPONENT);
+ }
+ icalprop = icalproperty_new_tzname(types[prev_idx].zname);
+ icalcomponent_add_property(comp, icalprop);
+ dtstart = icaltime_from_timet(start, 0);
+ icalprop = icalproperty_new_dtstart(dtstart);
+ icalcomponent_add_property(comp, icalprop);
+ icalprop = icalproperty_new_tzoffsetfrom(types[idx].gmtoff);
+ icalcomponent_add_property(comp, icalprop);
+ icalprop = icalproperty_new_tzoffsetto(types[prev_idx].gmtoff);
+ icalcomponent_add_property(comp, icalprop);
+ icalcomponent_add_component(tz_comp, comp);
}
/* finally, add a last zone with no end date */
- if (types [idx].isdst)
- comp = icalcomponent_new (ICAL_XDAYLIGHT_COMPONENT);
- else
- comp = icalcomponent_new (ICAL_XSTANDARD_COMPONENT);
- icalprop = icalproperty_new_tzname (types [idx].zname);
- icalcomponent_add_property (comp, icalprop);
+ if (types[idx].isdst) {
+ comp = icalcomponent_new(ICAL_XDAYLIGHT_COMPONENT);
+ } else {
+ comp = icalcomponent_new(ICAL_XSTANDARD_COMPONENT);
+ }
+ icalprop = icalproperty_new_tzname(types[idx].zname);
+ icalcomponent_add_property(comp, icalprop);
dtstart = icaltime_from_timet(start, 0);
- icalprop = icalproperty_new_dtstart (dtstart);
- icalcomponent_add_property (comp, icalprop);
- icalprop = icalproperty_new_tzoffsetfrom (types [prev_idx].gmtoff);
- icalcomponent_add_property (comp, icalprop);
- icalprop = icalproperty_new_tzoffsetto (types [idx].gmtoff);
- icalcomponent_add_property (comp, icalprop);
- icalcomponent_add_component (tz_comp, comp);
-
+ icalprop = icalproperty_new_dtstart(dtstart);
+ icalcomponent_add_property(comp, icalprop);
+ icalprop = icalproperty_new_tzoffsetfrom(types[prev_idx].gmtoff);
+ icalcomponent_add_property(comp, icalprop);
+ icalprop = icalproperty_new_tzoffsetto(types[idx].gmtoff);
+ icalcomponent_add_property(comp, icalprop);
+ icalcomponent_add_component(tz_comp, comp);
error:
- if (f)
- fclose (f);
-
- if (transitions)
- free (transitions);
- if (trans_idx)
- free (trans_idx);
- if (types) {
- for (i = 0; i < num_types; i++)
- if (types [i].zname)
- free (types [i].zname);
- free (types);
+ if (f)
+ fclose(f);
+
+ if (full_path)
+ free(full_path);
+
+ if (transitions)
+ free(transitions);
+
+ if (r_trans)
+ free(r_trans);
+
+ if (trans_idx)
+ free(trans_idx);
+
+ if (types) {
+ for (i = 0; i < num_types; i++) {
+ if (types[i].zname) {
+ free(types[i].zname);
+ }
}
- if (znames)
- free (znames);
- free (full_path);
- if (leaps)
- free (leaps);
+ free(types);
+ }
- return tz_comp;
-}
+ if (znames)
+ free(znames);
-/*
-int
-main (int argc, char *argv [])
-{
- tzutil_fetch_timezone (argv [1]);
- return 0;
-}*/
+ if (leaps)
+ free(leaps);
+
+ return tz_comp;
+}