]> TLD Linux GIT Repositories - packages/php.git/blob - php-systzdata.patch
- updated to 7.2.34
[packages/php.git] / php-systzdata.patch
1 # License: MIT
2 # http://opensource.org/licenses/MIT
3
4 Add support for use of the system timezone database, rather
5 than embedding a copy.  Discussed upstream but was not desired.
6
7 History:
8 r17: adapt for autotool change in 7.2.16RC1
9 r16: adapt for timelib 2017.06 (in 7.2.3RC1)
10 r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
11 r14: improve check for valid tz file
12 r13: adapt for upstream changes to use PHP allocator
13 r12: adapt for upstream changes for new zic
14 r11: use canonical names to avoid more case sensitivity issues
15      round lat/long from zone.tab towards zero per builtin db
16 r10: make timezone case insensitive
17 r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
18 r8: fix compile error without --with-system-tzdata configured
19 r7: improve check for valid timezone id to exclude directories
20 r6: fix fd leak in r5, fix country code/BC flag use in
21     timezone_identifiers_list() using system db,
22     fix use of PECL timezonedb to override system db,
23 r5: reverts addition of "System/Localtime" fake tzname.
24     updated for 5.3.0, parses zone.tab to pick up mapping between
25     timezone name, country code and long/lat coords
26 r4: added "System/Localtime" tzname which uses /etc/localtime
27 r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
28 r2: add filesystem trawl to set up name alias index
29 r1: initial revision
30
31 diff -up php-7.2.16RC1/ext/date/config0.m4.systzdata php-7.2.16RC1/ext/date/config0.m4
32 --- php-7.2.16RC1/ext/date/config0.m4.systzdata 2019-02-19 11:22:22.223741585 +0100
33 +++ php-7.2.16RC1/ext/date/config0.m4   2019-02-19 11:23:05.089111556 +0100
34 @@ -10,6 +10,19 @@ io.h
35  dnl Check for strtoll, atoll
36  AC_CHECK_FUNCS(strtoll atoll)
37  
38 +PHP_ARG_WITH(system-tzdata, for use of system timezone data,
39 +[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
40 +no, no)
41 +
42 +if test "$PHP_SYSTEM_TZDATA" != "no"; then
43 +   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
44 +
45 +   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
46 +      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
47 +                         [Define for location of system timezone data])
48 +   fi
49 +fi
50 +
51  PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
52  timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
53                   lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
54 diff -up php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata php-7.2.16RC1/ext/date/lib/parse_tz.c
55 --- php-7.2.16RC1/ext/date/lib/parse_tz.c.systzdata     2019-02-19 11:13:22.000000000 +0100
56 +++ php-7.2.16RC1/ext/date/lib/parse_tz.c       2019-02-19 11:19:40.245313535 +0100
57 @@ -25,8 +25,21 @@
58  #include "timelib.h"
59  #include "timelib_private.h"
60  
61 +#ifdef HAVE_SYSTEM_TZDATA
62 +#include <sys/mman.h>
63 +#include <sys/stat.h>
64 +#include <limits.h>
65 +#include <fcntl.h>
66 +#include <unistd.h>
67 +
68 +#include "php_scandir.h"
69 +
70 +#else
71  #define TIMELIB_SUPPORTS_V2DATA
72  #include "timezonedb.h"
73 +#endif
74 +
75 +#include <ctype.h>
76  
77  #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
78  # if defined(__LITTLE_ENDIAN__)
79 @@ -67,6 +80,11 @@ static int read_php_preamble(const unsig
80  {
81         uint32_t version;
82  
83 +       if (memcmp(*tzf, "TZif", 4) == 0) {
84 +               *tzf += 20;
85 +               return 0;
86 +       }
87 +
88         /* read ID */
89         version = (*tzf)[3] - '0';
90         *tzf += 4;
91 @@ -374,7 +392,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
92         }
93  }
94  
95 -static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
96 +#ifdef HAVE_SYSTEM_TZDATA
97 +
98 +#ifdef HAVE_SYSTEM_TZDATA_PREFIX
99 +#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
100 +#else
101 +#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
102 +#endif
103 +
104 +/* System timezone database pointer. */
105 +static const timelib_tzdb *timezonedb_system;
106 +
107 +/* Hash table entry for the cache of the zone.tab mapping table. */
108 +struct location_info {
109 +        char code[2];
110 +        double latitude, longitude;
111 +        char name[64];
112 +        char *comment;
113 +        struct location_info *next;
114 +};
115 +
116 +/* Cache of zone.tab. */
117 +static struct location_info **system_location_table;
118 +
119 +/* Size of the zone.tab hash table; a random-ish prime big enough to
120 + * prevent too many collisions. */
121 +#define LOCINFO_HASH_SIZE (1021)
122 +
123 +/* Compute a case insensitive hash of str */
124 +static uint32_t tz_hash(const char *str)
125 +{
126 +    const unsigned char *p = (const unsigned char *)str;
127 +    uint32_t hash = 5381;
128 +    int c;
129 +
130 +    while ((c = tolower(*p++)) != '\0') {
131 +        hash = (hash << 5) ^ hash ^ c;
132 +    }
133 +
134 +    return hash % LOCINFO_HASH_SIZE;
135 +}
136 +
137 +/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
138 + * parsed string on success, or NULL on parse error.  On success,
139 + * writes the parsed number to *result. */
140 +static char *parse_iso6709(char *p, double *result)
141 +{
142 +    double v, sign;
143 +    char *pend;
144 +    size_t len;
145 +
146 +    if (*p == '+')
147 +        sign = 1.0;
148 +    else if (*p == '-')
149 +        sign = -1.0;
150 +    else
151 +        return NULL;
152 +
153 +    p++;
154 +    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
155 +        ;;
156 +
157 +    /* Annoying encoding used by zone.tab has no decimal point, so use
158 +     * the length to determine the format:
159 +     * 
160 +     * 4 = DDMM
161 +     * 5 = DDDMM
162 +     * 6 = DDMMSS
163 +     * 7 = DDDMMSS
164 +     */
165 +    len = pend - p;
166 +    if (len < 4 || len > 7) {
167 +        return NULL;
168 +    }
169 +
170 +    /* p => [D]DD */
171 +    v = (p[0] - '0') * 10.0 + (p[1] - '0');
172 +    p += 2;
173 +    if (len == 5 || len == 7)
174 +        v = v * 10.0 + (*p++ - '0');
175 +    /* p => MM[SS] */
176 +    v += (10.0 * (p[0] - '0')
177 +          + p[1] - '0') / 60.0;
178 +    p += 2;
179 +    /* p => [SS] */
180 +    if (len > 5) {
181 +        v += (10.0 * (p[0] - '0')
182 +              + p[1] - '0') / 3600.0;
183 +        p += 2;
184 +    }
185 +
186 +    /* Round to five decimal place, not because it's a good idea,
187 +     * but, because the builtin data uses rounded data, so, match
188 +     * that. */
189 +    *result = trunc(v * sign * 100000.0) / 100000.0;
190 +
191 +    return p;
192 +}
193 +
194 +/* This function parses the zone.tab file to build up the mapping of
195 + * timezone to country code and geographic location, and returns a
196 + * hash table.  The hash table is indexed by the function:
197 + *
198 + *   tz_hash(timezone-name)
199 + */
200 +static struct location_info **create_location_table(void)
201 +{
202 +    struct location_info **li, *i;
203 +    char zone_tab[PATH_MAX];
204 +    char line[512];
205 +    FILE *fp;
206 +
207 +    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
208 +
209 +    fp = fopen(zone_tab, "r");
210 +    if (!fp) {
211 +        return NULL;
212 +    }
213 +
214 +    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
215 +
216 +    while (fgets(line, sizeof line, fp)) {
217 +        char *p = line, *code, *name, *comment;
218 +        uint32_t hash;
219 +        double latitude, longitude;
220 +
221 +        while (isspace(*p))
222 +            p++;
223 +
224 +        if (*p == '#' || *p == '\0' || *p == '\n')
225 +            continue;
226 +        
227 +        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
228 +            continue;
229 +        
230 +        /* code => AA */
231 +        code = p;
232 +        p[2] = 0;
233 +        p += 3;
234 +
235 +        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
236 +        p = parse_iso6709(p, &latitude);
237 +        if (!p) {
238 +            continue;
239 +        }
240 +        p = parse_iso6709(p, &longitude);
241 +        if (!p) {
242 +            continue;
243 +        }
244 +
245 +        if (!p || *p != '\t') {
246 +            continue;
247 +        }
248 +
249 +        /* name = string */
250 +        name = ++p;
251 +        while (*p != '\t' && *p && *p != '\n')
252 +            p++;
253 +
254 +        *p++ = '\0';
255 +
256 +        /* comment = string */
257 +        comment = p;
258 +        while (*p != '\t' && *p && *p != '\n')
259 +            p++;
260 +
261 +        if (*p == '\n' || *p == '\t')
262 +            *p = '\0';
263 +        
264 +        hash = tz_hash(name);
265 +        i = malloc(sizeof *i);
266 +        memcpy(i->code, code, 2);
267 +        strncpy(i->name, name, sizeof i->name);
268 +        i->comment = strdup(comment);
269 +        i->longitude = longitude;
270 +        i->latitude = latitude;
271 +        i->next = li[hash];
272 +        li[hash] = i;
273 +        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
274 +    }
275 +
276 +    fclose(fp);
277 +
278 +    return li;
279 +}
280 +
281 +/* Return location info from hash table, using given timezone name.
282 + * Returns NULL if the name could not be found. */
283 +const struct location_info *find_zone_info(struct location_info **li, 
284 +                                           const char *name)
285 +{
286 +    uint32_t hash = tz_hash(name);
287 +    const struct location_info *l;
288 +
289 +    if (!li) {
290 +        return NULL;
291 +    }
292 +
293 +    for (l = li[hash]; l; l = l->next) {
294 +        if (timelib_strcasecmp(l->name, name) == 0)
295 +            return l;
296 +    }
297 +
298 +    return NULL;
299 +}    
300 +
301 +/* Filter out some non-tzdata files and the posix/right databases, if
302 + * present. */
303 +static int index_filter(const struct dirent *ent)
304 +{
305 +       return strcmp(ent->d_name, ".") != 0
306 +               && strcmp(ent->d_name, "..") != 0
307 +               && strcmp(ent->d_name, "posix") != 0
308 +               && strcmp(ent->d_name, "posixrules") != 0
309 +               && strcmp(ent->d_name, "right") != 0
310 +               && strstr(ent->d_name, ".list") == NULL
311 +               && strstr(ent->d_name, ".tab") == NULL;
312 +}
313 +
314 +static int sysdbcmp(const void *first, const void *second)
315 +{
316 +        const timelib_tzdb_index_entry *alpha = first, *beta = second;
317 +
318 +        return timelib_strcasecmp(alpha->id, beta->id);
319 +}
320 +
321 +
322 +/* Create the zone identifier index by trawling the filesystem. */
323 +static void create_zone_index(timelib_tzdb *db)
324 +{
325 +       size_t dirstack_size,  dirstack_top;
326 +       size_t index_size, index_next;
327 +       timelib_tzdb_index_entry *db_index;
328 +       char **dirstack;
329 +
330 +       /* LIFO stack to hold directory entries to scan; each slot is a
331 +        * directory name relative to the zoneinfo prefix. */
332 +       dirstack_size = 32;
333 +       dirstack = malloc(dirstack_size * sizeof *dirstack);
334 +       dirstack_top = 1;
335 +       dirstack[0] = strdup("");
336 +       
337 +       /* Index array. */
338 +       index_size = 64;
339 +       db_index = malloc(index_size * sizeof *db_index);
340 +       index_next = 0;
341 +
342 +       do {
343 +               struct dirent **ents;
344 +               char name[PATH_MAX], *top;
345 +               int count;
346 +
347 +               /* Pop the top stack entry, and iterate through its contents. */
348 +               top = dirstack[--dirstack_top];
349 +               snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
350 +
351 +               count = php_scandir(name, &ents, index_filter, php_alphasort);
352 +
353 +               while (count > 0) {
354 +                       struct stat st;
355 +                       const char *leaf = ents[count - 1]->d_name;
356 +
357 +                       snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
358 +                                top, leaf);
359 +                       
360 +                       if (strlen(name) && stat(name, &st) == 0) {
361 +                               /* Name, relative to the zoneinfo prefix. */
362 +                               const char *root = top;
363 +
364 +                               if (root[0] == '/') root++;
365 +
366 +                               snprintf(name, sizeof name, "%s%s%s", root, 
367 +                                        *root ? "/": "", leaf);
368 +
369 +                               if (S_ISDIR(st.st_mode)) {
370 +                                       if (dirstack_top == dirstack_size) {
371 +                                               dirstack_size *= 2;
372 +                                               dirstack = realloc(dirstack, 
373 +                                                                  dirstack_size * sizeof *dirstack);
374 +                                       }
375 +                                       dirstack[dirstack_top++] = strdup(name);
376 +                               }
377 +                               else {
378 +                                       if (index_next == index_size) {
379 +                                               index_size *= 2;
380 +                                               db_index = realloc(db_index,
381 +                                                                  index_size * sizeof *db_index);
382 +                                       }
383 +
384 +                                       db_index[index_next++].id = strdup(name);
385 +                               }
386 +                       }
387 +
388 +                       free(ents[--count]);
389 +               }
390 +               
391 +               if (count != -1) free(ents);
392 +               free(top);
393 +       } while (dirstack_top);
394 +
395 +        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
396 +
397 +       db->index = db_index;
398 +       db->index_size = index_next;
399 +
400 +       free(dirstack);
401 +}
402 +
403 +#define FAKE_HEADER "1234\0??\1??"
404 +#define FAKE_UTC_POS (7 - 4)
405 +
406 +/* Create a fake data segment for database 'sysdb'. */
407 +static void fake_data_segment(timelib_tzdb *sysdb,
408 +                              struct location_info **info)
409 +{
410 +        size_t n;
411 +        char *data, *p;
412 +        
413 +        data = malloc(3 * sysdb->index_size + 7);
414 +
415 +        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
416 +
417 +        for (n = 0; n < sysdb->index_size; n++) {
418 +                const struct location_info *li;
419 +                timelib_tzdb_index_entry *ent;
420 +
421 +                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
422 +
423 +                /* Lookup the timezone name in the hash table. */
424 +                if (strcmp(ent->id, "UTC") == 0) {
425 +                        ent->pos = FAKE_UTC_POS;
426 +                        continue;
427 +                }
428 +
429 +                li = find_zone_info(info, ent->id);
430 +                if (li) {
431 +                        /* If found, append the BC byte and the
432 +                         * country code; set the position for this
433 +                         * section of timezone data.  */
434 +                        ent->pos = (p - data) - 4;
435 +                        *p++ = '\1';
436 +                        *p++ = li->code[0];
437 +                        *p++ = li->code[1];
438 +                }
439 +                else {
440 +                        /* If not found, the timezone data can
441 +                         * point at the header. */
442 +                        ent->pos = 0;
443 +                }
444 +        }
445 +        
446 +        sysdb->data = (unsigned char *)data;
447 +}
448 +
449 +/* Returns true if the passed-in stat structure describes a
450 + * probably-valid timezone file. */
451 +static int is_valid_tzfile(const struct stat *st, int fd)
452 +{
453 +       if (fd) {
454 +               char buf[20];
455 +               if (read(fd, buf, 20)!=20) {
456 +                       return 0;
457 +               }
458 +               lseek(fd, SEEK_SET, 0);
459 +               if (memcmp(buf, "TZif", 4)) {
460 +                       return 0;
461 +               }
462 +       }
463 +       return S_ISREG(st->st_mode) && st->st_size > 20;
464 +}
465 +
466 +/* To allow timezone names to be used case-insensitively, find the
467 + * canonical name for this timezone, if possible. */
468 +static const char *canonical_tzname(const char *timezone)
469 +{
470 +    if (timezonedb_system) {
471 +        timelib_tzdb_index_entry *ent, lookup;
472 +
473 +        lookup.id = (char *)timezone;
474 +
475 +        ent = bsearch(&lookup, timezonedb_system->index,
476 +                      timezonedb_system->index_size, sizeof lookup,
477 +                      sysdbcmp);
478 +        if (ent) {
479 +            return ent->id;
480 +        }
481 +    }
482 +
483 +    return timezone;
484 +}
485 +
486 +/* Return the mmap()ed tzfile if found, else NULL.  On success, the
487 + * length of the mapped data is placed in *length. */
488 +static char *map_tzfile(const char *timezone, size_t *length)
489 +{
490 +       char fname[PATH_MAX];
491 +       struct stat st;
492 +       char *p;
493 +       int fd;
494 +       
495 +       if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
496 +               return NULL;
497 +       }
498 +
499 +       snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
500 +
501 +       fd = open(fname, O_RDONLY);
502 +       if (fd == -1) {
503 +               return NULL;
504 +       } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
505 +               close(fd);
506 +               return NULL;
507 +       }
508 +
509 +       *length = st.st_size;
510 +       p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
511 +       close(fd);
512 +       
513 +       return p != MAP_FAILED ? p : NULL;
514 +}
515 +
516 +#endif
517 +
518 +static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
519  {
520         int left = 0, right = tzdb->index_size - 1;
521  
522 @@ -400,9 +840,48 @@ static int seek_to_tz_position(const uns
523         return 0;
524  }
525  
526 +static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
527 +                              char **map, size_t *maplen,
528 +                              const timelib_tzdb *tzdb)
529 +{
530 +#ifdef HAVE_SYSTEM_TZDATA
531 +       if (tzdb == timezonedb_system) {
532 +               char *orig;
533 +
534 +               orig = map_tzfile(timezone, maplen);
535 +               if (orig == NULL) {
536 +                       return 0;
537 +               }
538 +
539 +               (*tzf) = (unsigned char *)orig;
540 +               *map = orig;
541 +        return 1;
542 +       }
543 +       else
544 +#endif
545 +       {
546 +               return inmem_seek_to_tz_position(tzf, timezone, tzdb);
547 +       }
548 +}
549 +
550  const timelib_tzdb *timelib_builtin_db(void)
551  {
552 +#ifdef HAVE_SYSTEM_TZDATA
553 +       if (timezonedb_system == NULL) {
554 +               timelib_tzdb *tmp = malloc(sizeof *tmp);
555 +
556 +               tmp->version = "0.system";
557 +               tmp->data = NULL;
558 +               create_zone_index(tmp);
559 +               system_location_table = create_location_table();
560 +               fake_data_segment(tmp, system_location_table);
561 +               timezonedb_system = tmp;
562 +       }
563 +
564 +       return timezonedb_system;
565 +#else
566         return &timezonedb_builtin;
567 +#endif
568  }
569  
570  const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
571 @@ -414,7 +893,30 @@ const timelib_tzdb_index_entry *timelib_
572  int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
573  {
574         const unsigned char *tzf;
575 -       return (seek_to_tz_position(&tzf, timezone, tzdb));
576 +
577 +#ifdef HAVE_SYSTEM_TZDATA
578 +       if (tzdb == timezonedb_system) {
579 +               char fname[PATH_MAX];
580 +               struct stat st;
581 +
582 +               if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
583 +                       return 0;
584 +               }
585 +
586 +               if (system_location_table) {
587 +                       if (find_zone_info(system_location_table, timezone) != NULL) {
588 +                               /* found in cache */
589 +                               return 1;
590 +                       }
591 +               }
592 +
593 +               snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
594 +
595 +               return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
596 +       }
597 +#endif
598 +
599 +       return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
600  }
601  
602  static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
603 @@ -456,12 +958,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
604  timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb, int *error_code)
605  {
606         const unsigned char *tzf;
607 +       char *memmap = NULL;
608 +       size_t maplen;
609         timelib_tzinfo *tmp;
610         int version;
611         int transitions_result, types_result;
612         unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
613  
614 -       if (seek_to_tz_position(&tzf, timezone, tzdb)) {
615 +       if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
616                 tmp = timelib_tzinfo_ctor(timezone);
617  
618                 version = read_preamble(&tzf, tmp, &type);
619 @@ -484,6 +988,29 @@ timelib_tzinfo *timelib_parse_tzfile(cha
620                         timelib_tzinfo_dtor(tmp);
621                         return NULL;
622                 }
623 +
624 +#ifdef HAVE_SYSTEM_TZDATA
625 +               if (memmap) {
626 +                       const struct location_info *li;
627 +
628 +                       /* TZif-style - grok the location info from the system database,
629 +                        * if possible. */
630 +
631 +                       if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
632 +                               tmp->location.comments = timelib_strdup(li->comment);
633 +                               strncpy(tmp->location.country_code, li->code, 2);
634 +                               tmp->location.longitude = li->longitude;
635 +                               tmp->location.latitude = li->latitude;
636 +                               tmp->bc = 1;
637 +                       }
638 +                       else {
639 +                               set_default_location_and_comments(&tzf, tmp);
640 +                       }
641 +
642 +                       /* Now done with the mmap segment - discard it. */
643 +                       munmap(memmap, maplen);
644 +               } else {
645 +#endif
646                 if (version == 2 || version == 3) {
647                         if (!skip_64bit_preamble(&tzf, tmp)) {
648                                 /* 64 bit preamble is not in place */
649 @@ -501,6 +1028,9 @@ timelib_tzinfo *timelib_parse_tzfile(cha
650                 } else {
651                         set_default_location_and_comments(&tzf, tmp);
652                 }
653 +#ifdef HAVE_SYSTEM_TZDATA
654 +               }
655 +#endif
656         } else {
657                 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
658                 tmp = NULL;