Skip to content

Commit

Permalink
fs: add fs_heap_strndup, replace strndup with fs_heap_strndup
Browse files Browse the repository at this point in the history
Signed-off-by: yinshengkai <[email protected]>
  • Loading branch information
Gary-Hobson authored and xiaoxiang781216 committed Oct 22, 2024
1 parent 824dfac commit c3f3b33
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions fs/fs_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions fs/fs_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fs/tmpfs/fs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c3f3b33

Please sign in to comment.