Skip to content

Commit ac9675f

Browse files
[libc] Independent strcat/strncat
The previous implementations called other entrypoints. This patch fixes strcat and strncat to be properly independent.
1 parent 1be7c6f commit ac9675f

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

libc/src/string/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ add_entrypoint_object(
110110
HDRS
111111
strcat.h
112112
DEPENDS
113-
.strcpy
114113
.string_utils
115114
libc.include.llvm-libc-types.size_t
116115
)
@@ -267,7 +266,6 @@ add_entrypoint_object(
267266
HDRS
268267
strncat.h
269268
DEPENDS
270-
.strncpy
271269
.string_utils
272270
libc.include.llvm-libc-types.size_t
273271
)

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)