vfs: fix logic errors and leaks in vfs_open

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-02 14:09:41 +02:00
commit dd00e79b18
3 changed files with 69 additions and 22 deletions

View file

@ -14,14 +14,17 @@ static list_t fs_type_list = {0};
static char *path_trim(const char *name) static char *path_trim(const char *name)
{ {
if (!*name)
return NULL;
size_t len = strlen(name); size_t len = strlen(name);
char *ret = kzalloc(len, ALLOC_KERN); char *ret = kzalloc(len + 1, ALLOC_KERN);
const char *zeroptr = NULL; const char *zeroptr = name + len;
/* trim trailing slashes */ /* trim trailing slashes */
for (size_t i = len - 1; i >= 1; i++) { for (size_t i = len - 1; i >= 1; i--) {
if (name[i] == '/') if (name[i] == '/')
zeroptr = &name[i]; zeroptr = name + i;
else else
break; break;
} }
@ -30,7 +33,8 @@ static char *path_trim(const char *name)
char c; char c;
char last = 0; char last = 0;
size_t i = 0; size_t i = 0;
while (name < zeroptr && (c = *name++)) { const char *cursor = name;
while (cursor < zeroptr && (c = *cursor++)) {
if (last == c && c == '/') if (last == c && c == '/')
continue; continue;
@ -88,22 +92,46 @@ int vfs_open(const char *abs_path, inode_t **out)
char *path_optr = path_trim(abs_path); char *path_optr = path_trim(abs_path);
char *cur_path = path_optr; char *cur_path = path_optr;
list_item_t *item; list_item_t *item;
size_t ret = -ENOENT;
list_t visited; if (!cur_path)
list_create(&visited); return -ENOENT;
/* root case */ /* root case */
if (*cur_path == '\0') { if (*cur_path == '\0') {
inode_inc_ref(c_ino);
*out = c_ino; *out = c_ino;
return 0; ret = 0;
goto out_1;
} }
list_t visited;
list_create(&visited);
inode_inc_ref(c_ino);
list_insert(&visited, c_ino);
while (1) { while (1) {
while (*cur_path == '/') while (*cur_path == '/')
cur_path++; cur_path++;
if (*cur_path == '\0') {
*out = c_ino;
ret = 0;
/* Clean intermediate elements, keeping only target node reference alive */
item = visited.head;
while (item && item->next) {
inode_dec_ref(item->ptr);
item = item->next;
}
break;
}
int res = next_level(cur_path, &c_ino); int res = next_level(cur_path, &c_ino);
if (res < 0)
goto out;
cur_path += res; cur_path += res;
if (!c_ino) if (!c_ino)
@ -111,29 +139,36 @@ int vfs_open(const char *abs_path, inode_t **out)
list_insert(&visited, c_ino); list_insert(&visited, c_ino);
if (res < 0)
goto out;
/* found */ /* found */
if (res == 0) { if (*cur_path == '\0') {
*out = c_ino; *out = c_ino;
return 0; ret = 0;
}
cur_path += res; /* Clean intermediate elements, keeping only target node reference alive */
item = visited.head;
while (item && item->next) {
inode_dec_ref(item->ptr);
item = item->next;
}
break;
}
} }
out: out:
/* If we hit an error, ret remains -ENOENT and we unwind everything */
if (ret != 0) {
item = visited.head; item = visited.head;
while (item) { while (item) {
inode_t *inode = item->ptr; inode_t *inode = item->ptr;
inode_dec_ref(inode); inode_dec_ref(inode);
item = item->next; item = item->next;
} }
}
list_destroy(&visited);
out_1:
kfree(path_optr); kfree(path_optr);
return -ENOENT; return ret;
} }
void vfs_close(inode_t *inode) void vfs_close(inode_t *inode)

View file

@ -21,6 +21,7 @@ typedef struct _list_head {
} list_t; } list_t;
void list_create(list_t *list); void list_create(list_t *list);
void list_destroy(list_t * list);
list_item_t *list_insert(list_t *list, void *item); list_item_t *list_insert(list_t *list, void *item);
void list_remove(list_item_t *item); void list_remove(list_item_t *item);
void list_init(); void list_init();

View file

@ -12,6 +12,17 @@ void list_create(list_t *list)
list->tail = NULL; list->tail = NULL;
} }
void list_destroy(list_t *list)
{
list_item_t *item = list->head;
while (item) {
list_item_t *next = item->next;
slab_free(list_cache, item);
item = next;
}
}
list_item_t *list_insert(list_t *list, void *item) list_item_t *list_insert(list_t *list, void *item)
{ {
spinlock_acquire(&list->lock); spinlock_acquire(&list->lock);