[PATCH] D123420: [demangler] Rust demangler buffer reuse

Nathan Sidwell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 8 12:13:17 PDT 2022


urnathan created this revision.
urnathan added reviewers: dblaikie, iains, tmiasko.
Herald added subscribers: JDevlieghere, hiraditya.
Herald added a project: All.
urnathan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The rust demangler has some odd buffer handling code, which will copy the demangled string into the provided buffer, if it will fit. Otherwise it uses the allocated buffer it made.  But the length of the incoming buffer will have come from a previous call, which was the length of the demangled string -- not the buffer size.  And of course, we're unconditionally allocating a temporary buffer in the first place.  So we don't actually get buffer reuse, and we get a memcpy in somecases.

Let's just simplify the code by using the initialize API to reuse any incoming buffer from the get go.

[this is part of a series trying to clean up the demangler's APIs]


https://reviews.llvm.org/D123420

Files:
  llvm/lib/Demangle/RustDemangle.cpp


Index: llvm/lib/Demangle/RustDemangle.cpp
===================================================================
--- llvm/lib/Demangle/RustDemangle.cpp
+++ llvm/lib/Demangle/RustDemangle.cpp
@@ -164,7 +164,7 @@
   }
 
   Demangler D;
-  if (!initializeOutputBuffer(nullptr, nullptr, D.Output, 1024)) {
+  if (!initializeOutputBuffer(Buf, N, D.Output, 1024)) {
     if (Status != nullptr)
       *Status = demangle_memory_alloc_failure;
     return nullptr;
@@ -181,16 +181,6 @@
   char *Demangled = D.Output.getBuffer();
   size_t DemangledLen = D.Output.getCurrentPosition();
 
-  if (Buf != nullptr) {
-    if (DemangledLen <= *N) {
-      std::memcpy(Buf, Demangled, DemangledLen);
-      std::free(Demangled);
-      Demangled = Buf;
-    } else {
-      std::free(Buf);
-    }
-  }
-
   if (N != nullptr)
     *N = DemangledLen;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123420.421611.patch
Type: text/x-patch
Size: 833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220408/7c7ceffe/attachment.bin>


More information about the llvm-commits mailing list