From 28c14d475fbd16d07db88c8d12edddfe9cc226ab Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Thu, 12 Jun 2025 15:57:58 -0700 Subject: [PATCH] [libc] Independent strcat/strncat/stpcpy (#142643) The previous implementations called other entrypoints. This patch fixes strcat, strncat, and stpcpy to be properly independent. --- libc/src/string/CMakeLists.txt | 3 --- libc/src/string/stpcpy.cpp | 5 ++--- libc/src/string/strcat.cpp | 9 +++++---- libc/src/string/strncat.cpp | 10 +++++----- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt index c3b414d87285..8784bc3750cb 100644 --- a/libc/src/string/CMakeLists.txt +++ b/libc/src/string/CMakeLists.txt @@ -87,7 +87,6 @@ add_entrypoint_object( HDRS stpcpy.h DEPENDS - .mempcpy .string_utils ) @@ -108,7 +107,6 @@ add_entrypoint_object( HDRS strcat.h DEPENDS - .strcpy .string_utils libc.include.llvm-libc-types.size_t ) @@ -265,7 +263,6 @@ add_entrypoint_object( HDRS strncat.h DEPENDS - .strncpy .string_utils libc.include.llvm-libc-types.size_t ) diff --git a/libc/src/string/stpcpy.cpp b/libc/src/string/stpcpy.cpp index 979edd72c1f1..48c0db950ace 100644 --- a/libc/src/string/stpcpy.cpp +++ b/libc/src/string/stpcpy.cpp @@ -8,7 +8,6 @@ #include "src/string/stpcpy.h" #include "src/__support/macros/config.h" -#include "src/string/mempcpy.h" #include "src/string/string_utils.h" #include "src/__support/common.h" @@ -18,8 +17,8 @@ namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(char *, stpcpy, (char *__restrict dest, const char *__restrict src)) { size_t size = internal::string_length(src) + 1; - char *result = - reinterpret_cast(LIBC_NAMESPACE::mempcpy(dest, src, size)); + __builtin_memcpy(dest, src, size); + char *result = dest + size; if (result != nullptr) return result - 1; diff --git a/libc/src/string/strcat.cpp b/libc/src/string/strcat.cpp index 6a6f068bd475..7dce6d15a65c 100644 --- a/libc/src/string/strcat.cpp +++ b/libc/src/string/strcat.cpp @@ -9,7 +9,6 @@ #include "src/string/strcat.h" #include "src/__support/macros/config.h" #include "src/__support/macros/null_check.h" -#include "src/string/strcpy.h" #include "src/string/string_utils.h" #include "src/__support/common.h" @@ -21,9 +20,11 @@ LLVM_LIBC_FUNCTION(char *, strcat, LIBC_CRASH_ON_NULLPTR(dest); LIBC_CRASH_ON_NULLPTR(src); size_t dest_length = internal::string_length(dest); - size_t src_length = internal::string_length(src); - LIBC_NAMESPACE::strcpy(dest + dest_length, src); - dest[dest_length + src_length] = '\0'; + size_t i; + for (i = 0; src[i] != '\0'; ++i) + dest[dest_length + i] = src[i]; + + dest[dest_length + i] = '\0'; return dest; } diff --git a/libc/src/string/strncat.cpp b/libc/src/string/strncat.cpp index 4926b7d244d1..6d8bb6960748 100644 --- a/libc/src/string/strncat.cpp +++ b/libc/src/string/strncat.cpp @@ -10,7 +10,6 @@ #include "src/__support/macros/config.h" #include "src/__support/macros/null_check.h" #include "src/string/string_utils.h" -#include "src/string/strncpy.h" #include "src/__support/common.h" @@ -23,11 +22,12 @@ LLVM_LIBC_FUNCTION(char *, strncat, LIBC_CRASH_ON_NULLPTR(dest); LIBC_CRASH_ON_NULLPTR(src); } - size_t src_length = internal::string_length(src); - size_t copy_amount = src_length > count ? count : src_length; size_t dest_length = internal::string_length(dest); - LIBC_NAMESPACE::strncpy(dest + dest_length, src, copy_amount); - dest[dest_length + copy_amount] = '\0'; + size_t i; + for (i = 0; i < count && src[i] != '\0'; ++i) + dest[dest_length + i] = src[i]; + + dest[dest_length + i] = '\0'; return dest; }