[v4,01/22] perf symbol: Avoid memory leak from abi::__cxa_demangle

Message ID 20230320033810.980165-2-irogers@google.com
State New
Headers
Series Reference count checker and related fixes |

Commit Message

Ian Rogers March 20, 2023, 3:37 a.m. UTC
  Rather than allocate memory, allow abi::__cxa_demangle to do
that. This avoids a problem where on error NULL was returned
triggering a memory leak.

Fixes: 3b4e4efe88f6 ("perf symbol: Add abi::__cxa_demangle C++ demangling support")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/demangle-cxx.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
  

Patch

diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangle-cxx.cpp
index 8708bcafd370..85b706641837 100644
--- a/tools/perf/util/demangle-cxx.cpp
+++ b/tools/perf/util/demangle-cxx.cpp
@@ -38,11 +38,10 @@  char *cxx_demangle_sym(const char *str, bool params __maybe_unused,
 
         return cplus_demangle(str, flags);
 #elif defined(HAVE_CXA_DEMANGLE_SUPPORT)
-        size_t len = strlen(str);
-        char *output = (char*)malloc(len);
+        char *output;
         int status;
 
-        output = abi::__cxa_demangle(str, output, &len, &status);
+        output = abi::__cxa_demangle(str, /*output_buffer=*/NULL, /*length=*/NULL, &status);
         return output;
 #else
         return NULL;