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 <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2023-08-08 01:11:20 +03:00 committed by Tiago Medicci Serrano
parent b3d620152e
commit 1b0baa8337
18 changed files with 77 additions and 74 deletions

View File

@ -35,7 +35,7 @@
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
#include <nuttx/efuse/efuse.h> #include <nuttx/efuse/efuse.h>
@ -311,7 +311,7 @@ FAR void *efuse_register(FAR const char *path,
return (FAR void *)upper; return (FAR void *)upper;
errout_with_path: errout_with_path:
kmm_free(upper->path); lib_free(upper->path);
errout_with_upper: errout_with_upper:
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
@ -356,7 +356,7 @@ void efuse_unregister(FAR void *handle)
/* Then free all of the driver resources */ /* Then free all of the driver resources */
kmm_free(upper->path); lib_free(upper->path);
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
kmm_free(upper); kmm_free(upper);
} }

View File

@ -50,7 +50,7 @@
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h> #include <nuttx/fs/ioctl.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/modem/u-blox.h> #include <nuttx/modem/u-blox.h>
/**************************************************************************** /****************************************************************************
@ -77,30 +77,30 @@
struct ubxmdm_upper struct ubxmdm_upper
{ {
FAR char * path; /* Registration path */ FAR char *path; /* Registration path */
/* The contained lower-half driver. */ /* The contained lower-half driver. */
FAR struct ubxmdm_lower * lower; FAR struct ubxmdm_lower *lower;
}; };
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
****************************************************************************/ ****************************************************************************/
static ssize_t ubxmdm_read (FAR struct file * filep, static ssize_t ubxmdm_read(FAR struct file *filep,
FAR char * buffer, FAR char *buffer,
size_t buflen);
static ssize_t ubxmdm_write(FAR struct file *filep,
FAR const char *buffer,
size_t buflen); size_t buflen);
static ssize_t ubxmdm_write(FAR struct file * filep, static int ubxmdm_ioctl(FAR struct file *filep,
FAR const char * buffer,
size_t buflen);
static int ubxmdm_ioctl(FAR struct file * filep,
int cmd, int cmd,
unsigned long arg); unsigned long arg);
static int ubxmdm_poll (FAR struct file * filep, static int ubxmdm_poll(FAR struct file *filep,
FAR struct pollfd * fds, FAR struct pollfd *fds,
bool setup); bool setup);
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@ -123,29 +123,29 @@ static const struct file_operations g_ubxmdm_fops =
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
static ssize_t ubxmdm_read(FAR struct file * filep, static ssize_t ubxmdm_read(FAR struct file *filep,
FAR char * buffer, FAR char *buffer,
size_t len) size_t len)
{ {
return 0; /* Return EOF */ return 0; /* Return EOF */
} }
static ssize_t ubxmdm_write(FAR struct file * filep, static ssize_t ubxmdm_write(FAR struct file *filep,
FAR const char * buffer, FAR const char *buffer,
size_t len) size_t len)
{ {
return len; /* Say that everything was written */ 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, int cmd,
unsigned long arg) unsigned long arg)
{ {
FAR struct inode * inode = filep->f_inode; FAR struct inode *inode = filep->f_inode;
FAR struct ubxmdm_upper * upper; FAR struct ubxmdm_upper *upper;
FAR struct ubxmdm_lower * lower; FAR struct ubxmdm_lower *lower;
int ret; FAR struct ubxmdm_status *status;
FAR struct ubxmdm_status * status; int ret;
m_info("cmd: %d arg: %ld\n", cmd, arg); m_info("cmd: %d arg: %ld\n", cmd, arg);
upper = inode->i_private; upper = inode->i_private;
@ -214,7 +214,7 @@ static int ubxmdm_ioctl(FAR struct file * filep,
case MODEM_IOC_GETSTATUS: case MODEM_IOC_GETSTATUS:
if (lower->ops->getstatus) if (lower->ops->getstatus)
{ {
status = (FAR struct ubxmdm_status *) ((uintptr_t) arg); status = (FAR struct ubxmdm_status *)((uintptr_t)arg);
if (status) if (status)
{ {
ret = lower->ops->getstatus(lower, status); ret = lower->ops->getstatus(lower, status);
@ -253,8 +253,8 @@ static int ubxmdm_ioctl(FAR struct file * filep,
return ret; return ret;
} }
static int ubxmdm_poll(FAR struct file * filep, static int ubxmdm_poll(FAR struct file *filep,
FAR struct pollfd * fds, FAR struct pollfd *fds,
bool setup) bool setup)
{ {
if (setup) if (setup)
@ -269,8 +269,8 @@ static int ubxmdm_poll(FAR struct file * filep,
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
FAR void * ubxmdm_register(FAR const char * path, FAR void *ubxmdm_register(FAR const char *path,
FAR struct ubxmdm_lower * lower) FAR struct ubxmdm_lower *lower)
{ {
FAR struct ubxmdm_upper *upper; FAR struct ubxmdm_upper *upper;
int ret; int ret;
@ -300,10 +300,10 @@ FAR void * ubxmdm_register(FAR const char * path,
goto errout_with_path; goto errout_with_path;
} }
return (FAR void *) upper; return (FAR void *)upper;
errout_with_path: errout_with_path:
kmm_free(upper->path); lib_free(upper->path);
errout_with_upper: errout_with_upper:
kmm_free(upper); kmm_free(upper);
@ -329,6 +329,6 @@ void ubxmdm_unregister(FAR void *handle)
unregister_driver(upper->path); unregister_driver(upper->path);
kmm_free(upper->path); lib_free(upper->path);
kmm_free(upper); kmm_free(upper);
} }

View File

@ -36,7 +36,7 @@
#include <assert.h> #include <assert.h>
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
#include <nuttx/syslog/syslog.h> #include <nuttx/syslog/syslog.h>
@ -781,7 +781,7 @@ void syslog_dev_uninitialize(FAR struct syslog_channel_s *channel)
if (syslog_dev->sl_devpath != NULL) if (syslog_dev->sl_devpath != NULL)
{ {
kmm_free(syslog_dev->sl_devpath); lib_free(syslog_dev->sl_devpath);
} }
/* Free the channel structure */ /* Free the channel structure */

View File

@ -34,7 +34,7 @@
#include <debug.h> #include <debug.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/signal.h> #include <nuttx/signal.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -473,7 +473,7 @@ FAR void *timer_register(FAR const char *path,
return (FAR void *)upper; return (FAR void *)upper;
errout_with_path: errout_with_path:
kmm_free(upper->path); lib_free(upper->path);
errout_with_upper: errout_with_upper:
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
@ -523,7 +523,7 @@ void timer_unregister(FAR void *handle)
/* Then free all of the driver resources */ /* Then free all of the driver resources */
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
kmm_free(upper->path); lib_free(upper->path);
kmm_free(upper); kmm_free(upper);
} }

View File

@ -35,7 +35,7 @@
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/panic_notifier.h> #include <nuttx/panic_notifier.h>
#include <nuttx/power/pm.h> #include <nuttx/power/pm.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -790,7 +790,7 @@ FAR void *watchdog_register(FAR const char *path,
return (FAR void *)upper; return (FAR void *)upper;
errout_with_path: errout_with_path:
kmm_free(upper->path); lib_free(upper->path);
errout_with_upper: errout_with_upper:
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
@ -847,7 +847,7 @@ void watchdog_unregister(FAR void *handle)
/* Then free all of the driver resources */ /* Then free all of the driver resources */
kmm_free(upper->path); lib_free(upper->path);
nxmutex_destroy(&upper->lock); nxmutex_destroy(&upper->lock);
kmm_free(upper); kmm_free(upper);
} }

View File

@ -36,7 +36,7 @@
#include <assert.h> #include <assert.h>
#include <debug.h> #include <debug.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/drivers/drivers.h> #include <nuttx/drivers/drivers.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -197,14 +197,14 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
/* Free the allocated character driver name. */ /* Free the allocated character driver name. */
kmm_free(chardev); lib_free(chardev);
return OK; return OK;
errout_with_bchdev: errout_with_bchdev:
nx_unlink(chardev); nx_unlink(chardev);
errout_with_chardev: errout_with_chardev:
kmm_free(chardev); lib_free(chardev);
return ret; return ret;
} }

View File

@ -33,7 +33,7 @@
#include <assert.h> #include <assert.h>
#include <debug.h> #include <debug.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/mtd/mtd.h> #include <nuttx/mtd/mtd.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -187,6 +187,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
out_with_fltdev: out_with_fltdev:
nx_unlink(blkdev); nx_unlink(blkdev);
out_with_blkdev: out_with_blkdev:
kmm_free(blkdev); lib_free(blkdev);
return ret; return ret;
} }

