ext2: fix memory leak

This commit is contained in:
Michael Shaw 2023-10-20 01:11:10 -04:00
commit cd8d016f34
3 changed files with 18 additions and 5 deletions

View file

@ -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)

View file

@ -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);
}
}

View file

@ -140,7 +140,7 @@ char *strtok(char *str, const char *delim, char **llast)
if(!(*ret))
return NULL;
return ret;
}