]> granicus.if.org Git - php/commitdiff
Fixed memory leaks.
authorIlia Alshanetsky <iliaa@php.net>
Tue, 14 Jun 2005 23:40:57 +0000 (23:40 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Tue, 14 Jun 2005 23:40:57 +0000 (23:40 +0000)
ext/date/lib/datetime.c
ext/date/php_date.c

index 8efbd9d622dbe4470338a1b1026bef537257ae83..f874a2526752e3163c2e4908082f85fca9e64e6d 100644 (file)
 #include "datetime.h"
 #include <ctype.h>
 
+#define PHP_TIME_FREE(m)       \
+       if (m) {                \
+               free(m);        \
+               m = NULL;       \
+       }                       \
+
 timelib_time* timelib_time_ctor()
 {
        timelib_time *t;
@@ -33,10 +39,8 @@ timelib_time* timelib_time_ctor()
 void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
 {
        int i;
-
-       if (tm->tz_abbr) {
-               free(tm->tz_abbr);
-       }
+       
+       PHP_TIME_FREE(tm->tz_abbr);
        tm->tz_abbr = strdup(tz_abbr);
        for (i = 0; i < strlen(tz_abbr); i++) {
                tm->tz_abbr[i] = toupper(tz_abbr[i]);
@@ -45,10 +49,12 @@ void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
 
 void timelib_time_dtor(timelib_time* t)
 {
-       if (t->tz_abbr) {
-               free(t->tz_abbr);
+       PHP_TIME_FREE(t->tz_abbr);
+       if (t->tz_info) {
+               timelib_tzinfo_dtor(t->tz_info);
+               t->tz_info = NULL;
        }
-       free(t);
+       PHP_TIME_FREE(t);
 }
 
 timelib_time_offset* timelib_time_offset_ctor()
@@ -61,10 +67,8 @@ timelib_time_offset* timelib_time_offset_ctor()
 
 void timelib_time_offset_dtor(timelib_time_offset* t)
 {
-       if (t->abbr) {
-               free(t->abbr);
-       }
-       free(t);
+       PHP_TIME_FREE(t->abbr);
+       PHP_TIME_FREE(t);
 }
 
 timelib_tzinfo* timelib_tzinfo_ctor(char *name)
@@ -105,13 +109,13 @@ timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
 
 void timelib_tzinfo_dtor(timelib_tzinfo *tz)
 {
-       free(tz->name);
-       free(tz->trans);
-       free(tz->trans_idx);
-       free(tz->type);
-       free(tz->timezone_abbr);
-       free(tz->leap_times);
-       free(tz);
+       PHP_TIME_FREE(tz->name);
+       PHP_TIME_FREE(tz->trans);
+       PHP_TIME_FREE(tz->trans_idx);
+       PHP_TIME_FREE(tz->type);
+       PHP_TIME_FREE(tz->timezone_abbr);
+       PHP_TIME_FREE(tz->leap_times);
+       PHP_TIME_FREE(tz);
 }
 
 char *timelib_get_tz_abbr_ptr(timelib_time *t)
index 115836c708bd3fe1098507552a1c26ad00b24928..ffa97dc8f6ea393a8a3b76aa29b5808f088c6e55 100644 (file)
@@ -128,6 +128,7 @@ PHP_FUNCTION(strtotime)
                now = timelib_time_ctor();
                timelib_unixtime2local(now, (signed long long) time(NULL), tzi);
        } else {
+               timelib_tzinfo_ctor(tzi);
                RETURN_FALSE;
        }
 
@@ -135,6 +136,16 @@ PHP_FUNCTION(strtotime)
        timelib_fill_holes(t, now, 0);
        timelib_update_ts(t, tzi);
        ts = timelib_date_to_int(t, &error);
+
+       /* if tz_info is not a copy, avoid double free */
+       if (now->tz_info == tzi) {
+               now->tz_info = NULL;
+       }
+
+       timelib_time_dtor(now); 
+       timelib_time_dtor(t);
+       timelib_tzinfo_dtor(tzi);
+
        if (error) {
                RETURN_FALSE;
        } else {