Skip to content

[libc] Independent strcat/strncat/stpcpy #142643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions libc/src/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ add_entrypoint_object(
HDRS
stpcpy.h
DEPENDS
.mempcpy
.string_utils
)

Expand All @@ -108,7 +107,6 @@ add_entrypoint_object(
HDRS
strcat.h
DEPENDS
.strcpy
.string_utils
libc.include.llvm-libc-types.size_t
)
Expand Down Expand Up @@ -265,7 +263,6 @@ add_entrypoint_object(
HDRS
strncat.h
DEPENDS
.strncpy
.string_utils
libc.include.llvm-libc-types.size_t
)
Expand Down
5 changes: 2 additions & 3 deletions libc/src/string/stpcpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<char *>(LIBC_NAMESPACE::mempcpy(dest, src, size));
__builtin_memcpy(dest, src, size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a definition for this in libc/src/string/memory_utils/generic/builtin.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to call that version instead of calling the builtin directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly was wondering if there would be an easier way to select which flavor of memcpy we wanted. Might've been better as a template function than this macro dispatch but that's a big change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be interested in making memcpy specialization template controlled, though to keep the includes for different architectures separate we would probably still need some amount of macro control. I think that's probably a topic for a separate PR though.

char *result = dest + size;

if (result != nullptr)
return result - 1;
Expand Down
9 changes: 5 additions & 4 deletions libc/src/string/strcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions libc/src/string/strncat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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;
}

Expand Down
Loading