git: 588838aa2484 - stable/14 - libkern: strdup.c, strndup.c: Prefer memcpy() over bcopy()

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Mon, 03 Mar 2025 14:58:05 UTC
The branch stable/14 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=588838aa2484619c93231aedb22c24e761d244da

commit 588838aa2484619c93231aedb22c24e761d244da
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2025-02-17 15:37:59 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2025-03-03 14:56:34 +0000

    libkern: strdup.c, strndup.c: Prefer memcpy() over bcopy()
    
    The newly allocated memory can not overlap with the string if the string
    is properly null-terminated or the maxlen is a valid lengh, i.e no out
    of bounds reads. Prefer memcpy() over memmove(), aka bcopy(), for slight
    performance gain.
    
    No functional change intended.
    
    Reviewed by:    kib
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D49026
    
    (cherry picked from commit 90a3b6a7a4e4c8201ba1bb90fb8e2b088f3a5788)
---
 sys/libkern/strdup.c  | 2 +-
 sys/libkern/strndup.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys/libkern/strdup.c b/sys/libkern/strdup.c
index f5685fcc554f..38f5c8f194c3 100644
--- a/sys/libkern/strdup.c
+++ b/sys/libkern/strdup.c
@@ -47,7 +47,7 @@ strdup_flags(const char *string, struct malloc_type *type, int flags)
 	copy = malloc(len, type, flags);
 	if (copy == NULL)
 		return (NULL);
-	bcopy(string, copy, len);
+	memcpy(copy, string, len);
 	return (copy);
 }
 
diff --git a/sys/libkern/strndup.c b/sys/libkern/strndup.c
index f8867c140e67..9065153d7232 100644
--- a/sys/libkern/strndup.c
+++ b/sys/libkern/strndup.c
@@ -43,7 +43,7 @@ strndup(const char *string, size_t maxlen, struct malloc_type *type)
 
 	len = strnlen(string, maxlen) + 1;
 	copy = malloc(len, type, M_WAITOK);
-	bcopy(string, copy, len);
+	memcpy(copy, string, len);
 	copy[len - 1] = '\0';
 	return (copy);
 }