diff --git a/fs/fs_heap.c b/fs/fs_heap.c index 78b0bcece02a2..bb8a9ef966431 100644 --- a/fs/fs_heap.c +++ b/fs/fs_heap.c @@ -85,6 +85,19 @@ FAR char *fs_heap_strdup(FAR const char *s) return copy; } +FAR char *fs_heap_strndup(FAR const char *s, size_t size) +{ + size_t len = strnlen(s, size) + 1; + FAR char *copy = fs_heap_malloc(len); + if (copy != NULL) + { + memcpy(copy, s, len); + copy[len - 1] = '\0'; + } + + return copy; +} + int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...) { va_list ap; diff --git a/fs/fs_heap.h b/fs/fs_heap.h index d569a18c76abb..fa22370eee429 100644 --- a/fs/fs_heap.h +++ b/fs/fs_heap.h @@ -45,6 +45,7 @@ size_t fs_heap_malloc_size(FAR void *mem); 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; +FAR char *fs_heap_strndup(FAR const char *s, size_t size) malloc_like; int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...) printf_like(2, 3); #else @@ -55,6 +56,7 @@ int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...) # define fs_heap_realloc kmm_realloc # define fs_heap_free kmm_free # define fs_heap_strdup strdup +# define fs_heap_strndup strndup # define fs_heap_asprintf asprintf #endif diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c index 286bd2137241b..152272fd94529 100644 --- a/fs/tmpfs/fs_tmpfs.c +++ b/fs/tmpfs/fs_tmpfs.c @@ -521,7 +521,7 @@ static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo, } } - newname = strndup(name, namelen); + newname = fs_heap_strndup(name, namelen); if (newname == NULL) { return -ENOMEM;