Skip to content

Commit 80ded52

Browse files
authored
Fix off-by-one error in wasmfs getcwd (#23771)
When the buffer is exactly the correct size wasmfs was incorrectly erroring out.
1 parent 2e14c6d commit 80ded52

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

system/lib/wasmfs/syscalls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ int __syscall_getcwd(intptr_t buf, size_t size) {
776776

777777
// Check if the size argument is less than the length of the absolute
778778
// pathname of the working directory, including null terminator.
779-
if (len >= size) {
779+
if (len > size) {
780780
return -ERANGE;
781781
}
782782

test/wasmfs/wasmfs_chdir.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@
1616
#include <unistd.h>
1717

1818
int main() {
19+
char cwd[100];
20+
1921
// Set up test directories.
2022
assert(mkdir("working", 0777) != -1);
2123
assert(mkdir("/working/test", 0777) != -1);
2224

23-
// Try to print the root directory.
24-
char cwd[100];
25+
// Try to pass a size of 0.
26+
errno = 0;
27+
getcwd(cwd, 0);
28+
printf("Errno: %s\n", strerror(errno));
29+
assert(errno == EINVAL);
30+
31+
// Try to write to a buffer of size 1.
32+
errno = 0;
33+
getcwd(cwd, 1);
34+
assert(errno == ERANGE);
35+
36+
// Try to print the root directory, using size 2
2537
char* ret;
26-
ret = getcwd(cwd, sizeof(cwd));
38+
errno = 0;
39+
ret = getcwd(cwd, 2);
2740
assert(ret == cwd);
2841
printf("Current working dir: %s\n", cwd);
2942

@@ -86,16 +99,5 @@ int main() {
8699
assert(ret == cwd);
87100
printf("Current working dir is still: %s\n", cwd);
88101

89-
// Try to pass a size of 0.
90-
errno = 0;
91-
getcwd(cwd, 0);
92-
printf("Errno: %s\n", strerror(errno));
93-
assert(errno == EINVAL);
94-
95-
// Try to write to a buffer of size 1.
96-
errno = 0;
97-
char smallBuffer[1];
98-
getcwd(smallBuffer, 1);
99-
assert(errno == ERANGE);
100102
return 0;
101103
}

test/wasmfs/wasmfs_chdir.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Errno: Invalid argument
12
Current working dir: /
23
Current working dir: /working
34
Current working dir: /
@@ -11,4 +12,3 @@ Current working dir is still: /working
1112
Current working dir: /dev
1213
Errno: Not a directory
1314
Current working dir is still: /dev
14-
Errno: Invalid argument

0 commit comments

Comments
 (0)