Skip to content

Commit a0fbebc

Browse files
authored
[WasmFS] Fix return value of cwd syscall (emscripten-core#16061)
The syscall should return the buffer. This makes us behave the same as in linux and the old FS.
1 parent 40ea491 commit a0fbebc

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

system/lib/wasmfs/syscalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,9 @@ long __syscall_getcwd(long buf, long size) {
632632
// Return value is a null-terminated c string.
633633
strcpy((char*)buf, res);
634634

635-
return 0;
635+
return buf;
636636
}
637+
637638
__wasi_errno_t __wasi_fd_fdstat_get(__wasi_fd_t fd, __wasi_fdstat_t* stat) {
638639
// TODO: This is only partial implementation of __wasi_fd_fdstat_get. Enough
639640
// to get __wasi_fd_is_valid working.

tests/wasmfs/wasmfs_chdir.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,68 @@ int main() {
2222

2323
// Try to print the root directory.
2424
char cwd[100];
25-
getcwd(cwd, sizeof(cwd));
25+
char* ret;
26+
ret = getcwd(cwd, sizeof(cwd));
27+
assert(ret == cwd);
2628
printf("Current working dir: %s\n", cwd);
2729

2830
// Try to switch to /working.
2931
chdir("working");
30-
getcwd(cwd, sizeof(cwd));
32+
ret = getcwd(cwd, sizeof(cwd));
33+
assert(ret == cwd);
3134
printf("Current working dir: %s\n", cwd);
3235

3336
// Try to switch back to the root directory.
3437
chdir("/");
35-
getcwd(cwd, sizeof(cwd));
38+
ret = getcwd(cwd, sizeof(cwd));
39+
assert(ret == cwd);
3640
printf("Current working dir: %s\n", cwd);
3741

3842
// Try to switch to a subdirectory of working.
3943
chdir("/working/test");
40-
getcwd(cwd, sizeof(cwd));
44+
ret = getcwd(cwd, sizeof(cwd));
45+
assert(ret == cwd);
4146
printf("Current working dir: %s\n", cwd);
4247

4348
// Try to switch to a non-existent relative path from subdirectory.
4449
// Changing to working in /working/test should fail.
4550
chdir("working");
4651
printf("Errno: %s\n", strerror(errno));
4752
assert(errno == ENOENT);
48-
getcwd(cwd, sizeof(cwd));
53+
ret = getcwd(cwd, sizeof(cwd));
54+
assert(ret == cwd);
4955
printf("Current working dir is still: %s\n", cwd);
5056

5157
// Try to switch back to absolute path /working.
5258
errno = 0;
5359
chdir("/working");
5460
printf("Errno: %s\n", strerror(errno));
5561
assert(errno == 0);
56-
getcwd(cwd, sizeof(cwd));
62+
ret = getcwd(cwd, sizeof(cwd));
63+
assert(ret == cwd);
5764
printf("Current working dir is now: %s\n", cwd);
5865

5966
// Try to switch to a non-existent absolute path.
6067
errno = 0;
6168
chdir("/foobar");
6269
printf("Errno: %s\n", strerror(errno));
6370
assert(errno == ENOENT);
64-
getcwd(cwd, sizeof(cwd));
71+
ret = getcwd(cwd, sizeof(cwd));
72+
assert(ret == cwd);
6573
printf("Current working dir is still: %s\n", cwd);
6674

6775
// Try to switch to /dev.
6876
chdir("/dev");
69-
getcwd(cwd, sizeof(cwd));
77+
ret = getcwd(cwd, sizeof(cwd));
78+
assert(ret == cwd);
7079
printf("Current working dir: %s\n", cwd);
7180

7281
// Try to change cwd to a file.
7382
chdir("/dev/stdout");
7483
printf("Errno: %s\n", strerror(errno));
7584
assert(errno == ENOTDIR);
76-
getcwd(cwd, sizeof(cwd));
85+
ret = getcwd(cwd, sizeof(cwd));
86+
assert(ret == cwd);
7787
printf("Current working dir is still: %s\n", cwd);
7888

7989
// Try to pass a size of 0.

0 commit comments

Comments
 (0)