Skip to content

Commit

Permalink
fs:replase all strdup in fs mode with fs_heap_malloc && memcpy
Browse files Browse the repository at this point in the history
Change-Id: I5f3e27b2b9740bcaf6857869f0608c9d79bbcbfd
Signed-off-by: guohao15 <[email protected]>
  • Loading branch information
guohao15 authored and crafcat7 committed Oct 12, 2024
1 parent 116e4b5 commit 64dff49
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 41 deletions.
18 changes: 15 additions & 3 deletions fs/driver/fs_blockproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <nuttx/mutex.h>

#include "driver.h"
#include "fs_heap.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
Expand Down Expand Up @@ -80,6 +81,8 @@ static FAR char *unique_chardev(void)
struct stat statbuf;
char devbuf[16];
uint32_t devno;
FAR char *buf;
size_t len;
int ret;

/* Loop until we get a unique device name */
Expand Down Expand Up @@ -112,7 +115,16 @@ static FAR char *unique_chardev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);

len = strlen(devbuf) + 1;
buf = fs_heap_malloc(len);
if (buf == NULL)
{
return NULL;
}

memcpy(buf, devbuf, len);
return buf;
}

/* It is in use, try again */
Expand Down Expand Up @@ -198,14 +210,14 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)

/* Free the allocated character driver name. */

lib_free(chardev);
fs_heap_free(chardev);
return OK;

errout_with_bchdev:
nx_unlink(chardev);

errout_with_chardev:
lib_free(chardev);
fs_heap_free(chardev);
return ret;
}

Expand Down
16 changes: 14 additions & 2 deletions fs/driver/fs_mtdproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <nuttx/mutex.h>

#include "driver/driver.h"
#include "fs_heap.h"

/****************************************************************************
* Private Data
Expand Down Expand Up @@ -73,6 +74,8 @@ static FAR char *unique_blkdev(void)
struct stat statbuf;
char devbuf[16];
uint32_t devno;
FAR char *buf;
size_t len;
int ret;

/* Loop until we get a unique device name */
Expand Down Expand Up @@ -105,7 +108,16 @@ static FAR char *unique_blkdev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);

len = strlen(devbuf) + 1;
buf = fs_heap_malloc(len);
if (buf == NULL)
{
return NULL;
}

memcpy(buf, devbuf, len);
return buf;
}

/* It is in use, try again */
Expand Down Expand Up @@ -188,6 +200,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
out_with_fltdev:
nx_unlink(blkdev);
out_with_blkdev:
lib_free(blkdev);
fs_heap_free(blkdev);
return ret;
}
11 changes: 8 additions & 3 deletions fs/hostfs/hostfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,18 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "fs=whatever", remote dir
*/

options = strdup(data);
if (!options)
len = strlen(data) + 1;
options = (FAR char *)fs_heap_malloc(len);
if (options == NULL)
{
fs_heap_free(fs);
return -ENOMEM;
}

/* Copy the options string */

memcpy(options, data, len);

ptr = strtok_r(options, ",", &saveptr);
while (ptr != NULL)
{
Expand All @@ -1070,7 +1075,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
ptr = strtok_r(NULL, ",", &saveptr);
}

lib_free(options);
fs_heap_free(options);

/* Take the lock for the mount */

Expand Down
23 changes: 17 additions & 6 deletions fs/notify/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ inotify_alloc_watch_list(FAR const char *path)
{
FAR struct inotify_watch_list_s *list;
FAR ENTRY *result;
size_t len;
ENTRY item;

list = fs_heap_zalloc(sizeof(struct inotify_watch_list_s));
Expand All @@ -746,18 +747,20 @@ inotify_alloc_watch_list(FAR const char *path)
}

list_initialize(&list->watches);
list->path = strdup(path);
len = strlen(path) + 1;
list->path = fs_heap_malloc(len);
if (list->path == NULL)
{
fs_heap_free(list);
return NULL;
}

memcpy(list->path, path, len);
item.key = list->path;
item.data = list;
if (hsearch_r(item, ENTER, &result, &g_inotify.hash) == 0)
{
lib_free(list->path);
fs_heap_free(list->path);
fs_heap_free(list);
return NULL;
}
Expand Down Expand Up @@ -1022,7 +1025,7 @@ static void notify_free_entry(FAR ENTRY *entry)
{
/* Key is alloced by lib_malloc, value is alloced by fs_heap_malloc */

lib_free(entry->key);
fs_heap_free(entry->key);
fs_heap_free(entry->data);
}

