]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/BackTrace: let abi::__cxa_demangle() do the malloc 37045/head
authorKefu Chai <kchai@redhat.com>
Wed, 9 Sep 2020 00:42:24 +0000 (08:42 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 17 Sep 2020 07:24:37 +0000 (15:24 +0800)
also use the returned length for constructing the string_view to be
appended.

we could reuse the buffer across multiple demangle() call for saving the
calls to malloc()/free(). but the upside of this change is that it's
simpler.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/BackTrace.cc

index 453ba02dbb9d7fccb3e9e488411cdb72b9dbb404..1dfa39cda1762b421f7c260a2030e7a8528ad14f 100644 (file)
@@ -51,23 +51,17 @@ std::string BackTrace::demangle(const char* name)
     int status;
     // only demangle a C++ mangled name
     if (mangled.compare(0, 2, "_Z") == 0) {
-      // just a guess, template names will go much wider
-      size_t len = 1024;
-      char* buf = (char*)malloc(len);
-      if (!buf) {
-        return {};
-      }
-      if (char* demangled = abi::__cxa_demangle(mangled.c_str(), buf, &len, &status)) {
+      // let __cxa_demangle do the malloc
+      size_t len = 0;
+      if (char* demangled = abi::__cxa_demangle(mangled.c_str(), nullptr, &len, &status)) {
         std::string full_name{OPEN};
-        full_name += demangled;
+        full_name += std::string_view(demangled, len);
         full_name += end;
         // buf could be reallocated, so free(demangled) instead
         free(demangled);
         return full_name;
-      } else {
-        // demangle failed, just pretend it's a C function with no args
-        free(buf);
       }
+      // demangle failed, just pretend it's a C function with no args
     }
     // C function
     return mangled + "()";