Skip to content

Commit 223f52a

Browse files
committed
Fix utime/stat for time_t > 2^32
See: #17393
1 parent a3d8979 commit 223f52a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/library_syscall.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ 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, 'Math.floor(stat.atime.getTime() / 1000)', '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, 'Math.floor(stat.mtime.getTime() / 1000)', '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') }}};
71+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i64') }}};
7272
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
7373
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
7474
return 0;
@@ -954,6 +954,7 @@ var SyscallsLibrary = {
954954
return 0;
955955
},
956956
__syscall_utimensat__sig: 'iippi',
957+
__syscall_utimensat__deps: ['$readI53FromI64'],
957958
__syscall_utimensat: function(dirfd, path, times, flags) {
958959
path = SYSCALLS.getStr(path);
959960
#if ASSERTIONS
@@ -964,11 +965,11 @@ var SyscallsLibrary = {
964965
var atime = Date.now();
965966
var mtime = atime;
966967
} else {
967-
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
968+
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
968969
var nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
969970
atime = (seconds*1000) + (nanoseconds/(1000*1000));
970971
times += {{{ C_STRUCTS.timespec.__size__ }}};
971-
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
972+
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
972973
nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
973974
mtime = (seconds*1000) + (nanoseconds/(1000*1000));
974975
}

tests/stat/test_stat.c

+18-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,11 @@ 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+
printf("TEST_TIME: %llx\n", TEST_TIME);
75+
printf("s.st_atime: %llx\n", s.st_atime);
76+
printf("s.st_mtime: %llx\n", s.st_mtime);
77+
assert(s.st_atime == TEST_TIME);
78+
assert(s.st_mtime == TEST_TIME);
7579
assert(s.st_ctime);
7680
#ifdef __EMSCRIPTEN__
7781
assert(s.st_blksize == 4096);
@@ -92,8 +96,8 @@ void test() {
9296
assert(s.st_nlink);
9397
assert(s.st_rdev == 0);
9498
assert(s.st_size == 6);
95-
assert(s.st_atime == 1200000000);
96-
assert(s.st_mtime == 1200000000);
99+
assert(s.st_atime == TEST_TIME);
100+
assert(s.st_mtime == TEST_TIME);
97101
assert(s.st_ctime);
98102
#ifdef __EMSCRIPTEN__
99103
assert(s.st_blksize == 4096);
@@ -110,8 +114,8 @@ void test() {
110114
assert(s.st_nlink);
111115
assert(s.st_rdev == 0);
112116
assert(s.st_size == 6);
113-
assert(s.st_atime == 1200000000);
114-
assert(s.st_mtime == 1200000000);
117+
assert(s.st_atime == TEST_TIME);
118+
assert(s.st_mtime == TEST_TIME);
115119
assert(s.st_ctime);
116120
#ifdef __EMSCRIPTEN__
117121
assert(s.st_blksize == 4096);
@@ -152,8 +156,8 @@ void test() {
152156
assert(s.st_nlink);
153157
assert(s.st_rdev == 0);
154158
assert(s.st_size == 6);
155-
assert(s.st_atime == 1200000000);
156-
assert(s.st_mtime == 1200000000);
159+
assert(s.st_atime == TEST_TIME);
160+
assert(s.st_mtime == TEST_TIME);
157161
assert(s.st_ctime);
158162
#ifdef __EMSCRIPTEN__
159163
assert(s.st_blksize == 4096);
@@ -170,8 +174,8 @@ void test() {
170174
assert(s.st_nlink);
171175
assert(s.st_rdev == 0);
172176
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);
177+
assert(s.st_atime != TEST_TIME); // should NOT match the utime call we did for dir/file
178+
assert(s.st_mtime != TEST_TIME);
175179
assert(s.st_ctime);
176180
#ifdef __EMSCRIPTEN__
177181
assert(s.st_blksize == 4096);
@@ -183,11 +187,11 @@ void test() {
183187
utime("folder/subdir", &t);
184188
create_file("folder/subdir/file", "abcdef", 0777);
185189
err = stat("folder/subdir", &s);
186-
assert(s.st_mtime != 1200000000);
190+
assert(s.st_mtime != TEST_TIME);
187191
utime("folder/subdir", &t);
188192
unlink("folder/subdir/file");
189193
err = stat("folder/subdir", &s);
190-
assert(s.st_mtime != 1200000000);
194+
assert(s.st_mtime != TEST_TIME);
191195

192196
puts("success");
193197
}

0 commit comments

Comments
 (0)