Skip to content

Commit f0c223d

Browse files
committed
Fix utime/stat for time_t > 2^32
See: #17393
1 parent 8c047b1 commit f0c223d

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

src/library_syscall.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ var SyscallsLibrary = {
6464
{{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i64') }}};
6565
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
6666
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
67-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0', 'i32') }}};
67+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0 ', 'i64') }}};
6868
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
69-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i32') }}};
69+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i64') }}};
7070
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
71-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i32') }}};
72-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
71+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i64') }}};
72+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i64') }}};
7373
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
7474
return 0;
7575
},
@@ -955,6 +955,7 @@ var SyscallsLibrary = {
955955
return 0;
956956
},
957957
__syscall_utimensat__sig: 'iippi',
958+
__syscall_utimensat__deps: ['$readI53FromU64'],
958959
__syscall_utimensat: function(dirfd, path, times, flags) {
959960
path = SYSCALLS.getStr(path);
960961
#if ASSERTIONS
@@ -965,11 +966,11 @@ var SyscallsLibrary = {
965966
var atime = Date.now();
966967
var mtime = atime;
967968
} else {
968-
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
969+
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
969970
var nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
970971
atime = (seconds*1000) + (nanoseconds/(1000*1000));
971972
times += {{{ C_STRUCTS.timespec.__size__ }}};
972-
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
973+
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
973974
nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
974975
mtime = (seconds*1000) + (nanoseconds/(1000*1000));
975976
}

src/parseTools.js

+4
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
407407
}
408408
}
409409

410+
410411
const offset = calcFastOffset(ptr, pos, noNeedFirst);
412+
if (type == 'i53') {
413+
return 'readI53FromU64(' + offset + ')';
414+
}
411415

412416
const slab = getHeapForType(type);
413417
let ret = slab + '[' + getHeapOffset(offset, type) + ']';

system/lib/libc/musl/arch/emscripten/bits/alltypes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ struct timeval { time_t tv_sec; suseconds_t tv_usec; };
309309
#endif
310310

311311
#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec)
312-
struct timespec { time_t tv_sec; long tv_nsec; };
312+
struct timespec { time_t tv_sec; int tv_nsec; };
313313
#define __DEFINED_struct_timespec
314314
#endif
315315

tests/stat/test_stat.c

+15-14
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ void create_file(const char *path, const char *buffer, int mode) {
3030
close(fd);
3131
}
3232

33+
#define TEST_TIME (1ll << 34)
3334
void setup() {
34-
struct utimbuf t = {1200000000, 1200000000};
35+
struct utimbuf t = {TEST_TIME, TEST_TIME};
3536

3637
mkdir("folder", 0777);
3738
create_file("folder/file", "abcdef", 0777);
@@ -51,7 +52,7 @@ void cleanup() {
5152
void test() {
5253
int err;
5354
struct stat s;
54-
struct utimbuf t = {1200000000, 1200000000};
55+
struct utimbuf t = {TEST_TIME, TEST_TIME};
5556

5657
// non-existent
5758
err = stat("does_not_exist", &s);
@@ -70,8 +71,8 @@ void test() {
7071
assert(s.st_rdev == 0);
7172
#endif
7273
assert(s.st_size);
73-
assert(s.st_atime == 1200000000);
74-
assert(s.st_mtime == 1200000000);
74+
assert(s.st_atime == TEST_TIME);
75+
assert(s.st_mtime == TEST_TIME);
7576
assert(s.st_ctime);
7677
#ifdef __EMSCRIPTEN__
7778
assert(s.st_blksize == 4096);
@@ -92,8 +93,8 @@ void test() {
9293
assert(s.st_nlink);
9394
assert(s.st_rdev == 0);
9495
assert(s.st_size == 6);
95-
assert(s.st_atime == 1200000000);
96-
assert(s.st_mtime == 1200000000);
96+
assert(s.st_atime == TEST_TIME);
97+
assert(s.st_mtime == TEST_TIME);
9798
assert(s.st_ctime);
9899
#ifdef __EMSCRIPTEN__
99100
assert(s.st_blksize == 4096);
@@ -110,8 +111,8 @@ void test() {
110111
assert(s.st_nlink);
111112
assert(s.st_rdev == 0);
112113
assert(s.st_size == 6);
113-
assert(s.st_atime == 1200000000);
114-
assert(s.st_mtime == 1200000000);
114+
assert(s.st_atime == TEST_TIME);
115+
assert(s.st_mtime == TEST_TIME);
115116
assert(s.st_ctime);
116117
#ifdef __EMSCRIPTEN__
117118
assert(s.st_blksize == 4096);
@@ -152,8 +153,8 @@ void test() {
152153
assert(s.st_nlink);
153154
assert(s.st_rdev == 0);
154155
assert(s.st_size == 6);
155-
assert(s.st_atime == 1200000000);
156-
assert(s.st_mtime == 1200000000);
156+
assert(s.st_atime == TEST_TIME);
157+
assert(s.st_mtime == TEST_TIME);
157158
assert(s.st_ctime);
158159
#ifdef __EMSCRIPTEN__
159160
assert(s.st_blksize == 4096);
@@ -170,8 +171,8 @@ void test() {
170171
assert(s.st_nlink);
171172
assert(s.st_rdev == 0);
172173
assert(s.st_size == 4); // strlen("file")
173-
assert(s.st_atime != 1200000000); // should NOT match the utime call we did for dir/file
174-
assert(s.st_mtime != 1200000000);
174+
assert(s.st_atime != TEST_TIME); // should NOT match the utime call we did for dir/file
175+
assert(s.st_mtime != TEST_TIME);
175176
assert(s.st_ctime);
176177
#ifdef __EMSCRIPTEN__
177178
assert(s.st_blksize == 4096);
@@ -183,11 +184,11 @@ void test() {
183184
utime("folder/subdir", &t);
184185
create_file("folder/subdir/file", "abcdef", 0777);
185186
err = stat("folder/subdir", &s);
186-
assert(s.st_mtime != 1200000000);
187+
assert(s.st_mtime != TEST_TIME);
187188
utime("folder/subdir", &t);
188189
unlink("folder/subdir/file");
189190
err = stat("folder/subdir", &s);
190-
assert(s.st_mtime != 1200000000);
191+
assert(s.st_mtime != TEST_TIME);
191192

192193
puts("success");
193194
}

0 commit comments

Comments
 (0)