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

commit 1531ce9649c98221a0279c06b5742378c1f57773
Author: chao an <[email protected]>
AuthorDate: Mon Oct 26 14:29:12 2026 +0800

    libs/libc/string: Use memset() for strncpy padding
    
    Replace the byte-at-a-time zero padding at the end of strncpy() and
    stpncpy() with memset().
    
    These functions must pad the remaining destination buffer with NUL bytes
    when the source string ends before the requested length.  That remaining
    range is contiguous, so memset() directly expresses the operation and lets
    the C library use an optimized block fill for larger buffers.
    
    This keeps the existing return value semantics for both the generic and
    BSD string implementations.
    
    Signed-off-by: chao an <[email protected]>
---
 libs/libc/string/lib_bsdstpncpy.c | 5 +----
 libs/libc/string/lib_bsdstrncpy.c | 5 +----
 libs/libc/string/lib_stpncpy.c    | 5 +----
 libs/libc/string/lib_strncpy.c    | 5 +----
 4 files changed, 4 insertions(+), 16 deletions(-)

diff --git a/libs/libc/string/lib_bsdstpncpy.c 
b/libs/libc/string/lib_bsdstpncpy.c
index 0df9c108982..87a85b8066a 100644
--- a/libs/libc/string/lib_bsdstpncpy.c
+++ b/libs/libc/string/lib_bsdstpncpy.c
@@ -128,10 +128,7 @@ FAR char *stpncpy(FAR char *dest, FAR const char *src, 
size_t n)
         }
     }
 
-  while (n-- > 0)
-    {
-      *dest++ = '\0';
-    }
+  memset(dest, 0, n);
 
   return ret ? ret : dest;
 }
diff --git a/libs/libc/string/lib_bsdstrncpy.c 
b/libs/libc/string/lib_bsdstrncpy.c
index ce261b10f54..6eb74103539 100644
--- a/libs/libc/string/lib_bsdstrncpy.c
+++ b/libs/libc/string/lib_bsdstrncpy.c
@@ -127,10 +127,7 @@ FAR char *strncpy(FAR char *dest, FAR const char *src, 
size_t n)
         }
     }
 
-  while (n-- > 0)
-    {
-      *dst0++ = '\0';
-    }
+  memset(dst0, 0, n);
 
   return dest;
 }
diff --git a/libs/libc/string/lib_stpncpy.c b/libs/libc/string/lib_stpncpy.c
index 6e6ee45c692..b661dfb79f5 100644
--- a/libs/libc/string/lib_stpncpy.c
+++ b/libs/libc/string/lib_stpncpy.c
@@ -85,10 +85,7 @@ FAR char *stpncpy(FAR char *dest, FAR const char *src, 
size_t n)
    * overwrites any previously copied NUL terminator.
    */
 
-  while (dest != end)
-    {
-      *dest++ = '\0';
-    }
+  memset(dest, 0, end - dest);
 
   return ret;
 }
diff --git a/libs/libc/string/lib_strncpy.c b/libs/libc/string/lib_strncpy.c
index dc3af786ee7..4545de7af8e 100644
--- a/libs/libc/string/lib_strncpy.c
+++ b/libs/libc/string/lib_strncpy.c
@@ -74,10 +74,7 @@ FAR char *strncpy(FAR char *dest, FAR const char *src, 
size_t n)
 
   /* Pad the remainder of the array pointer to 'dest' with NULs */
 
-  while (dest != end)
-    {
-      *dest++ = '\0';
-    }
+  memset(dest, 0, end - dest);
 
   return ret;
 }

Reply via email to