[libc-commits] [libc] c2f17bf - [libc][NFC] Use the custom operator new from strndup.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Wed Dec 21 12:58:48 PST 2022


Author: Siva Chandra Reddy
Date: 2022-12-21T20:58:40Z
New Revision: c2f17bfbff80352ae43df9649a51b56253b0097d

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

LOG: [libc][NFC] Use the custom operator new from strndup.

Reviewed By: lntue

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

Added: 
    

Modified: 
    libc/src/string/CMakeLists.txt
    libc/src/string/strndup.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index caeddc8995e68..0faa1db4684e6 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -246,6 +246,7 @@ add_entrypoint_object(
     .memory_utils.memcpy_implementation
     .string_utils
     libc.include.stdlib
+    libc.src.__support.CPP.new
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/string/strndup.cpp b/libc/src/string/strndup.cpp
index 4856a484b851b..9a8d414c76bc9 100644
--- a/libc/src/string/strndup.cpp
+++ b/libc/src/string/strndup.cpp
@@ -10,10 +10,10 @@
 #include "src/string/memory_utils/memcpy_implementations.h"
 #include "src/string/string_utils.h"
 
+#include "src/__support/CPP/new.h"
 #include "src/__support/common.h"
 
 #include <stddef.h>
-#include <stdlib.h>
 
 namespace __llvm_libc {
 
@@ -23,8 +23,9 @@ LLVM_LIBC_FUNCTION(char *, strndup, (const char *src, size_t size)) {
   size_t len = internal::string_length(src);
   if (len > size)
     len = size;
-  char *dest = reinterpret_cast<char *>(::malloc(len + 1));
-  if (dest == nullptr)
+  AllocChecker ac;
+  char *dest = new (ac) char[len];
+  if (!ac)
     return nullptr;
   inline_memcpy(dest, src, len + 1);
   dest[len] = '\0';


        


More information about the libc-commits mailing list