From 0eb01e8a0c2a3c8e20c56dd029d74f0d8a95af00 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 20 Sep 2014 21:22:06 -0500 Subject: [PROPOSED PATCH 1/3] Fix memory leak if ALL_STATE and multithreaded. * localtime.c (gmtcheck) [ALL_STATE]: Don't leak memory if two threads invoke this function nearly simultaneously. Problem reported by Andy Heninger in: http://mm.icann.org/pipermail/tz/2014-September/021599.html * NEWS: Document this. --- NEWS | 4 ++++ localtime.c | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 29cf395..56ae660 100644 --- a/NEWS +++ b/NEWS @@ -30,6 +30,10 @@ Unreleased, experimental changes An access to uninitalized data has been fixed. (Thanks to Jörg Richter for reporting the problem.) + A memory leak has been fixed if ALL_STATE and THREAD_SAFE are defined + and two threads race to initialize data used by gmtime-like functions. + (Thanks to Andy Heninger for reporting the problem.) + Changes affecting build procedure 'make check' now checks better for properly-sorted data. diff --git a/localtime.c b/localtime.c index 62743e2..ad1241b 100644 --- a/localtime.c +++ b/localtime.c @@ -1249,12 +1249,14 @@ gmtcheck(void) return; if (lock() != 0) return; + if (! gmt_is_set) { #ifdef ALL_STATE - gmtptr = malloc(sizeof *gmtptr); + gmtptr = malloc(sizeof *gmtptr); #endif - if (gmtptr) - gmtload(gmtptr); - gmt_is_set = true; + if (gmtptr) + gmtload(gmtptr); + gmt_is_set = true; + } unlock(); } -- 1.9.3