fs:replase all asprintf / strdup in fs with fs_heap_xxx

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1 2024-10-12 17:06:17 +08:00 committed by Xiang Xiao
parent 7d218f93fa
commit b613863bad
20 changed files with 120 additions and 59 deletions

View file

@ -65,9 +65,10 @@ if(CONFIG_BLK_RPMSG)
endif()
if(CONFIG_BLK_RPMSG_SERVER)
set_source_files_properties(
rpmsgblk_server.c DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/..
PROPERTIES INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs/inode)
target_include_directories(drivers PRIVATE ${NUTTX_DIR}/fs/inode
${NUTTX_DIR}/fs)
set_source_files_properties(rpmsgblk_server.c DIRECTORY
${CMAKE_CURRENT_LIST_DIR}/..)
list(APPEND SRCS rpmsgblk_server.c)
endif()

View file

@ -70,6 +70,7 @@ endif
ifeq ($(CONFIG_BLK_RPMSG_SERVER),y)
CSRCS += rpmsgblk_server.c
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs$(DELIM)inode
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs
endif
ifneq ($(CONFIG_DEV_OPTEE_NONE),y)

View file

@ -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)
@ -112,7 +113,7 @@ static FAR char *unique_chardev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);
return fs_heap_strdup(devbuf);
}
/* It is in use, try again */
@ -198,14 +199,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;
}

View file

@ -38,6 +38,7 @@
#include <nuttx/mutex.h>
#include "driver/driver.h"
#include "fs_heap.h"
/****************************************************************************
* Private Data
@ -105,7 +106,7 @@ static FAR char *unique_blkdev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);
return fs_heap_strdup(devbuf);
}
/* It is in use, try again */
@ -188,6 +189,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;
}

View file

@ -73,4 +73,46 @@ void fs_heap_free(FAR void *mem)
mm_free(g_fs_heap, mem);
}
FAR char *fs_heap_strdup(FAR const char *s)
{
size_t len = strlen(s) + 1;
FAR char *copy = fs_heap_malloc(len);
if (copy != NULL)
{
memcpy(copy, s, len);
}
return copy;
}
int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
{
va_list ap;
int len;
/* Calculates the length required to format the string */
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (len < 0)
{
*strp = NULL;
return len;
}
*strp = fs_heap_malloc(len + 1);
if (*strp == NULL)
{
return -ENOMEM;
}
va_start(ap, fmt);
vsnprintf(*strp, len + 1, fmt, ap);
va_end(ap);
return len;
}
#endif

View file

@ -26,20 +26,27 @@
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <nuttx/kmalloc.h>
#include <stdio.h>
#include <string.h>
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#if defined(CONFIG_FS_HEAPSIZE) && CONFIG_FS_HEAPSIZE > 0
void fs_heap_initialize(void);
FAR void *fs_heap_zalloc(size_t size);
FAR void *fs_heap_malloc(size_t size);
FAR void *fs_heap_zalloc(size_t size) malloc_like1(1);
FAR void *fs_heap_malloc(size_t size) malloc_like1(1);
size_t fs_heap_malloc_size(FAR void *mem);
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size);
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size) realloc_like(2);
void fs_heap_free(FAR void *mem);
FAR char *fs_heap_strdup(FAR const char *s) malloc_like;
int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
printf_like(2, 3);
#else
# define fs_heap_initialize()
# define fs_heap_zalloc kmm_zalloc
@ -47,6 +54,8 @@ void fs_heap_free(FAR void *mem);
# define fs_heap_malloc_size kmm_malloc_size
# define fs_heap_realloc kmm_realloc
# define fs_heap_free kmm_free
# define fs_heap_strdup strdup
# define fs_heap_asprintf asprintf
#endif
#endif

View file

@ -1052,7 +1052,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "fs=whatever", remote dir
*/
options = strdup(data);
options = fs_heap_strdup(data);
if (!options)
{
fs_heap_free(fs);
@ -1070,7 +1070,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 */

View file

@ -34,6 +34,7 @@
#include <nuttx/fs/fs.h>
#include "inode/inode.h"
#include "fs_heap.h"
/****************************************************************************
* Private Function Prototypes
@ -348,12 +349,12 @@ static int _inode_search(FAR struct inode_search_s *desc)
{
FAR char *buffer = NULL;
ret = asprintf(&buffer,
"%s/%s", desc->relpath,
name);
ret = fs_heap_asprintf(&buffer, "%s/%s",
desc->relpath,
name);
if (ret > 0)
{
lib_free(desc->buffer);
fs_heap_free(desc->buffer);
desc->buffer = buffer;
relpath = buffer;
ret = OK;
@ -478,7 +479,8 @@ int inode_search(FAR struct inode_search_s *desc)
if (*desc->path != '/')
{
ret = asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
ret = fs_heap_asprintf(&desc->buffer, "%s/%s",
_inode_getcwd(), desc->path);
if (ret < 0)
{
return -ENOMEM;

View file

@ -39,6 +39,8 @@
#include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h>
#include "fs_heap.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -61,7 +63,7 @@
{ \
if ((d)->buffer != NULL) \
{ \
lib_free((d)->buffer); \
fs_heap_free((d)->buffer); \
(d)->buffer = NULL; \
} \
} \

View file

@ -746,7 +746,7 @@ inotify_alloc_watch_list(FAR const char *path)
}
list_initialize(&list->watches);
list->path = strdup(path);
list->path = fs_heap_strdup(path);
if (list->path == NULL)
{
fs_heap_free(list);
@ -757,7 +757,7 @@ inotify_alloc_watch_list(FAR const char *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;
}
@ -1022,7 +1022,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);
}
@ -1150,7 +1150,7 @@ out:
out_free:
fs_putfilep(filep);
lib_free(abspath);
fs_heap_free(abspath);
if (ret < 0)
{
set_errno(-ret);

View file

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

View file

@ -508,7 +508,7 @@ 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_strdup(name);
if (!wrfile->ofile.entry.name)
{
ret = -ENOMEM;
@ -655,7 +655,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);

View file

@ -34,6 +34,7 @@
#include <nuttx/kmalloc.h>
#include "nxffs.h"
#include "fs_heap.h"
/****************************************************************************
* Private Types
@ -1089,7 +1090,7 @@ nxffs_setupwriter(FAR struct nxffs_volume_s *volume,
/* Initialize for the packing operation. */
memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
pack->dest.entry.name = strdup(wrfile->ofile.entry.name);
pack->dest.entry.name = fs_heap_strdup(wrfile->ofile.entry.name);
pack->dest.entry.utc = wrfile->ofile.entry.utc;
pack->dest.entry.datlen = wrfile->ofile.entry.datlen;

View file

@ -1075,7 +1075,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "timeout=xx", connect timeout, unit (ms)
*/
options = strdup(data);
options = fs_heap_strdup(data);
if (!options)
{
fs_heap_free(fs);
@ -1106,7 +1106,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);

View file

@ -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 */
@ -1216,7 +1216,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 */

View file

@ -729,11 +729,11 @@ static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name)
if (path[pathlen - 1] == '/')
{
ret = asprintf(&relpath, "%s%s", path, name);
ret = fs_heap_asprintf(&relpath, "%s%s", path, name);
}
else
{
ret = asprintf(&relpath, "%s/%s", path, name);
ret = fs_heap_asprintf(&relpath, "%s/%s", path, name);
}
/* Handle errors */
@ -753,7 +753,7 @@ static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name)
* will work later).
*/
return strdup(name);
return fs_heap_strdup(name);
}
}
@ -840,12 +840,12 @@ static void unionfs_destroy(FAR struct unionfs_inode_s *ui)
if (ui->ui_fs[0].um_prefix)
{
lib_free(ui->ui_fs[0].um_prefix);
fs_heap_free(ui->ui_fs[0].um_prefix);
}
if (ui->ui_fs[1].um_prefix)
{
lib_free(ui->ui_fs[1].um_prefix);
fs_heap_free(ui->ui_fs[1].um_prefix);
}
/* And finally free the allocated unionfs state structure as well */
@ -1436,7 +1436,7 @@ static int unionfs_opendir(FAR struct inode *mountpt,
if (strlen(relpath) > 0)
{
udir->fu_relpath = strdup(relpath);
udir->fu_relpath = fs_heap_strdup(relpath);
if (!udir->fu_relpath)
{
goto errout_with_lock;
@ -1522,7 +1522,7 @@ static int unionfs_opendir(FAR struct inode *mountpt,
errout_with_relpath:
if (udir->fu_relpath != NULL)
{
lib_free(udir->fu_relpath);
fs_heap_free(udir->fu_relpath);
}
errout_with_lock:
@ -1776,7 +1776,7 @@ static int unionfs_readdir(FAR struct inode *mountpt,
/* Free the allocated relpath */
lib_free(relpath);
fs_heap_free(relpath);
/* Check for a duplicate */
@ -1863,7 +1863,7 @@ static int unionfs_readdir(FAR struct inode *mountpt,
/* Free the allocated relpath */
lib_free(relpath);
fs_heap_free(relpath);
}
}
}
@ -1946,7 +1946,7 @@ static int unionfs_bind(FAR struct inode *blkdriver, FAR const void *data,
/* Parse options from mount syscall */
dup = tmp = strdup(data);
dup = tmp = fs_heap_strdup(data);
if (!dup)
{
return -ENOMEM;
@ -1975,7 +1975,7 @@ static int unionfs_bind(FAR struct inode *blkdriver, FAR const void *data,
/* Call unionfs_dobind to do the real work. */
ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2, handle);
lib_free(dup);
fs_heap_free(dup);
return ret;
}
@ -2625,10 +2625,10 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1,
if (prefix1 && strlen(prefix1) > 0)
{
ui->ui_fs[0].um_prefix = strdup(prefix1);
ui->ui_fs[0].um_prefix = fs_heap_strdup(prefix1);
if (ui->ui_fs[0].um_prefix == NULL)
{
ferr("ERROR: strdup(prefix1) failed\n");
ferr("ERROR: fs_heap_strdup(prefix1) failed\n");
ret = -ENOMEM;
goto errout_with_fs2;
}
@ -2636,10 +2636,10 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1,
if (prefix2 && strlen(prefix2) > 0)
{
ui->ui_fs[1].um_prefix = strdup(prefix2);
ui->ui_fs[1].um_prefix = fs_heap_strdup(prefix2);
if (ui->ui_fs[1].um_prefix == NULL)
{
ferr("ERROR: strdup(prefix2) failed\n");
ferr("ERROR: fs_heap_strdup(prefix2) failed\n");
ret = -ENOMEM;
goto errout_with_prefix1;
}
@ -2661,7 +2661,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1,
errout_with_prefix1:
if (ui->ui_fs[0].um_prefix != NULL)
{
lib_free(ui->ui_fs[0].um_prefix);
fs_heap_free(ui->ui_fs[0].um_prefix);
}
errout_with_fs2:

View file

@ -454,7 +454,7 @@ static int dir_close(FAR struct file *filep)
/* Release our references on the contained 'root' inode */
inode_release(inode);
lib_free(relpath);
fs_heap_free(relpath);
return ret;
}
@ -608,7 +608,7 @@ int dir_allocate(FAR struct file *filep, FAR const char *relpath)
}
inode_getpath(inode, path_prefix, sizeof(path_prefix));
ret = asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath);
ret = fs_heap_asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath);
if (ret < 0)
{
dir->fd_path = NULL;

View file

@ -300,7 +300,7 @@ file_lock_find_bucket(FAR const char *filepath)
static void file_lock_free_entry(FAR ENTRY *entry)
{
lib_free(entry->key);
fs_heap_free(entry->key);
fs_heap_free(entry->data);
}
@ -323,7 +323,7 @@ file_lock_create_bucket(FAR const char *filepath)
/* Creating an instance store */
item.key = strdup(filepath);
item.key = fs_heap_strdup(filepath);
if (item.key == NULL)
{
fs_heap_free(bucket);
@ -334,7 +334,7 @@ file_lock_create_bucket(FAR const char *filepath)
if (hsearch_r(item, ENTER, &hretvalue, &g_file_lock_table) == 0)
{
lib_free(item.key);
fs_heap_free(item.key);
fs_heap_free(bucket);
return NULL;
}

View file

@ -124,7 +124,7 @@ next_subdir:
if (subdir != NULL)
{
lib_free(subdir);
fs_heap_free(subdir);
subdir = NULL;
}
@ -134,7 +134,7 @@ next_subdir:
*/
subdirname = basename((FAR char *)oldpath);
ret = asprintf(&subdir, "%s/%s", newpath, subdirname);
ret = fs_heap_asprintf(&subdir, "%s/%s", newpath, subdirname);
if (ret < 0)
{
subdir = NULL;
@ -256,7 +256,7 @@ errout:
RELEASE_SEARCH(&newdesc);
if (subdir != NULL)
{
lib_free(subdir);
fs_heap_free(subdir);
}
return ret;
@ -388,7 +388,7 @@ next_subdir:
FAR void *tmp = subdir;
ret = asprintf(&subdir, "%s/%s", newrelpath,
ret = fs_heap_asprintf(&subdir, "%s/%s", newrelpath,
subdirname);
if (tmp != NULL)
{
@ -479,7 +479,7 @@ errout_with_newsearch:
RELEASE_SEARCH(&newdesc);
if (subdir != NULL)
{
lib_free(subdir);
fs_heap_free(subdir);
}
return ret;

View file

@ -37,6 +37,7 @@
#include "notify/notify.h"
#include "inode/inode.h"
#include "fs_heap.h"
/****************************************************************************
* Pre-processor Definitions
@ -129,7 +130,7 @@ int symlink(FAR const char *path1, FAR const char *path2)
{
/* Copy path1 */
FAR char *newpath2 = strdup(path1);
FAR char *newpath2 = fs_heap_strdup(path1);
if (newpath2 == NULL)
{
errcode = ENOMEM;
@ -146,7 +147,7 @@ int symlink(FAR const char *path1, FAR const char *path2)
inode_unlock();
if (ret < 0)
{
lib_free(newpath2);
fs_heap_free(newpath2);
errcode = -ret;
goto errout_with_search;
}