8000 extmod/vfs_posix: Fix getcwd() on non-root VFS. · micropython/micropython@0d5ead9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d5ead9

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 bf7649c commit 0d5ead9

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

extmod/vfs_posix.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ STATIC mp_obj_t vfs_posix_getcwd(mp_obj_t self_in) {
173173
if (ret == NULL) {
174174
mp_raise_OSError(errno);
175175
}
176-
ret += self->root_len;
176+
if (self->root_len > 0) {
177+
ret += self->root_len - 1;
178+
}
177179
return mp_obj_new_str(ret, strlen(ret));
178180
}
179181
STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd);

0 commit comments

Comments
 (0)
0