Skip to content

Commit 9f2d47b

Browse files
committed
Avoid malloc dependency in tzset
1 parent d67fa99 commit 9f2d47b

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

src/generated_struct_info32.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
"TIOCGWINSZ": 21523,
402402
"TIOCSPGRP": 21520,
403403
"TIOCSWINSZ": 21524,
404+
"TZNAME_MAX": 6,
404405
"UUID_TYPE_DCE_RANDOM": 4,
405406
"UUID_VARIANT_DCE": 1,
406407
"W_OK": 2,

src/generated_struct_info64.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
"TIOCGWINSZ": 21523,
402402
"TIOCSPGRP": 21520,
403403
"TIOCSWINSZ": 21524,
404+
"TZNAME_MAX": 6,
404405
"UUID_TYPE_DCE_RANDOM": 4,
405406
"UUID_VARIANT_DCE": 1,
406407
"W_OK": 2,

src/library.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -604,19 +604,22 @@ addToLibrary({
604604
return ret;
605605
},
606606

607-
_tzset_js__deps: ['$stringToNewUTF8'],
607+
_tzset_js__deps: ['$stringToUTF8'],
608608
_tzset_js__internal: true,
609-
_tzset_js: (timezone, daylight, tzname) => {
609+
_tzset_js: (timezone, daylight, std_name, dst_name) => {
610610
// TODO: Use (malleable) environment variables instead of system settings.
611611
var currentYear = new Date().getFullYear();
612612
var winter = new Date(currentYear, 0, 1);
613613
var summer = new Date(currentYear, 6, 1);
614614
var winterOffset = winter.getTimezoneOffset();
615615
var summerOffset = summer.getTimezoneOffset();
616616

617-
// Local standard timezone offset. Local standard time is not adjusted for daylight savings.
618-
// This code uses the fact that getTimezoneOffset returns a greater value during Standard Time versus Daylight Saving Time (DST).
619-
// Thus it determines the expected output during Standard Time, and it compares whether the output of the given date the same (Standard) or less (DST).
617+
// Local standard timezone offset. Local standard time is not adjusted for
618+
// daylight savings. This code uses the fact that getTimezoneOffset returns
619+
// a greater value during Standard Time versus Daylight Saving Time (DST).
620+
// Thus it determines the expected output during Standard Time, and it
621+
// compares whether the output of the given date the same (Standard) or less
622+
// (DST).
620623
var stdTimezoneOffset = Math.max(winterOffset, summerOffset);
621624

622625
// timezone is specified as seconds west of UTC ("The external variable
@@ -634,15 +637,13 @@ addToLibrary({
634637
};
635638
var winterName = extractZone(winter);
636639
var summerName = extractZone(summer);
637-
var winterNamePtr = stringToNewUTF8(winterName);
638-
var summerNamePtr = stringToNewUTF8(summerName);
639640
if (summerOffset < winterOffset) {
640641
// Northern hemisphere
641-
{{{ makeSetValue('tzname', '0', 'winterNamePtr', POINTER_TYPE) }}};
642-
{{{ makeSetValue('tzname', POINTER_SIZE, 'summerNamePtr', POINTER_TYPE) }}};
642+
stringToUTF8(winterName, std_name, {{{ cDefs.TZNAME_MAX + 1 }}});
643+
stringToUTF8(summerName, dst_name, {{{ cDefs.TZNAME_MAX + 1 }}});
643644
} else {
644-
{{{ makeSetValue('tzname', '0', 'summerNamePtr', POINTER_TYPE) }}};
645-
{{{ makeSetValue('tzname', POINTER_SIZE, 'winterNamePtr', POINTER_TYPE) }}};
645+
stringToUTF8(winterName, dst_name, {{{ cDefs.TZNAME_MAX + 1 }}});
646+
stringToUTF8(summerName, std_name, {{{ cDefs.TZNAME_MAX + 1 }}});
646647
}
647648
},
648649

src/struct_info.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@
318318
"X_OK"
319319
]
320320
},
321+
{
322+
"file": "limits.h",
323+
"defines": [
324+
"TZNAME_MAX"
325+
]
326+
},
321327
{
322328
"file": "bits/errno.h",
323329
"defines": [

system/lib/libc/emscripten_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ time_t _mktime_js(struct tm* tm);
4444
void _localtime_js(time_t t, struct tm* __restrict__ tm);
4545
void _gmtime_js(time_t t, struct tm* __restrict__ tm);
4646

47-
void _tzset_js(long* timezone, int* daylight, char** tzname);
47+
void _tzset_js(long* timezone, int* daylight, char* std_name, char* dst_name);
4848

4949
const char* emscripten_pc_get_function(uintptr_t pc);
5050
const char* emscripten_pc_get_file(uintptr_t pc);

system/lib/libc/tzset.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
* University of Illinois/NCSA Open Source License. Both these licenses can be
55
* found in the LICENSE file.
66
*/
7+
#include <limits.h>
78
#include <time.h>
89
#include <stdbool.h>
910
#include <pthread.h>
1011

1112
#include "emscripten_internal.h"
1213

14+
static char std_name[TZNAME_MAX+1];
15+
static char dst_name[TZNAME_MAX+1];
16+
1317
weak void tzset() {
1418
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
1519
static _Atomic bool done_init = false;
1620
if (!done_init) {
1721
pthread_mutex_lock(&lock);
1822
if (!done_init) {
19-
_tzset_js(&timezone, &daylight, tzname);
23+
_tzset_js(&timezone, &daylight, std_name, dst_name);
24+
tzname[0] = std_name;
25+
tzname[1] = dst_name;
2026
done_init = true;
2127
}
2228
pthread_mutex_unlock(&lock);

0 commit comments

Comments
 (0)