From cd8d016f34681c481bc645e20090d3103d904382 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 20 Oct 2023 01:11:10 -0400 Subject: [PATCH] ext2: fix memory leak --- fs/ext2.c | 8 +++++--- fs/vfs.c | 13 ++++++++++++- lib/string.c | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index 7ab129e..1942f27 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -234,8 +234,9 @@ int ext2_open_file(struct fs *vfs, struct file *file, const char *path) char *strtok_last = NULL; char *token = strtok(path_copy, "/", &strtok_last); + struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); + void *oentry = entry; while (token) { - struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); if (!entry) { kfree(inode); kfree(path_copy); @@ -260,14 +261,14 @@ int ext2_open_file(struct fs *vfs, struct file *file, const char *path) } } -found: if (!entry || !entry->inode) { kfree(inode); kfree(path_copy); - kfree(entry); + kfree(oentry); return -ENOENT; } +found: ext2_read_inode(fs, inode, entry->inode); token = strtok(NULL, "/", &strtok_last); @@ -279,6 +280,7 @@ found: file->size = inode->size; file->type = inode_to_ftype[inode->mode >> 12]; file->inode_num = inode_no; + kfree(oentry); /* check for long filesize */ if (fs->sb.feature_ro_compat & EXT2_FLAG_LONG_FILESIZE) diff --git a/fs/vfs.c b/fs/vfs.c index 3f371fe..82434af 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -37,7 +37,7 @@ int open(const char *pathname, int flags) int result = rootfs->ops.open(rootfs, &fd->file, pathname); if (result < 0) { - kprintf(LOG_ERROR "Failed to open %s\n", pathname); + kprintf(LOG_ERROR "Failed to open %s: %s\n", pathname, strerror(-result)); return result; } else if (result == VFSE_IS_BDEV) { /* TODO: continue search into mounted filesystems */ @@ -106,6 +106,9 @@ DIR *opendir(const char *name) struct dirent *readdir(DIR *dir) { + if(!dir) + return NULL; + struct fs *fs = dir->fs; struct file *file = dir->file; @@ -154,5 +157,13 @@ void vfs_init(const char *rootdev_name) /* init file descriptor slab */ fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0); + + DIR *dir = opendir("test/lol"); + struct dirent *dirent; + + kprintf("Root directory contents:\n"); + while ((dirent = readdir(dir))) { + kprintf(LOG_DEBUG "Found file %s\n", dirent->name); + } } diff --git a/lib/string.c b/lib/string.c index ffa67fe..647f2f5 100644 --- a/lib/string.c +++ b/lib/string.c @@ -140,7 +140,7 @@ char *strtok(char *str, const char *delim, char **llast) if(!(*ret)) return NULL; - + return ret; }