php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #67343
Patch patch-timlib-c.patch revision 2014-05-28 00:53 UTC by al-phpbug at none dot at

Patch patch-timlib-c.patch for Date/time related Bug #67343

Patch version 2014-05-28 00:53 UTC

Return to Bug #67343 | Download this patch
Patch Revisions:

Developer: al-phpbug@none.at

commit 5fab4b145c3f721fd415876e37d7a0eddb0e49d8
Author: git001 <al-git001@none.at>
Date:   Wed May 28 02:47:32 2014 +0200

    Catch memory errors

diff --git a/ext/date/lib/timelib.c b/ext/date/lib/timelib.c
index bf7c55a..884de09 100644
--- a/ext/date/lib/timelib.c
+++ b/ext/date/lib/timelib.c
@@ -24,7 +24,7 @@
 
 #define TIMELIB_TIME_FREE(m) 	\
 	if (m) {		\
-		free(m);	\
+		efree(m);	\
 		m = NULL;	\
 	}			\
 
@@ -35,7 +35,7 @@
 timelib_time* timelib_time_ctor(void)
 {
 	timelib_time *t;
-	t = calloc(1, sizeof(timelib_time));
+	t = (timelib_time *)ecalloc(1, sizeof(timelib_time));
 
 	return t;
 }
@@ -43,7 +43,7 @@ timelib_time* timelib_time_ctor(void)
 timelib_rel_time* timelib_rel_time_ctor(void)
 {
 	timelib_rel_time *t;
-	t = calloc(1, sizeof(timelib_rel_time));
+	t = (timelib_rel_time *)ecalloc(1, sizeof(timelib_rel_time));
 
 	return t;
 }
@@ -51,19 +51,36 @@ timelib_rel_time* timelib_rel_time_ctor(void)
 timelib_time* timelib_time_clone(timelib_time *orig)
 {
 	timelib_time *tmp = timelib_time_ctor();
+
+        if (UNEXPECTED(tmp == NULL)) {
+                return tmp;
+        }
+
 	memcpy(tmp, orig, sizeof(timelib_time));
 	if (orig->tz_abbr) {
-		tmp->tz_abbr = strdup(orig->tz_abbr);
+		tmp->tz_abbr = (char *) estrdup(orig->tz_abbr);
+
+		if (UNEXPECTED(tmp->tz_abbr == NULL)) {
+			TIMELIB_TIME_FREE(tmp);
+			return tmp;
+		}
+
 	}
 	if (orig->tz_info) {
 		tmp->tz_info = orig->tz_info;
 	}
+
 	return tmp;
 }
 
 timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
 {
 	timelib_rel_time *tmp = timelib_rel_time_ctor();
+
+        if (UNEXPECTED(tmp == NULL)) {
+                return tmp;
+        }
+
 	memcpy(tmp, rel, sizeof(timelib_rel_time));
 	return tmp;
 }
@@ -73,7 +90,13 @@ void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
 	unsigned int i;
 	
 	TIMELIB_TIME_FREE(tm->tz_abbr);
-	tm->tz_abbr = strdup(tz_abbr);
+	tm->tz_abbr = (char *)estrdup(tz_abbr);
+
+	/* TODO: What is the right way to handle this case? 20140528 */
+	if (UNEXPECTED(tm->tz_abbr == NULL)) {
+		return;
+	}
+
 	for (i = 0; i < strlen(tz_abbr); i++) {
 		tm->tz_abbr[i] = toupper(tz_abbr[i]);
 	}
@@ -93,7 +116,7 @@ void timelib_rel_time_dtor(timelib_rel_time* t)
 timelib_time_offset* timelib_time_offset_ctor(void)
 {
 	timelib_time_offset *t;
-	t = calloc(1, sizeof(timelib_time_offset));
+	t = (timelib_time_offset *)ecalloc(1, sizeof(timelib_time_offset));
 
 	return t;
 }