Expand Down Expand Up @@ -1058,6 +1061,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
FAR struct inotify_watch_s *old;
FAR struct inotify_device_s *dev;
FAR struct file *filep;
FAR char *pathbuffer;
FAR char *abspath;
struct stat buf;
int ret;
Expand All @@ -1075,13 +1079,20 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
return ERROR;
}

abspath = lib_realpath(pathname, NULL, mask & IN_DONT_FOLLOW);
if (abspath == NULL)
pathbuffer = fs_heap_malloc(PATH_MAX);
if (pathbuffer == NULL)
{
fs_putfilep(filep);
set_errno(ENOMEM);
return ERROR;
}

abspath = lib_realpath(pathname, pathbuffer, mask & IN_DONT_FOLLOW);
if (abspath == NULL)
{
goto out_free;
}

ret = nx_stat(abspath, &buf, 1);
if (ret < 0)
{
Expand Down Expand Up @@ -1150,7 +1161,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)

out_free:
fs_putfilep(filep);
lib_free(abspath);
fs_heap_free(pathbuffer);
if (ret < 0)
{
set_errno(-ret);
Expand Down
2 changes: 1 addition & 1 deletion fs/nxffs/nxffs_inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void nxffs_freeentry(FAR struct nxffs_entry_s *entry)
{
if (entry->name)
{
lib_free(entry->name);
fs_heap_free(entry->name);
entry->name = NULL;
}
}
Expand Down
6 changes: 4 additions & 2 deletions fs/nxffs/nxffs_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,15 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,

/* Save a copy of the inode name. */

wrfile->ofile.entry.name = strdup(name);
wrfile->ofile.entry.name = fs_heap_malloc(namlen + 1);
if (!wrfile->ofile.entry.name)
{
ret = -ENOMEM;
goto errout_with_ofile;
}

memcpy(wrfile->ofile.entry.name, name, namlen + 1);

/* Allocate FLASH memory for the file and set up for the write.
*
* Loop until the inode header is configured or until a failure occurs.
Expand Down Expand Up @@ -655,7 +657,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
return OK;

errout_with_name:
lib_free(wrfile->ofile.entry.name);
fs_heap_free(wrfile->ofile.entry.name);
errout_with_ofile:
#ifndef CONFIG_NXFFS_PREALLOCATED
fs_heap_free(wrfile);
Expand Down
11 changes: 8 additions & 3 deletions fs/rpmsgfs/rpmsgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,13 +1075,18 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "timeout=xx", connect timeout, unit (ms)
*/

options = strdup(data);
if (!options)
len = strlen(data) + 1;
options = (FAR char *)fs_heap_malloc(len);
if (options == NULL)
{
fs_heap_free(fs);
return -ENOMEM;
}

/* Copy the options string */

memcpy(options, data, len);

/* Set timeout default value */

fs->timeout = INT_MAX;
Expand All @@ -1106,7 +1111,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
}

ret = rpmsgfs_client_bind(&fs->handle, cpuname);
lib_free(options);
fs_heap_free(options);
if (ret < 0)
{
fs_heap_free(fs);
Expand Down
8 changes: 5 additions & 3 deletions fs/tmpfs/fs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,

if (tdo->tdo_entry[index].tde_name != NULL)
{
lib_free(tdo->tdo_entry[index].tde_name);
fs_heap_free(tdo->tdo_entry[index].tde_name);
}

/* Remove by replacing this entry with the final directory entry */
Expand Down Expand Up @@ -521,12 +521,14 @@ static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
}
}

newname = strndup(name, namelen);
newname = fs_heap_malloc(namelen + 1);
if (newname == NULL)
{
return -ENOMEM;
}

memcpy(newname, name, namelen + 1);

/* Get the new number of entries */

nentries = tdo->tdo_nentries + 1;
Expand Down Expand Up @@ -1216,7 +1218,7 @@ static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,

if (tdo->tdo_entry[index].tde_name != NULL)
{
lib_free(tdo->tdo_entry[index].tde_name);
fs_heap_free(tdo->tdo_entry[index].tde_name);
}

/* Remove by replacing this entry with the final directory entry */
Expand Down
Loading

0 comments on commit 64dff49

Please sign in to comment.