Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions arch/risc-v/src/common/espressif/esp_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stdio.h>

#include <nuttx/timers/timer.h>
#include <nuttx/lib/lib.h>

#include "esp_irq.h"
#include "esp_timer.h"
Expand Down Expand Up @@ -508,7 +509,7 @@ IRAM_ATTR static int esp_timer_isr(int irq, void *context, void *arg)
int esp_timer_initialize(uint32_t timer_id)
{
struct esp_timer_lowerhalf_s *lower = NULL;
char devpath[PATH_MAX];
FAR char *devpath;
uint32_t group_num;
uint32_t timer_num;

Expand Down Expand Up @@ -539,7 +540,13 @@ int esp_timer_initialize(uint32_t timer_id)
break;
}

snprintf(devpath, sizeof(devpath), "/dev/timer%" PRIu32, timer_id);
devpath = lib_get_pathbuffer();

@anchao anchao Nov 4, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since CONFIG_PATH_MAX can define a smaller size. let us add a configure CONFIG_LIBC_PATHBUFFER to enable/disable lib_get_pathbuffer implement.

#ifdef CONFIG_LIBC_PATHBUFFER
FAR char *lib_get_pathbuffer(void);
#else
#  define lib_get_pathbuffer() alloca(PATH_MAX)
#endif

@xiaoxiang781216 xiaoxiang781216 Nov 4, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alloca isn't ANSI or POSIX Api, so it isn't available on all possible compiler, and should disable by default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current code base:
https://github.com/apache/nuttx/blob/master/include/string.h#L40-L51
image

I think most toolchains support this feature (even MSVC has a similar implementation)
https://github.com/apache/nuttx/blob/master/include/alloca.h#L30-L42

if (devpath == NULL)
{
return -ENOMEM;
}

snprintf(devpath, PATH_MAX, "/dev/timer%" PRIu32, timer_id);

/* Initialize the elements of lower half state structure */

Expand All @@ -562,9 +569,11 @@ int esp_timer_initialize(uint32_t timer_id)
* indicate the failure (implying the non-unique devpath).
*/

lib_put_pathbuffer(devpath);
return -EEXIST;
}

lib_put_pathbuffer(devpath);
esp_setup_irq(lower->source,
ESP_IRQ_PRIORITY_DEFAULT,
ESP_IRQ_TRIGGER_LEVEL);
Expand Down
12 changes: 10 additions & 2 deletions drivers/input/aw86225.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <nuttx/input/aw86225.h>
#include <nuttx/input/ff.h>
#include <nuttx/irq.h>
#include <nuttx/lib/lib.h>

