[libc-commits] [PATCH] D140461: [libc][NFC] Use the custom operator new from strndup.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Dec 20 23:37:39 PST 2022


sivachandra created this revision.
sivachandra added reviewers: lntue, michaelrj.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
sivachandra requested review of this revision.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140461

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


Index: libc/src/string/strndup.cpp
===================================================================
--- libc/src/string/strndup.cpp
+++ 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 @@
   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';
Index: libc/src/string/CMakeLists.txt
===================================================================
--- libc/src/string/CMakeLists.txt
+++ libc/src/string/CMakeLists.txt
@@ -246,6 +246,7 @@
     .memory_utils.memcpy_implementation
     .string_utils
     libc.include.stdlib
+    libc.src.__support.CPP.new
 )
 
 add_entrypoint_object(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140461.484476.patch
Type: text/x-patch
Size: 1107 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221221/9fc4529c/attachment.bin>


More information about the libc-commits mailing list