[libc] Add strncat and fix strcat

This adds strncat to llvm libc. In addition, an error was found with
strcat and that was fixed.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D111583

GitOrigin-RevId: 9e9803bf82500b8a075d9658d2b4c248115f4a6f
diff --git a/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index b261a1a..56ae31d 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -35,6 +35,7 @@
     libc.src.string.strcpy
     libc.src.string.strcspn
     libc.src.string.strlen
+    libc.src.string.strncat
     libc.src.string.strncmp
     libc.src.string.strncpy
     libc.src.string.strnlen
diff --git a/src/string/CMakeLists.txt b/src/string/CMakeLists.txt
index aa22fa0..e5d6ebc 100644
--- a/src/string/CMakeLists.txt
+++ b/src/string/CMakeLists.txt
@@ -96,6 +96,17 @@
 )
 
 add_entrypoint_object(
+  strncat
+  SRCS
+    strncat.cpp
+  HDRS
+    strncat.h
+  DEPENDS
+    .strncpy
+    .string_utils
+)
+
+add_entrypoint_object(
   strncmp
   SRCS
     strncmp.cpp
diff --git a/src/string/strcat.cpp b/src/string/strcat.cpp
index 176a42a..fef2758 100644
--- a/src/string/strcat.cpp
+++ b/src/string/strcat.cpp
@@ -16,7 +16,10 @@
 
 LLVM_LIBC_FUNCTION(char *, strcat,
                    (char *__restrict dest, const char *__restrict src)) {
-  __llvm_libc::strcpy(dest + internal::string_length(dest), src);
+  size_t destLength = internal::string_length(dest);
+  size_t srcLength = internal::string_length(src);
+  __llvm_libc::strcpy(dest + destLength, src);
+  dest[destLength + srcLength] = '\0';
   return dest;
 }
 
diff --git a/src/string/strncat.cpp b/src/string/strncat.cpp
new file mode 100644
index 0000000..968c1e2
--- /dev/null
+++ b/src/string/strncat.cpp
@@ -0,0 +1,28 @@
+//===-- Implementation of strncat -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strncat.h"
+#include "src/string/string_utils.h"
+#include "src/string/strncpy.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(char *, strncat,
+                   (char *__restrict dest, const char *__restrict src,
+                    size_t count)) {
+  size_t srcLength = internal::string_length(src);
+  size_t copyAmount = srcLength > count ? count : srcLength;
+  size_t destLength = internal::string_length(dest);
+  __llvm_libc::strncpy(dest + destLength, src, copyAmount);
+  dest[destLength + copyAmount] = '\0';
+  return dest;
+}
+
+} // namespace __llvm_libc
diff --git a/src/string/strncat.h b/src/string/strncat.h
new file mode 100644
index 0000000..9b2c70f
--- /dev/null
+++ b/src/string/strncat.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for strncat -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRING_STRNCAT_H
+#define LLVM_LIBC_SRC_STRING_STRNCAT_H
+
+#include <string.h>
+
+namespace __llvm_libc {
+
+char *strncat(char *__restrict dest, const char *__restrict src, size_t count);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRNCAT_H
diff --git a/test/src/string/CMakeLists.txt b/test/src/string/CMakeLists.txt
index d68c1c9..2d6d919 100644
--- a/test/src/string/CMakeLists.txt
+++ b/test/src/string/CMakeLists.txt
@@ -83,6 +83,16 @@
 )
 
 add_libc_unittest(
+  strncat_test
+  SUITE
+    libc_string_unittests
+  SRCS
+    strncat_test.cpp
+  DEPENDS
+    libc.src.string.strncat
+)
+
+add_libc_unittest(
   strncmp_test
   SUITE
     libc_string_unittests
diff --git a/test/src/string/strncat_test.cpp b/test/src/string/strncat_test.cpp
new file mode 100644
index 0000000..f7442dc
--- /dev/null
+++ b/test/src/string/strncat_test.cpp
@@ -0,0 +1,76 @@
+//===-- Unittests for strncat ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strncat.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(LlvmLibcStrNCatTest, EmptyDest) {
+  const char *abc = "abc";
+  char dest[4];
+
+  dest[0] = '\0';
+
+  // Start by copying nothing
+  char *result = __llvm_libc::strncat(dest, abc, 0);
+  ASSERT_EQ(dest, result);
+  ASSERT_EQ(dest[0], '\0');
+
+  // Then copy part of it.
+  result = __llvm_libc::strncat(dest, abc, 1);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "a");
+
+  // Reset for the last test.
+  dest[0] = '\0';
+
+  // Then copy all of it.
+  result = __llvm_libc::strncat(dest, abc, 3);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, result);
+  ASSERT_STREQ(dest, abc);
+}
+
+TEST(LlvmLibcStrNCatTest, NonEmptyDest) {
+  const char *abc = "abc";
+  char dest[7];
+
+  dest[0] = 'x';
+  dest[1] = 'y';
+  dest[2] = 'z';
+  dest[3] = '\0';
+
+  // Copy only part of the string onto the end
+  char *result = __llvm_libc::strncat(dest, abc, 1);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "xyza");
+
+  // Copy a bit more, but without resetting.
+  result = __llvm_libc::strncat(dest, abc, 2);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "xyzaab");
+
+  // Set just the end marker, to make sure it overwrites properly.
+  dest[3] = '\0';
+
+  result = __llvm_libc::strncat(dest, abc, 3);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "xyzabc");
+
+  // Check that copying still works when count > src length
+  dest[0] = '\0';
+  // And that it doesn't write beyond what is necessary.
+  dest[4] = 'Z';
+  result = __llvm_libc::strncat(dest, abc, 4);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "abc");
+  ASSERT_EQ(dest[4], 'Z');
+
+  result = __llvm_libc::strncat(dest, abc, 5);
+  ASSERT_EQ(dest, result);
+  ASSERT_STREQ(dest, "abcabc");
+}