View File

@ -36,7 +36,7 @@
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/fs/fat.h> #include <nuttx/fs/fat.h>
@ -1048,7 +1048,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
ptr = strtok_r(NULL, ",", &saveptr); ptr = strtok_r(NULL, ",", &saveptr);
} }
kmm_free(options); lib_free(options);
/* Take the lock for the mount */ /* Take the lock for the mount */

View File

@ -31,7 +31,7 @@
#include <debug.h> #include <debug.h>
#include <nuttx/crc32.h> #include <nuttx/crc32.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/mtd/mtd.h> #include <nuttx/mtd/mtd.h>
#include "nxffs.h" #include "nxffs.h"
@ -212,7 +212,7 @@ void nxffs_freeentry(FAR struct nxffs_entry_s *entry)
{ {
if (entry->name) if (entry->name)
{ {
kmm_free(entry->name); lib_free(entry->name);
entry->name = NULL; entry->name = NULL;
} }
} }

View File

@ -33,7 +33,7 @@
#include <debug.h> #include <debug.h>
#include <nuttx/crc32.h> #include <nuttx/crc32.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/mtd/mtd.h> #include <nuttx/mtd/mtd.h>
@ -654,7 +654,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
return OK; return OK;
errout_with_name: errout_with_name:
kmm_free(wrfile->ofile.entry.name); lib_free(wrfile->ofile.entry.name);
errout_with_ofile: errout_with_ofile:
#ifndef CONFIG_NXFFS_PREALLOCATED #ifndef CONFIG_NXFFS_PREALLOCATED
kmm_free(wrfile); kmm_free(wrfile);

