This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new c3f3b338c9 fs: add fs_heap_strndup, replace strndup with 
fs_heap_strndup
c3f3b338c9 is described below

commit c3f3b338c9933361bced0242e064cf0032f3b4c9
Author: yinshengkai <[email protected]>
AuthorDate: Tue Oct 22 11:10:01 2024 +0800

    fs: add fs_heap_strndup, replace strndup with fs_heap_strndup
    
    Signed-off-by: yinshengkai <[email protected]>
---
 fs/fs_heap.c        | 13 +++++++++++++
 fs/fs_heap.h        |  2 ++
 fs/tmpfs/fs_tmpfs.c |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/fs/fs_heap.c b/fs/fs_heap.c
index 78b0bcece0..bb8a9ef966 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 d569a18c76..fa22370eee 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 286bd21372..152272fd94 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;

Reply via email to