diff --git a/source/fs/mufs/path.c b/source/fs/mufs/path.c index b58f9c0..21b5215 100644 --- a/source/fs/mufs/path.c +++ b/source/fs/mufs/path.c @@ -19,8 +19,7 @@ int mufs_mkpath(struct mufs *fs, const char *path, struct mufs_tree *subtree, struct mufs_stat stat; struct mufs_dirent dirent; struct mufs_tree *tree; - char *s; - char *name; + char *s, *root, *name; size_t len; int ret; @@ -33,17 +32,18 @@ int mufs_mkpath(struct mufs *fs, const char *path, struct mufs_tree *subtree, return 0; } + root = s; name = strrchr(s, '/'); *name++ = '\0'; - if (*s == '\0') - s = "/"; + if (*root == '\0') + root = "/"; if (*name == '\0') - return -1; + goto err_free_s; - if (resolve_path(fs, s, &tree, &stat) < 0 || stat.type != MUFS_DIR) - return -1; + if (resolve_path(fs, root, &tree, &stat) < 0 || stat.type != MUFS_DIR) + goto err_free_s; memset(&dirent, 0, sizeof dirent); @@ -65,14 +65,20 @@ int mufs_mkpath(struct mufs *fs, const char *path, struct mufs_tree *subtree, if (!is_aligned(tree->file_size, fs->dev->log2_block_size) && write_dirent(tree, tree->file_size, &dirent) > 0) { mufs_del_tree(tree); + free(s); return 0; } ret = write_dirent(tree, align_up(tree->file_size, fs->dev->log2_block_size), &dirent); mufs_del_tree(tree); + free(s); return ret; + +err_free_s: + free(s); + return -1; } static int remove_page(struct mufs_tree *tree, uint32_t va)