@@ -107,8 +130,19 @@ void timelib_time_offset_dtor(timelib_time_offset* t)
 timelib_tzinfo* timelib_tzinfo_ctor(char *name)
 {
 	timelib_tzinfo *t;
-	t = calloc(1, sizeof(timelib_tzinfo));
-	t->name = strdup(name);
+	t = (timelib_tzinfo *)ecalloc(1, sizeof(timelib_tzinfo));
+
+	if (UNEXPECTED(t == NULL)) {
+		return t;
+	}
+
+	t->name = (char *)estrdup(name);
+
+	if (UNEXPECTED(t->name == NULL)) {
+		TIMELIB_TIME_FREE(t);
+		return t;
+	}
+
 
 	return t;
 }
@@ -116,6 +150,11 @@ timelib_tzinfo* timelib_tzinfo_ctor(char *name)
 timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
 {
 	timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
+
+        if (UNEXPECTED(tmp == NULL)) {
+		return tmp;
+	}
+
 	tmp->ttisgmtcnt = tz->ttisgmtcnt;
 	tmp->ttisstdcnt = tz->ttisstdcnt;
 	tmp->leapcnt = tz->leapcnt;
@@ -123,18 +162,58 @@ timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
 	tmp->typecnt = tz->typecnt;
 	tmp->charcnt = tz->charcnt;
 	
-	tmp->trans = (int32_t *) malloc(tz->timecnt * sizeof(int32_t));
-	tmp->trans_idx = (unsigned char*) malloc(tz->timecnt * sizeof(unsigned char));
+	tmp->trans = (int32_t *) emalloc(tz->timecnt * sizeof(int32_t));
+
+	if (UNEXPECTED(tmp->trans == NULL)) {
+		TIMELIB_TIME_FREE(tmp);
+		return tmp;
+	}
+
+	tmp->trans_idx = (unsigned char*) emalloc(tz->timecnt * sizeof(unsigned char));
+
+	if (UNEXPECTED(tmp->trans_idx == NULL)) {
+		TIMELIB_TIME_FREE(tmp->trans);
+		TIMELIB_TIME_FREE(tmp);
+		return tmp;
+	}
+
 	memcpy(tmp->trans, tz->trans, tz->timecnt * sizeof(int32_t));
 	memcpy(tmp->trans_idx, tz->trans_idx, tz->timecnt * sizeof(unsigned char));
 
-	tmp->type = (ttinfo*) malloc(tz->typecnt * sizeof(struct ttinfo));
+	tmp->type = (ttinfo*) emalloc(tz->typecnt * sizeof(struct ttinfo));
+
+	if (UNEXPECTED(tmp->type == NULL)) {
+		TIMELIB_TIME_FREE(tmp->trans_idx);
+		TIMELIB_TIME_FREE(tmp->trans);
+		TIMELIB_TIME_FREE(tmp);
+		return tmp;
+	}
+
 	memcpy(tmp->type, tz->type, tz->typecnt * sizeof(struct ttinfo));
 
-	tmp->timezone_abbr = (char*) malloc(tz->charcnt);
+	tmp->timezone_abbr = (char*) emalloc(tz->charcnt);
+
+        if (UNEXPECTED(tmp->timezone_abbr == NULL)) {
+		TIMELIB_TIME_FREE(tmp->type);
+                TIMELIB_TIME_FREE(tmp->trans_idx);
+                TIMELIB_TIME_FREE(tmp->trans);
+                TIMELIB_TIME_FREE(tmp);
+                return tmp;
+        }
+
 	memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->charcnt);
 
-	tmp->leap_times = (tlinfo*) malloc(tz->leapcnt * sizeof(tlinfo));
+	tmp->leap_times = (tlinfo*) emalloc(tz->leapcnt * sizeof(tlinfo));
+
+        if (UNEXPECTED(tmp->leap_times == NULL)) {
+                TIMELIB_TIME_FREE(tmp->timezone_abbr);
+                TIMELIB_TIME_FREE(tmp->type);
+                TIMELIB_TIME_FREE(tmp->trans_idx);
+                TIMELIB_TIME_FREE(tmp->trans);
+                TIMELIB_TIME_FREE(tmp);
+                return tmp;
+        }
+
 	memcpy(tmp->leap_times, tz->leap_times, tz->leapcnt * sizeof(tlinfo));
 
 	return tmp;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 13:01:30 2024 UTC