#include "aw86225_reg.h"
#include "aw86225_internal.h"
Expand Down Expand Up @@ -367,14 +368,21 @@ static int aw86225_i2c_write_bits(FAR struct aw86225 *aw86225,
static int aw86225_request_firmware(FAR struct aw86225_firmware *fw,
FAR const char *filename)
{
char file_path[PATH_MAX];
FAR char *file_path;
struct file file;
size_t file_size;
int ret;

snprintf(file_path, sizeof(file_path), "%s/%s", CONFIG_FF_RTP_FILE_PATH,
file_path = lib_get_pathbuffer();
if (file_path == NULL)
{
return -ENOMEM;
}

snprintf(file_path, PATH_MAX, "%s/%s", CONFIG_FF_RTP_FILE_PATH,
filename);
ret = file_open(&file, file_path, O_RDONLY);
lib_put_pathbuffer(file_path);
if (ret < 0)
{
ierr("open file failed");
Expand Down
16 changes: 13 additions & 3 deletions drivers/mtd/dhara.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <nuttx/nuttx.h>
#include <nuttx/kmalloc.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/lib/lib.h>

#include <dhara/map.h>
#include <dhara/nand.h>
Expand Down Expand Up @@ -783,7 +784,8 @@ int dhara_initialize_by_path(FAR const char *path,

int dhara_initialize(int minor, FAR struct mtd_dev_s *mtd)
{
char path[PATH_MAX];
FAR char *path;
int ret;

#ifdef CONFIG_DEBUG_FEATURES
/* Sanity check */
Expand All @@ -794,8 +796,16 @@ int dhara_initialize(int minor, FAR struct mtd_dev_s *mtd)
}
#endif

path = lib_get_pathbuffer();
if (path == NULL)
{
return -ENOMEM;
}

/* Do the real work by dhara_mtdblock_initialize_by_path */

snprintf(path, sizeof(path), "/dev/mtdblock%d", minor);
return dhara_initialize_by_path(path, mtd);
snprintf(path, PATH_MAX, "/dev/mtdblock%d", minor);
ret = dhara_initialize_by_path(path, mtd);
lib_put_pathbuffer(path);
return ret;
}
21 changes: 19 additions & 2 deletions drivers/sensors/gnss_uorb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <nuttx/circbuf.h>
#include <nuttx/sensors/sensor.h>
#include <nuttx/sensors/gnss.h>
#include <nuttx/lib/lib.h>

#include <fcntl.h>
#include <poll.h>
Expand Down Expand Up @@ -713,7 +714,7 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
{
FAR struct gnss_upperhalf_s *upper;
FAR struct gnss_sensor_s *dev;
char path[PATH_MAX];
FAR char *path;
int ret;

upper = kmm_zalloc(sizeof(struct gnss_upperhalf_s));
Expand All @@ -722,6 +723,13 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
return -ENOMEM;
}

path = lib_get_pathbuffer();
if (path == NULL)
{
kmm_free(upper);
return -ENOMEM;
}

lower->push_data = gnss_push_data;
lower->push_event = gnss_push_event;
lower->priv = upper;
Expand Down Expand Up @@ -812,6 +820,7 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
goto driver_err;
}

lib_put_pathbuffer(path);
return ret;

driver_err:
Expand All @@ -830,6 +839,7 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
nxmutex_destroy(&upper->lock);
nxmutex_destroy(&upper->bufferlock);
nxsem_destroy(&upper->buffersem);
lib_put_pathbuffer(path);
kmm_free(upper);
return ret;
}
Expand All @@ -851,7 +861,13 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
void gnss_unregister(FAR struct gnss_lowerhalf_s *lower, int devno)
{
FAR struct gnss_upperhalf_s *upper = lower->priv;
char path[PATH_MAX];
FAR char *path;

path = lib_get_pathbuffer();
if (path == NULL)
{
return;
}

sensor_unregister(&upper->dev[GNSS_IDX].lower, devno);
sensor_unregister(&upper->dev[GNSS_SATELLITE_IDX].lower, devno);
Expand All @@ -862,5 +878,6 @@ void gnss_unregister(FAR struct gnss_lowerhalf_s *lower, int devno)
unregister_driver(path);
nxsem_destroy(&upper->buffersem);
circbuf_uninit(&upper->buffer);
lib_put_pathbuffer(path);
kmm_free(upper);
}
25 changes: 21 additions & 4 deletions drivers/sensors/sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <nuttx/circbuf.h>
#include <nuttx/mutex.h>
#include <nuttx/sensors/sensor.h>
#include <nuttx/lib/lib.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -1238,16 +1239,25 @@ void sensor_remap_vector_raw16(FAR const int16_t *in, FAR int16_t *out,

int sensor_register(FAR struct sensor_lowerhalf_s *lower, int devno)
{
char path[PATH_MAX];
FAR char *path;
int ret;

DEBUGASSERT(lower != NULL);

path = lib_get_pathbuffer();
if (path == NULL)
{
return -ENOMEM;
}

snprintf(path, PATH_MAX, DEVNAME_FMT,
g_sensor_meta[lower->type].name,
lower->uncalibrated ? DEVNAME_UNCAL : "",
devno);
return sensor_custom_register(lower, path,
g_sensor_meta[lower->type].esize);
ret = sensor_custom_register(lower, path,
g_sensor_meta[lower->type].esize);
lib_put_pathbuffer(path);
return ret;
}

/****************************************************************************
Expand Down Expand Up @@ -1379,13 +1389,20 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,

void sensor_unregister(FAR struct sensor_lowerhalf_s *lower, int devno)
{
char path[PATH_MAX];
FAR char *path;

path = lib_get_pathbuffer();
if (path == NULL)
{
return;
}

snprintf(path, PATH_MAX, DEVNAME_FMT,
g_sensor_meta[lower->type].name,
lower->uncalibrated ? DEVNAME_UNCAL : "",
devno);
sensor_custom_unregister(lower, path);
lib_put_pathbuffer(path);
}

/****************************************************************************
Expand Down
11 changes: 10 additions & 1 deletion fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <nuttx/sched.h>
#include <nuttx/spawn.h>
#include <nuttx/spinlock.h>
#include <nuttx/lib/lib.h>

#ifdef CONFIG_FDSAN
# include <android/fdsan.h>
Expand Down Expand Up @@ -381,6 +382,7 @@ void files_initlist(FAR struct filelist *list)
#ifdef CONFIG_SCHED_DUMP_ON_EXIT
void files_dumplist(FAR struct filelist *list)
{
FAR char *path;
int count = files_countlist(list);
int i;

Expand All @@ -392,10 +394,15 @@ void files_dumplist(FAR struct filelist *list)
"PID", "FD", "FLAGS", "TYPE", "POS", "PATH"
);

path = lib_get_pathbuffer();
if (path == NULL)
{
return;
}

for (i = 0; i < count; i++)
{
FAR struct file *filep = files_fget(list, i);
char path[PATH_MAX];

#if CONFIG_FS_BACKTRACE > 0
char buf[BACKTRACE_BUFFER_SIZE(CONFIG_FS_BACKTRACE)];
Expand Down Expand Up @@ -431,6 +438,8 @@ void files_dumplist(FAR struct filelist *list)
);
fs_putfilep(filep);
}

lib_put_pathbuffer(path);
}
#endif

Expand Down
19 changes: 15 additions & 4 deletions fs/mount/fs_automount.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/wqueue.h>
#include <nuttx/fs/automount.h>
#include <nuttx/lib/lib.h>

#ifdef CONFIG_FS_AUTOMOUNTER_DRIVER
# include <stdio.h>
Expand Down Expand Up @@ -807,7 +808,11 @@ FAR void *automount_initialize(FAR const struct automount_lower_s *lower)
FAR struct automounter_state_s *priv;
int ret;
#ifdef CONFIG_FS_AUTOMOUNTER_DRIVER
char devpath[PATH_MAX];
FAR char *devpath = lib_get_pathbuffer();
if (devpath == NULL)
{
return;
}
#endif /* CONFIG_FS_AUTOMOUNTER_DRIVER */

finfo("lower=%p\n", lower);
Expand Down Expand Up @@ -851,10 +856,11 @@ FAR void *automount_initialize(FAR const struct automount_lower_s *lower)

/* Register driver */

snprintf(devpath, sizeof(devpath),
snprintf(devpath, PATH_MAX,
CONFIG_FS_AUTOMOUNTER_VFS_PATH "%s", lower->mountpoint);

ret = register_driver(devpath, &g_automount_fops, 0444, priv);
lib_put_pathbuffer(devpath);
if (ret < 0)
{
ferr("ERROR: Failed to register automount driver: %d\n", ret);
Expand Down Expand Up @@ -911,12 +917,17 @@ void automount_uninitialize(FAR void *handle)
#ifdef CONFIG_FS_AUTOMOUNTER_DRIVER
if (priv->registered)
{
char devpath[PATH_MAX];
FAR char *devpath = lib_get_pathbuffer();
if (devpath == NULL)
{
return;
}

snprintf(devpath, sizeof(devpath),
snprintf(devpath, PATH_MAX,
CONFIG_FS_AUTOMOUNTER_VFS_PATH "%s", lower->mountpoint);

unregister_driver(devpath);
lib_put_pathbuffer(devpath);
}

nxmutex_destroy(&priv->lock);
Expand Down
12 changes: 10 additions & 2 deletions fs/partition/fs_partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ static void register_partition(FAR struct partition_s *part, FAR void *arg)
{
FAR struct partition_register_s *reg = arg;
FAR struct partition_state_s *state = reg->state;
char path[PATH_MAX];
FAR char *path;

snprintf(path, sizeof(path), "%s/%s", reg->dir, part->name);
path = lib_get_pathbuffer();
if (path == NULL)
{
return;
}

snprintf(path, PATH_MAX, "%s/%s", reg->dir, part->name);
if (state->blk != NULL)
{
register_partition_with_inode(path, 0660, state->blk,
Expand All @@ -106,6 +112,8 @@ static void register_partition(FAR struct partition_s *part, FAR void *arg)
part->firstblock, part->nblocks);
}
#endif

lib_put_pathbuffer(path);
}
}

Expand Down
Loading