Skip to content

Commit 3ce924f

Browse files
authored
Fix mktime/timegm for time_t > MAX_INT32 (#19700)
Fixes: #19694
1 parent 8fba427 commit 3ce924f

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

src/library.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ mergeInto(LibraryManager.library, {
476476
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_mon, 'date.getMonth()', 'i32') }}};
477477
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_year, 'date.getYear()', 'i32') }}};
478478

479-
return (date.getTime() / 1000)|0;
479+
return {{{ makeReturn64('date.getTime() / 1000') }}};
480480
},
481481

482482
_gmtime_js__deps: ['$readI53FromI64'].concat(i53ConversionDeps),
@@ -510,7 +510,7 @@ mergeInto(LibraryManager.library, {
510510
var yday = ((date.getTime() - start) / (1000 * 60 * 60 * 24))|0;
511511
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_yday, 'yday', 'i32') }}};
512512

513-
return (date.getTime() / 1000)|0;
513+
return {{{ makeReturn64('date.getTime() / 1000') }}};
514514
},
515515

516516
_localtime_js__deps: ['$readI53FromI64', '$ydayFromDate'].concat(i53ConversionDeps),

src/library_sigs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ sigs = {
353353
_emval_typeof__sig: 'pp',
354354
_gmtime_js__sig: 'vjp',
355355
_localtime_js__sig: 'vjp',
356-
_mktime_js__sig: 'ip',
356+
_mktime_js__sig: 'jp',
357357
_mmap_js__sig: 'ipiiijpp',
358358
_msync_js__sig: 'ippiiij',
359359
_munmap_js__sig: 'ippiiij',
360360
_setitimer_js__sig: 'iid',
361-
_timegm_js__sig: 'ip',
361+
_timegm_js__sig: 'jp',
362362
_tzset_js__sig: 'vppp',
363363
_wasmfs_copy_preloaded_file_data__sig: 'vip',
364364
_wasmfs_create_fetch_backend_js__sig: 'vp',

system/lib/libc/emscripten_internal.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ void emscripten_memset_big(void* ptr, char value, size_t n);
3535

3636
void emscripten_notify_memory_growth(size_t memory_index);
3737

38-
// Declare these functions `int` rather than time_t to avoid int64 at the wasm
39-
// boundary (avoids 64-bit complexity at the boundary when WASM_BIGINT is
40-
// missing).
41-
// TODO(sbc): Covert back to `time_t` before 2038 ...
42-
int _timegm_js(struct tm* tm);
43-
int _mktime_js(struct tm* tm);
38+
time_t _timegm_js(struct tm* tm);
39+
time_t _mktime_js(struct tm* tm);
4440
void _localtime_js(time_t t, struct tm* __restrict__ tm);
4541
void _gmtime_js(time_t t, struct tm* __restrict__ tm);
4642

test/core/test_time.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include <assert.h>
1111
#include <math.h>
1212

13-
void
14-
check_gmtime_localtime(time_t time)
13+
void check_gmtime_localtime(time_t time)
1514
{
1615
char gmbuf[32], locbuf[32];
1716
const char fmt[] = "%Y-%m-%d %H:%M:%S";
@@ -46,7 +45,7 @@ int main() {
4645
tzset();
4746
printf("tzname[0] set: %d\n", strlen(tzname[0]) >= 3);
4847
printf("tzname[1] set: %d\n", strlen(tzname[1]) >= 3);
49-
48+
5049
// Verify gmtime() creates correct struct.
5150
tm_ptr = gmtime(&xmas2002);
5251
printf("sec: %d\n", tm_ptr->tm_sec);
@@ -60,7 +59,7 @@ int main() {
6059
printf("dst: %d\n", tm_ptr->tm_isdst);
6160
printf("off: %ld\n", (long)tm_ptr->tm_gmtoff);
6261
printf("zone: %s\n", tm_ptr->tm_zone);
63-
62+
6463
// Verify timegm() reverses gmtime; run through an entire year in half hours.
6564
int timegmOk = 1;
6665
for (int i = 0; i < 2*24*266; ++i) {
@@ -75,14 +74,14 @@ int main() {
7574
timegmOk = 0;
7675
}
7776
printf("timegm <-> gmtime: %d\n", timegmOk);
78-
77+
7978
// Verify gmtime_r() doesn't clobber static data.
8079
time_t t1 = 0;
8180
struct tm tm1;
8281
gmtime_r(&t1, &tm1);
8382
printf("old year still: %d\n", tm_ptr->tm_year);
8483
printf("new year: %d\n", tm1.tm_year);
85-
84+
8685
// Verify localtime() picks up timezone data.
8786
struct tm tm_winter, tm_summer;
8887
if (localtime_r(&xmas2002, &tm_winter) != &tm_winter) printf("localtime_r failed\n");
@@ -152,7 +151,7 @@ int main() {
152151
tm2.tm_hour != tm_local.tm_hour || tm2.tm_mday != tm_local.tm_mday ||
153152
tm2.tm_mon != tm_local.tm_mon || tm2.tm_year != tm_local.tm_year ||
154153
tm2.tm_wday != tm_local.tm_wday || tm2.tm_yday != tm_local.tm_yday);
155-
154+
156155
printf("mktime parameter is equivalent to localtime return: %d\n", mktimeOk);
157156

158157
// Verify that mktime is able to guess what the dst is. It might get it wrong
@@ -249,6 +248,7 @@ int main() {
249248
check_gmtime_localtime(-2147483649);
250249
check_gmtime_localtime(253402300799); // end of year 9999
251250
check_gmtime_localtime(-62135596800); // beginning of year 1
251+
check_gmtime_localtime(0x83d4d9a5); // some time in 2040 (time_t > MAX_INT32)
252252

253253
// check that localtime sets tm_yday correctly whenever the day rolls over (issue #17635)
254254
// prior to being fixed, tm_yday did not increment correctly at epoch time 1049061599 (2003-03-31 00:00:00) in CET time

test/core/test_time.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ time: -2147483648, gmtime: 1901-12-13 20:45:52
5858
time: -2147483649, gmtime: 1901-12-13 20:45:51
5959
time: 253402300799, gmtime: 9999-12-31 23:59:59
6060
time: -62135596800, gmtime: 1-01-01 00:00:00
61+
time: 2211764645, gmtime: 2040-02-02 03:04:05
6162
success

0 commit comments

Comments
 (0)