From d1f265499b1f6a5cbca85a319d056be58ee2835b Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Fri, 4 Nov 2005 19:19:45 +0000 Subject: [PATCH] ICU-4911 Fix a memory leak X-SVN-Rev: 18756 --- icu4c/source/common/putil.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/icu4c/source/common/putil.c b/icu4c/source/common/putil.c index 1377294fae8..4987af3b5e3 100644 --- a/icu4c/source/common/putil.c +++ b/icu4c/source/common/putil.c @@ -1213,7 +1213,8 @@ extern U_IMPORT char *U_TZNAME[]; #if defined(U_DARWIN) /* For Mac OS X */ #define TZZONELINK "/etc/localtime" #define TZZONEINFO "/usr/share/zoneinfo/" -static char *gTimeZoneBuffer = NULL; /* Heap allocated */ +static char gTimeZoneBuffer[MAXPATHLEN + 2]; /* Heap allocated */ +static char *gTimeZoneBufferPtr = NULL; #endif U_CAPI const char* U_EXPORT2 @@ -1224,11 +1225,8 @@ uprv_tzname(int n) if (id != NULL) { return id; } -#endif - -#if defined(U_DARWIN) +#elif defined(U_DARWIN) int ret; - char *tzenv; tzenv = getenv("TZFILE"); @@ -1236,6 +1234,18 @@ uprv_tzname(int n) return tzenv; } + /* Caller must handle threading issues */ + if (gTimeZoneBufferPtr == NULL) { + ret = readlink(TZZONELINK, gTimeZoneBuffer, sizeof(gTimeZoneBuffer)); + if (0 < ret) { + gTimeZoneBuffer[ret] = 0; + if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO, sizeof(TZZONEINFO) - 1) == 0) { + return (gTimeZoneBufferPtr = gTimeZoneBuffer + sizeof(TZZONEINFO) - 1); + } + } + } +#endif + #if 0 /* TZ is often set to "PST8PDT" or similar, so we cannot use it. Alan */ tzenv = getenv("TZ"); @@ -1244,23 +1254,6 @@ uprv_tzname(int n) } #endif - /* Caller must handle threading issues */ - if (gTimeZoneBuffer == NULL) { - gTimeZoneBuffer = (char *) uprv_malloc(MAXPATHLEN + 2); - - ret = readlink(TZZONELINK, gTimeZoneBuffer, MAXPATHLEN + 2); - if (0 < ret) { - gTimeZoneBuffer[ret] = '\0'; - if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO, sizeof(TZZONEINFO) - 1) == 0) { - return (gTimeZoneBuffer += sizeof(TZZONEINFO) - 1); - } - } - - uprv_free(gTimeZoneBuffer); - gTimeZoneBuffer = NULL; - } -#endif - #ifdef U_TZNAME return U_TZNAME[n]; #else