Skip to content

Commit 6c6523b

Browse files
committed
extmod/vfs_posix: Fix getcwd() on non-root VFS.
The unwritten API contract expected of a VFS.getcwd() by mp_vfs_getcwd() is that its return value should be either "" or "/" when the CWD is at the root of the VFS and otherwise start with a slash and not end with a slash. This was not correctly implemented in VfsPosix for instances with a non-empty root - the required leading slash, if any, was cut off because the root length includes a trailing slash. This would result in missing slashes in the middle of the return value of os.getcwd() or in uninitialized garbage from beyond a string's null terminator when the CWD was at the VFS root. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent ea52c81 commit 6c6523b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

extmod/vfs_posix.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ STATIC mp_obj_t vfs_posix_getcwd(mp_obj_t self_in) {
186186
if (ret == NULL) {
187187
mp_raise_OSError(errno);
188188
}
189-
ret += self->root_len;
189+
if (self->root_len > 0) {
190+
ret += self->root_len - 1;
191+
#ifdef _WIN32
192+
if (*ret == '\\') {
193+
*(char *)ret = '/';
194+
}
195+
#endif
196+
}
190197
return mp_obj_new_str(ret, strlen(ret));
191198
}
192199
STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd);

0 commit comments

Comments
 (0)