From: Jérémie Galarneau Date: Fri, 8 Mar 2024 17:06:30 +0000 (-0500) Subject: format: use unique_ptr to wrap unmangled string X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=830bc99366f13bfb3d6fc975441aba7388cb7ca1 format: use unique_ptr to wrap unmangled string Change-Id: I8459507a55caf2c77a21fcc3442bcde069b2601b Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/format.hpp b/src/common/format.hpp index c1ab3d54d..07673e7a9 100644 --- a/src/common/format.hpp +++ b/src/common/format.hpp @@ -20,6 +20,8 @@ DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES #include DIAGNOSTIC_POP +#include + /* * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace, * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480. @@ -32,12 +34,15 @@ struct formatter : formatter { FormatContextType& ctx) { int status; - auto demangled_name = - abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status); - auto it = status == 0 ? formatter::format(demangled_name, ctx) : - formatter::format(type_info.name(), ctx); + /* + * The documentation of __cxa_demangle mentions the returned string is allocated + * using malloc (not new), hence the use of lttng::free. + */ + const auto demangled_name = lttng::make_unique_wrapper( + abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status)); - free(demangled_name); + auto it = status == 0 ? formatter::format(demangled_name.get(), ctx) : + formatter::format(type_info.name(), ctx); return it; } };