This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 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 ddef6bc84a1 fs: Do not read past size in fs_heap_strndup.
ddef6bc84a1 is described below
commit ddef6bc84a12c867d3739e103a0a010ec72969b8
Author: Shikha Bhadoria <[email protected]>
AuthorDate: Mon Nov 30 04:39:47 2026 +0000
fs: Do not read past size in fs_heap_strndup.
When s has no NUL in the first size bytes, memcpy of strnlen + 1
read s[size]. Copy only strnlen bytes and write the terminator,
matching nx_strndup.
Assisted-by: Cursor:Grok-4.6
Signed-off-by: Shikha Bhadoria <[email protected]>
---
fs/fs_heap.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/fs/fs_heap.c b/fs/fs_heap.c
index dc5df4fa693..43cbb25a27a 100644
--- a/fs/fs_heap.c
+++ b/fs/fs_heap.c
@@ -108,13 +108,13 @@ FAR char *fs_heap_strdup(FAR const char *s)
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);
+ size_t allocsize = strnlen(s, size);
+ FAR char *copy = fs_heap_malloc(allocsize + 1);
if (copy != NULL)
{
- memcpy(copy, s, len);
- copy[len - 1] = '\0';
+ memcpy(copy, s, allocsize);
+ copy[allocsize] = '\0';
}
return copy;