Skip to content

Commit 28c14d4

Browse files
[libc] Independent strcat/strncat/stpcpy (llvm#142643)
The previous implementations called other entrypoints. This patch fixes strcat, strncat, and stpcpy to be properly independent.
1 parent 703e446 commit 28c14d4

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

libc/src/string/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ add_entrypoint_object(
8787
HDRS
8888
stpcpy.h
8989
DEPENDS
90-
.mempcpy
9190
.string_utils
9291
)
9392

@@ -108,7 +107,6 @@ add_entrypoint_object(
108107
HDRS
109108
strcat.h
110109
DEPENDS
111-
.strcpy
112110
.string_utils
113111
libc.include.llvm-libc-types.size_t
114112
)
@@ -265,7 +263,6 @@ add_entrypoint_object(
265263
HDRS
266264
strncat.h
267265
DEPENDS
268-
.strncpy
269266
.string_utils
270267
libc.include.llvm-libc-types.size_t
271268
)

libc/src/string/stpcpy.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "src/string/stpcpy.h"
1010
#include "src/__support/macros/config.h"
11-
#include "src/string/mempcpy.h"
1211
#include "src/string/string_utils.h"
1312

1413
#include "src/__support/common.h"
@@ -18,8 +17,8 @@ namespace LIBC_NAMESPACE_DECL {
1817
LLVM_LIBC_FUNCTION(char *, stpcpy,
1918
(char *__restrict dest, const char *__restrict src)) {
2019
size_t size = internal::string_length(src) + 1;
21-
char *result =
22-
reinterpret_cast<char *>(LIBC_NAMESPACE::mempcpy(dest, src, size));
20+
__builtin_memcpy(dest, src, size);
21+
char *result = dest + size;
2322

2423
if (result != nullptr)
2524
return result - 1;

libc/src/string/strcat.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "src/string/strcat.h"
1010
#include "src/__support/macros/config.h"
1111
#include "src/__support/macros/null_check.h"
12-
#include "src/string/strcpy.h"
1312
#include "src/string/string_utils.h"
1413

1514
#include "src/__support/common.h"
@@ -21,9 +20,11 @@ LLVM_LIBC_FUNCTION(char *, strcat,
2120
LIBC_CRASH_ON_NULLPTR(dest);
2221
LIBC_CRASH_ON_NULLPTR(src);
2322
size_t dest_length = internal::string_length(dest);
24-
size_t src_length = internal::string_length(src);
25-
LIBC_NAMESPACE::strcpy(dest + dest_length, src);
26-
dest[dest_length + src_length] = '\0';
23+
size_t i;
24+
for (i = 0; src[i] != '\0'; ++i)
25+
dest[dest_length + i] = src[i];
26+
27+
dest[dest_length + i] = '\0';
2728
return dest;
2829
}
2930

libc/src/string/strncat.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "src/__support/macros/config.h"
1111
#include "src/__support/macros/null_check.h"
1212
#include "src/string/string_utils.h"
13-
#include "src/string/strncpy.h"
1413

1514
#include "src/__support/common.h"
1615

@@ -23,11 +22,12 @@ LLVM_LIBC_FUNCTION(char *, strncat,
2322
LIBC_CRASH_ON_NULLPTR(dest);
2423
LIBC_CRASH_ON_NULLPTR(src);
2524
}
26-
size_t src_length = internal::string_length(src);
27-
size_t copy_amount = src_length > count ? count : src_length;
2825
size_t dest_length = internal::string_length(dest);
29-
LIBC_NAMESPACE::strncpy(dest + dest_length, src, copy_amount);
30-
dest[dest_length + copy_amount] = '\0';
26+
size_t i;
27+
for (i = 0; i < count && src[i] != '\0'; ++i)
28+
dest[dest_length + i] = src[i];
29+
30+
dest[dest_length + i] = '\0';
3131
return dest;
3232
}
3333

0 commit comments

Comments
 (0)