[libc-commits] [libc] 3bbbec1 - [libc] add strndup

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Nov 1 12:52:55 PDT 2021


Author: Michael Jones
Date: 2021-11-01T12:52:51-07:00
New Revision: 3bbbec1ae7b7b3c3dc4e6a1801fb5bdb930aad79

URL: https://github.com/llvm/llvm-project/commit/3bbbec1ae7b7b3c3dc4e6a1801fb5bdb930aad79
DIFF: https://github.com/llvm/llvm-project/commit/3bbbec1ae7b7b3c3dc4e6a1801fb5bdb930aad79.diff

LOG: [libc] add strndup

add an implementation of strndup

Reviewed By: sivachandra

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

Added: 
    libc/src/string/strndup.cpp
    libc/src/string/strndup.h
    libc/test/src/string/strndup_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/stdc.td
    libc/src/string/CMakeLists.txt
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9f25e9e8efd52..bce6cbf1ee655 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -239,6 +239,7 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
 
     # string.h entrypoints that depend on malloc
     libc.src.string.strdup
+    libc.src.string.strndup
   )
 endif()
 

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index c997d4364fb15..2218bfca283f5 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -284,6 +284,11 @@ def StdC : StandardSpec<"stdc"> {
               RetValSpec<CharPtr>,
               [ArgSpec<ConstCharPtr>]
           >,
+          FunctionSpec<
+              "strndup",
+              RetValSpec<CharPtr>,
+              [ArgSpec<ConstCharPtr>,ArgSpec<SizeTType>]
+          >,
           FunctionSpec<
               "strpbrk",
               RetValSpec<CharPtr>,

diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 923ba09f3c64a..0fb50b6c8c353 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -153,6 +153,18 @@ add_entrypoint_object(
     strncpy.h
 )
 
+add_entrypoint_object(
+  strndup
+  SRCS
+    strndup.cpp
+  HDRS
+    strndup.h
+  DEPENDS
+    .memcpy
+    .string_utils
+    libc.include.stdlib
+)
+
 add_entrypoint_object(
   strnlen
   SRCS

diff  --git a/libc/src/string/strndup.cpp b/libc/src/string/strndup.cpp
new file mode 100644
index 0000000000000..6c904f4a646ca
--- /dev/null
+++ b/libc/src/string/strndup.cpp
@@ -0,0 +1,35 @@
+//===-- Implementation of strndup -----------------------------------------===//
+//
+// 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/strndup.h"
+#include "src/string/memcpy.h"
+#include "src/string/string_utils.h"
+
+#include "src/__support/common.h"
+
+#include <stddef.h>
+#include <stdlib.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(char *, strndup, (const char *src, size_t size)) {
+  if (src == nullptr)
+    return nullptr;
+  size_t len = internal::string_length(src);
+  if (len > size)
+    len = size;
+  char *dest = reinterpret_cast<char *>(::malloc(len + 1)); // NOLINT
+  if (dest == nullptr)
+    return nullptr;
+  char *result =
+      reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, len + 1));
+  result[len] = '\0';
+  return result;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/string/strndup.h b/libc/src/string/strndup.h
new file mode 100644
index 0000000000000..0a593a709aa97
--- /dev/null
+++ b/libc/src/string/strndup.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for strndup -----------------------*- 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_STRNDUP_H
+#define LLVM_LIBC_SRC_STRING_STRNDUP_H
+
+#include <string.h>
+
+namespace __llvm_libc {
+
+char *strndup(const char *src, size_t size);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRNDUP_H

diff  --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 8154aaa225a2e..a5b931d768d3c 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -143,6 +143,17 @@ add_libc_unittest(
     libc.src.string.strncpy
 )
 
+add_libc_unittest(
+  strndup_test
+  SUITE
+    libc_string_unittests
+  SRCS
+    strndup_test.cpp
+  DEPENDS
+    libc.include.stdlib
+    libc.src.string.strndup
+)
+
 add_libc_unittest(
   strnlen_test
   SUITE

diff  --git a/libc/test/src/string/strndup_test.cpp b/libc/test/src/string/strndup_test.cpp
new file mode 100644
index 0000000000000..0a17a7285b6d8
--- /dev/null
+++ b/libc/test/src/string/strndup_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for strndup ---------------------------------------------===//
+//
+// 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/strndup.h"
+#include "utils/UnitTest/Test.h"
+#include <stdlib.h>
+
+TEST(LlvmLibcstrndupTest, EmptyString) {
+  const char *empty = "";
+
+  char *result = __llvm_libc::strndup(empty, 1);
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(empty, const_cast<const char *>(result));
+  ASSERT_STREQ(empty, result);
+  ::free(result);
+}
+
+TEST(LlvmLibcstrndupTest, AnyString) {
+  const char *abc = "abc";
+
+  char *result = __llvm_libc::strndup(abc, 3);
+
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(abc, const_cast<const char *>(result));
+  ASSERT_STREQ(abc, result);
+  ::free(result);
+
+  result = __llvm_libc::strndup(abc, 1);
+
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(abc, const_cast<const char *>(result));
+  ASSERT_STREQ("a", result);
+  ::free(result);
+
+  result = __llvm_libc::strndup(abc, 10);
+
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(abc, const_cast<const char *>(result));
+  ASSERT_STREQ(abc, result);
+  ::free(result);
+}
+
+TEST(LlvmLibcstrndupTest, NullPtr) {
+  char *result = __llvm_libc::strndup(nullptr, 0);
+
+  ASSERT_EQ(result, static_cast<char *>(nullptr));
+}


        


More information about the libc-commits mailing list