View File

@ -36,7 +36,7 @@
#include <debug.h> #include <debug.h>
#include <limits.h> #include <limits.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h> #include <nuttx/fs/ioctl.h>
@ -1108,7 +1108,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
} }
ret = rpmsgfs_client_bind(&fs->handle, cpuname); ret = rpmsgfs_client_bind(&fs->handle, cpuname);
kmm_free(options); lib_free(options);
if (ret < 0) if (ret < 0)
{ {
kmm_free(fs); kmm_free(fs);

View File

@ -40,7 +40,7 @@
#include <fixedmath.h> #include <fixedmath.h>
#include <debug.h> #include <debug.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h> #include <nuttx/fs/ioctl.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -824,12 +824,12 @@ static void unionfs_destroy(FAR struct unionfs_inode_s *ui)
if (ui->ui_fs[0].um_prefix) 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) 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 */ /* 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: errout_with_relpath:
if (udir->fu_relpath != NULL) if (udir->fu_relpath != NULL)
{ {
kmm_free(udir->fu_relpath); lib_free(udir->fu_relpath);
} }
errout_with_lock: 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. */ /* Call unionfs_dobind to do the real work. */
ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2, handle); ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2, handle);
kmm_free(dup); lib_free(dup);
return ret; return ret;
} }
@ -2654,7 +2654,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1,
errout_with_prefix1: errout_with_prefix1:
if (ui->ui_fs[0].um_prefix != NULL) 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: errout_with_fs2:

View File

@ -33,6 +33,7 @@
#include <errno.h> #include <errno.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h>
#include "inode/inode.h" #include "inode/inode.h"
@ -118,7 +119,7 @@ next_subdir:
if (subdir != NULL) if (subdir != NULL)
{ {
kmm_free(subdir); lib_free(subdir);
subdir = NULL; subdir = NULL;
} }
@ -351,7 +352,7 @@ next_subdir:
if (subdir != NULL) if (subdir != NULL)
{ {
kmm_free(subdir); lib_free(subdir);
subdir = NULL; subdir = NULL;
} }

View File

@ -32,7 +32,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include "inode/inode.h" #include "inode/inode.h"
@ -143,7 +143,7 @@ int symlink(FAR const char *path1, FAR const char *path2)
ret = inode_lock(); ret = inode_lock();
if (ret < 0) if (ret < 0)
{ {
kmm_free(newpath2); lib_free(newpath2);
errcode = -ret; errcode = -ret;
goto errout_with_search; goto errout_with_search;
} }
@ -153,7 +153,7 @@ int symlink(FAR const char *path1, FAR const char *path2)
if (ret < 0) if (ret < 0)
{ {
kmm_free(newpath2); lib_free(newpath2);
errcode = -ret; errcode = -ret;
goto errout_with_search; goto errout_with_search;
} }

View File

@ -41,7 +41,7 @@
* then only the first mode is supported. * then only the first mode is supported.
*/ */
#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__) #if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
/* Domain-specific allocations */ /* Domain-specific allocations */

View File

@ -31,6 +31,8 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <nuttx/lib/lib.h>
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -162,7 +164,7 @@ int rexec_af(FAR char **ahost, int inport, FAR const char *user,
conn_out: conn_out:
close(sock); close(sock);
sock_out: sock_out:
free(*ahost); lib_free(*ahost);
addr_out: addr_out:
freeaddrinfo(res); freeaddrinfo(res);
return -1; return -1;

View File

@ -39,7 +39,7 @@
#include <sys/param.h> #include <sys/param.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h> #include <nuttx/fs/procfs.h>
#include <nuttx/net/netdev.h> #include <nuttx/net/netdev.h>
@ -244,7 +244,7 @@ static int netprocfs_open(FAR struct file *filep, FAR const char *relpath,
devname = basename(copy); devname = basename(copy);
dev = netdev_findbyname(devname); dev = netdev_findbyname(devname);
kmm_free(copy); lib_free(copy);
if (dev == NULL) if (dev == NULL)
{ {
@ -670,7 +670,7 @@ static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf)
devname = basename(copy); devname = basename(copy);
dev = netdev_findbyname(devname); dev = netdev_findbyname(devname);
kmm_free(copy); lib_free(copy);
if (dev == NULL) if (dev == NULL)
{ {

View File

@ -31,7 +31,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <nuttx/kmalloc.h> #include <nuttx/lib/lib.h>
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
@ -61,8 +61,8 @@
int putenv(FAR const char *string) int putenv(FAR const char *string)
{ {
char *pname; FAR char *pname;
char *pequal; FAR char *pequal;
int ret = OK; int ret = OK;
/* Verify that a string was passed */ /* Verify that a string was passed */
@ -91,7 +91,7 @@ int putenv(FAR const char *string)
ret = setenv(pname, pequal + 1, TRUE); ret = setenv(pname, pequal + 1, TRUE);
} }
kmm_free(pname); lib_free(pname);
return ret; return ret;
errout: errout: