From 1b0baa833742f4cd9018b7c69f6faed649faefb6 Mon Sep 17 00:00:00 2001 From: Petro Karashchenko Date: Tue, 8 Aug 2023 01:11:20 +0300 Subject: [PATCH] nuttx: use lib_free for memory de-allocation after strdup or asprintf The memory allocated with strdup and asprintf is done via lib_malloc so we need to use lib_free to deallocate memory otherwise the assertion "Free memory from the wrong heap" is hit with flat mode and user separated heap enabled mode. Signed-off-by: Petro Karashchenko --- drivers/efuse/efuse.c | 6 ++-- drivers/modem/u-blox.c | 60 +++++++++++++++++----------------- drivers/syslog/syslog_device.c | 4 +-- drivers/timers/timer.c | 6 ++-- drivers/timers/watchdog.c | 6 ++-- fs/driver/fs_blockproxy.c | 6 ++-- fs/driver/fs_mtdproxy.c | 4 +-- fs/hostfs/hostfs.c | 4 +-- fs/nxffs/nxffs_inode.c | 4 +-- fs/nxffs/nxffs_open.c | 4 +-- fs/rpmsgfs/rpmsgfs.c | 4 +-- fs/unionfs/fs_unionfs.c | 12 +++---- fs/vfs/fs_rename.c | 5 +-- fs/vfs/fs_symlink.c | 6 ++-- include/nuttx/lib/lib.h | 2 +- libs/libc/netdb/lib_rexec.c | 4 ++- net/procfs/net_procfs.c | 6 ++-- sched/environ/env_putenv.c | 8 ++--- 18 files changed, 77 insertions(+), 74 deletions(-) diff --git a/drivers/efuse/efuse.c b/drivers/efuse/efuse.c index 20102015ed..4af62ccbc3 100644 --- a/drivers/efuse/efuse.c +++ b/drivers/efuse/efuse.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include @@ -311,7 +311,7 @@ FAR void *efuse_register(FAR const char *path, return (FAR void *)upper; errout_with_path: - kmm_free(upper->path); + lib_free(upper->path); errout_with_upper: nxmutex_destroy(&upper->lock); @@ -356,7 +356,7 @@ void efuse_unregister(FAR void *handle) /* Then free all of the driver resources */ - kmm_free(upper->path); + lib_free(upper->path); nxmutex_destroy(&upper->lock); kmm_free(upper); } diff --git a/drivers/modem/u-blox.c b/drivers/modem/u-blox.c index ad1fb8f6e6..9f05344e94 100644 --- a/drivers/modem/u-blox.c +++ b/drivers/modem/u-blox.c @@ -50,7 +50,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -77,30 +77,30 @@ struct ubxmdm_upper { - FAR char * path; /* Registration path */ + FAR char *path; /* Registration path */ /* The contained lower-half driver. */ - FAR struct ubxmdm_lower * lower; + FAR struct ubxmdm_lower *lower; }; /**************************************************************************** * Private Function Prototypes ****************************************************************************/ -static ssize_t ubxmdm_read (FAR struct file * filep, - FAR char * buffer, +static ssize_t ubxmdm_read(FAR struct file *filep, + FAR char *buffer, + size_t buflen); +static ssize_t ubxmdm_write(FAR struct file *filep, + FAR const char *buffer, size_t buflen); -static ssize_t ubxmdm_write(FAR struct file * filep, - FAR const char * buffer, - size_t buflen); -static int ubxmdm_ioctl(FAR struct file * filep, +static int ubxmdm_ioctl(FAR struct file *filep, int cmd, unsigned long arg); -static int ubxmdm_poll (FAR struct file * filep, - FAR struct pollfd * fds, - bool setup); +static int ubxmdm_poll(FAR struct file *filep, + FAR struct pollfd *fds, + bool setup); /**************************************************************************** * Private Data @@ -123,29 +123,29 @@ static const struct file_operations g_ubxmdm_fops = * Private Functions ****************************************************************************/ -static ssize_t ubxmdm_read(FAR struct file * filep, - FAR char * buffer, +static ssize_t ubxmdm_read(FAR struct file *filep, + FAR char *buffer, size_t len) { return 0; /* Return EOF */ } -static ssize_t ubxmdm_write(FAR struct file * filep, - FAR const char * buffer, +static ssize_t ubxmdm_write(FAR struct file *filep, + FAR const char *buffer, size_t len) { return len; /* Say that everything was written */ } -static int ubxmdm_ioctl(FAR struct file * filep, +static int ubxmdm_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { - FAR struct inode * inode = filep->f_inode; - FAR struct ubxmdm_upper * upper; - FAR struct ubxmdm_lower * lower; - int ret; - FAR struct ubxmdm_status * status; + FAR struct inode *inode = filep->f_inode; + FAR struct ubxmdm_upper *upper; + FAR struct ubxmdm_lower *lower; + FAR struct ubxmdm_status *status; + int ret; m_info("cmd: %d arg: %ld\n", cmd, arg); upper = inode->i_private; @@ -214,7 +214,7 @@ static int ubxmdm_ioctl(FAR struct file * filep, case MODEM_IOC_GETSTATUS: if (lower->ops->getstatus) { - status = (FAR struct ubxmdm_status *) ((uintptr_t) arg); + status = (FAR struct ubxmdm_status *)((uintptr_t)arg); if (status) { ret = lower->ops->getstatus(lower, status); @@ -253,8 +253,8 @@ static int ubxmdm_ioctl(FAR struct file * filep, return ret; } -static int ubxmdm_poll(FAR struct file * filep, - FAR struct pollfd * fds, +static int ubxmdm_poll(FAR struct file *filep, + FAR struct pollfd *fds, bool setup) { if (setup) @@ -269,8 +269,8 @@ static int ubxmdm_poll(FAR struct file * filep, * Public Functions ****************************************************************************/ -FAR void * ubxmdm_register(FAR const char * path, - FAR struct ubxmdm_lower * lower) +FAR void *ubxmdm_register(FAR const char *path, + FAR struct ubxmdm_lower *lower) { FAR struct ubxmdm_upper *upper; int ret; @@ -300,10 +300,10 @@ FAR void * ubxmdm_register(FAR const char * path, goto errout_with_path; } - return (FAR void *) upper; + return (FAR void *)upper; errout_with_path: - kmm_free(upper->path); + lib_free(upper->path); errout_with_upper: kmm_free(upper); @@ -329,6 +329,6 @@ void ubxmdm_unregister(FAR void *handle) unregister_driver(upper->path); - kmm_free(upper->path); + lib_free(upper->path); kmm_free(upper); } diff --git a/drivers/syslog/syslog_device.c b/drivers/syslog/syslog_device.c index 2031ca136c..f382be3a6b 100644 --- a/drivers/syslog/syslog_device.c +++ b/drivers/syslog/syslog_device.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -781,7 +781,7 @@ void syslog_dev_uninitialize(FAR struct syslog_channel_s *channel) if (syslog_dev->sl_devpath != NULL) { - kmm_free(syslog_dev->sl_devpath); + lib_free(syslog_dev->sl_devpath); } /* Free the channel structure */ diff --git a/drivers/timers/timer.c b/drivers/timers/timer.c index 4777199282..72c91b4264 100644 --- a/drivers/timers/timer.c +++ b/drivers/timers/timer.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include #include @@ -473,7 +473,7 @@ FAR void *timer_register(FAR const char *path, return (FAR void *)upper; errout_with_path: - kmm_free(upper->path); + lib_free(upper->path); errout_with_upper: nxmutex_destroy(&upper->lock); @@ -523,7 +523,7 @@ void timer_unregister(FAR void *handle) /* Then free all of the driver resources */ nxmutex_destroy(&upper->lock); - kmm_free(upper->path); + lib_free(upper->path); kmm_free(upper); } diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c index fb2aa20e1a..2bae74ec2e 100644 --- a/drivers/timers/watchdog.c +++ b/drivers/timers/watchdog.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include #include @@ -790,7 +790,7 @@ FAR void *watchdog_register(FAR const char *path, return (FAR void *)upper; errout_with_path: - kmm_free(upper->path); + lib_free(upper->path); errout_with_upper: nxmutex_destroy(&upper->lock); @@ -847,7 +847,7 @@ void watchdog_unregister(FAR void *handle) /* Then free all of the driver resources */ - kmm_free(upper->path); + lib_free(upper->path); nxmutex_destroy(&upper->lock); kmm_free(upper); } diff --git a/fs/driver/fs_blockproxy.c b/fs/driver/fs_blockproxy.c index 048da735c7..0aafca530a 100644 --- a/fs/driver/fs_blockproxy.c +++ b/fs/driver/fs_blockproxy.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -197,14 +197,14 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags) /* Free the allocated character driver name. */ - kmm_free(chardev); + lib_free(chardev); return OK; errout_with_bchdev: nx_unlink(chardev); errout_with_chardev: - kmm_free(chardev); + lib_free(chardev); return ret; } diff --git a/fs/driver/fs_mtdproxy.c b/fs/driver/fs_mtdproxy.c index abe68775ba..4a73568ad1 100644 --- a/fs/driver/fs_mtdproxy.c +++ b/fs/driver/fs_mtdproxy.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include @@ -187,6 +187,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags, out_with_fltdev: nx_unlink(blkdev); out_with_blkdev: - kmm_free(blkdev); + lib_free(blkdev); return ret; } diff --git a/fs/hostfs/hostfs.c b/fs/hostfs/hostfs.c index 43da3eb4a2..13ba626368 100644 --- a/fs/hostfs/hostfs.c +++ b/fs/hostfs/hostfs.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -1048,7 +1048,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, ptr = strtok_r(NULL, ",", &saveptr); } - kmm_free(options); + lib_free(options); /* Take the lock for the mount */ diff --git a/fs/nxffs/nxffs_inode.c b/fs/nxffs/nxffs_inode.c index df3d9ec37a..d2a07271d5 100644 --- a/fs/nxffs/nxffs_inode.c +++ b/fs/nxffs/nxffs_inode.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include "nxffs.h" @@ -212,7 +212,7 @@ void nxffs_freeentry(FAR struct nxffs_entry_s *entry) { if (entry->name) { - kmm_free(entry->name); + lib_free(entry->name); entry->name = NULL; } } diff --git a/fs/nxffs/nxffs_open.c b/fs/nxffs/nxffs_open.c index ce609d8cc3..92c750e5b7 100644 --- a/fs/nxffs/nxffs_open.c +++ b/fs/nxffs/nxffs_open.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include @@ -654,7 +654,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, return OK; errout_with_name: - kmm_free(wrfile->ofile.entry.name); + lib_free(wrfile->ofile.entry.name); errout_with_ofile: #ifndef CONFIG_NXFFS_PREALLOCATED kmm_free(wrfile); diff --git a/fs/rpmsgfs/rpmsgfs.c b/fs/rpmsgfs/rpmsgfs.c index 96f26d27fb..ce1112a071 100644 --- a/fs/rpmsgfs/rpmsgfs.c +++ b/fs/rpmsgfs/rpmsgfs.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -1108,7 +1108,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data, } ret = rpmsgfs_client_bind(&fs->handle, cpuname); - kmm_free(options); + lib_free(options); if (ret < 0) { kmm_free(fs); diff --git a/fs/unionfs/fs_unionfs.c b/fs/unionfs/fs_unionfs.c index 53a726840c..a845cc7743 100644 --- a/fs/unionfs/fs_unionfs.c +++ b/fs/unionfs/fs_unionfs.c @@ -40,7 +40,7 @@ #include #include -#include +#include #include #include #include @@ -824,12 +824,12 @@ static void unionfs_destroy(FAR struct unionfs_inode_s *ui) if (ui->ui_fs[0].um_prefix) { - kmm_free(ui->ui_fs[0].um_prefix); + lib_free(ui->ui_fs[0].um_prefix); } if (ui->ui_fs[1].um_prefix) { - kmm_free(ui->ui_fs[1].um_prefix); + lib_free(ui->ui_fs[1].um_prefix); } /* And finally free the allocated unionfs state structure as well */ @@ -1516,7 +1516,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, errout_with_relpath: if (udir->fu_relpath != NULL) { - kmm_free(udir->fu_relpath); + lib_free(udir->fu_relpath); } errout_with_lock: @@ -1968,7 +1968,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); - kmm_free(dup); + lib_free(dup); return ret; } @@ -2654,7 +2654,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, errout_with_prefix1: if (ui->ui_fs[0].um_prefix != NULL) { - kmm_free(ui->ui_fs[0].um_prefix); + lib_free(ui->ui_fs[0].um_prefix); } errout_with_fs2: diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c index d7dfab2b03..dafc95af85 100644 --- a/fs/vfs/fs_rename.c +++ b/fs/vfs/fs_rename.c @@ -33,6 +33,7 @@ #include #include +#include #include "inode/inode.h" @@ -118,7 +119,7 @@ next_subdir: if (subdir != NULL) { - kmm_free(subdir); + lib_free(subdir); subdir = NULL; } @@ -351,7 +352,7 @@ next_subdir: if (subdir != NULL) { - kmm_free(subdir); + lib_free(subdir); subdir = NULL; } diff --git a/fs/vfs/fs_symlink.c b/fs/vfs/fs_symlink.c index e55067191b..0ad492488d 100644 --- a/fs/vfs/fs_symlink.c +++ b/fs/vfs/fs_symlink.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include "inode/inode.h" @@ -143,7 +143,7 @@ int symlink(FAR const char *path1, FAR const char *path2) ret = inode_lock(); if (ret < 0) { - kmm_free(newpath2); + lib_free(newpath2); errcode = -ret; goto errout_with_search; } @@ -153,7 +153,7 @@ int symlink(FAR const char *path1, FAR const char *path2) if (ret < 0) { - kmm_free(newpath2); + lib_free(newpath2); errcode = -ret; goto errout_with_search; } diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h index 6997fb527e..4ed6a01d16 100644 --- a/include/nuttx/lib/lib.h +++ b/include/nuttx/lib/lib.h @@ -41,7 +41,7 @@ * then only the first mode is supported. */ -#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) +#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) /* Domain-specific allocations */ diff --git a/libs/libc/netdb/lib_rexec.c b/libs/libc/netdb/lib_rexec.c index 00f69ce369..e6aa9a7955 100644 --- a/libs/libc/netdb/lib_rexec.c +++ b/libs/libc/netdb/lib_rexec.c @@ -31,6 +31,8 @@ #include #include +#include + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -162,7 +164,7 @@ int rexec_af(FAR char **ahost, int inport, FAR const char *user, conn_out: close(sock); sock_out: - free(*ahost); + lib_free(*ahost); addr_out: freeaddrinfo(res); return -1; diff --git a/net/procfs/net_procfs.c b/net/procfs/net_procfs.c index a59a36b364..65ec11f097 100644 --- a/net/procfs/net_procfs.c +++ b/net/procfs/net_procfs.c @@ -39,7 +39,7 @@ #include -#include +#include #include #include #include @@ -244,7 +244,7 @@ static int netprocfs_open(FAR struct file *filep, FAR const char *relpath, devname = basename(copy); dev = netdev_findbyname(devname); - kmm_free(copy); + lib_free(copy); if (dev == NULL) { @@ -670,7 +670,7 @@ static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf) devname = basename(copy); dev = netdev_findbyname(devname); - kmm_free(copy); + lib_free(copy); if (dev == NULL) { diff --git a/sched/environ/env_putenv.c b/sched/environ/env_putenv.c index 4061c7d86e..7ec3be4249 100644 --- a/sched/environ/env_putenv.c +++ b/sched/environ/env_putenv.c @@ -31,7 +31,7 @@ #include #include -#include +#include /**************************************************************************** * Public Functions @@ -61,8 +61,8 @@ int putenv(FAR const char *string) { - char *pname; - char *pequal; + FAR char *pname; + FAR char *pequal; int ret = OK; /* Verify that a string was passed */ @@ -91,7 +91,7 @@ int putenv(FAR const char *string) ret = setenv(pname, pequal + 1, TRUE); } - kmm_free(pname); + lib_free(pname); return ret; errout: