Skip to content

Commit

Permalink
dummyfs: fix memory leaks
Browse files Browse the repository at this point in the history
- deleted dirents for dummyfs root dir were never freed
 - fixed memory leaks in error paths
 - fixed ERRNO value

Related to phoenix-rtos/phoenix-rtos-project#169
  • Loading branch information
nalajcie committed Aug 19, 2021
1 parent c10376a commit cb29b1e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
18 changes: 12 additions & 6 deletions dummyfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#include <string.h>

#include "dummyfs.h"
#include "dir.h"


int dir_find(dummyfs_object_t *dir, const char *name, oid_t *res)
{

dummyfs_dirent_t *e = dir->entries;
char *dirname = strdup(name);
char *end = strchr(dirname, '/');
char *dirname, *end;
int len;

if (!S_ISDIR(dir->mode))
Expand All @@ -35,6 +35,8 @@ int dir_find(dummyfs_object_t *dir, const char *name, oid_t *res)
if (e == NULL)
return -ENOENT;

dirname = strdup(name);
end = strchr(dirname, '/');
if (end != NULL)
*end = 0;

Expand All @@ -60,15 +62,16 @@ int dir_replace(dummyfs_object_t *dir, const char *name, oid_t *new)
{

dummyfs_dirent_t *e = dir->entries;
char *dirname = strdup(name);
char *end = strchr(dirname, '/');
char *dirname, *end;

if (!S_ISDIR(dir->mode))
return -ENOTDIR;

if (e == NULL)
return -ENOENT;

dirname = strdup(name);
end = strchr(dirname, '/');
if (end != NULL)
*end = 0;

Expand Down Expand Up @@ -167,9 +170,12 @@ int dir_remove(dummyfs_object_t *dir, const char *name)

do {
if (!strcmp(e->name, (char *)name) && !e->deleted) {
dir->size -= strlen(e->name);
dir->size -= e->len;
e->deleted = 1;
dir->dirty = 1;

if (++dir->dirty > DUMMYFS_DIRTY_DIR_AUTOCLEANUP_THRESH)
dir_clean(dir);

return EOK;
}
e = e->next;
Expand Down
2 changes: 1 addition & 1 deletion dummyfs/dummyfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int dummyfs_unlink(oid_t *dir, const char *name)
object_unlock(d);
object_put(d);
object_put(o);
return -EINVAL;
return -ENOTEMPTY;
}

ret = dir_remove(d, name);
Expand Down
3 changes: 3 additions & 0 deletions dummyfs/dummyfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#define DUMMYFS_SIZE_MAX 32 * 1024 * 1024

/* threshold for cleaning directory from deleted dirents */
#define DUMMYFS_DIRTY_DIR_AUTOCLEANUP_THRESH 8


typedef struct _dummyfs_dirent_t {
char *name;
Expand Down
1 change: 1 addition & 0 deletions dummyfs/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dummyfs_object_t *object_create(void)
}

if (dummyfs_incsz(sizeof(dummyfs_object_t)) != EOK) {
free(r);
mutexUnlock(dummyfs_common.mutex);
mutexUnlock(olock);
return NULL;
Expand Down

0 comments on commit cb29b1e

Please sign in to comment.