From 830bc99366f13bfb3d6fc975441aba7388cb7ca1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Fri, 8 Mar 2024 12:06:30 -0500 Subject: [PATCH] format: use unique_ptr to wrap unmangled string MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I8459507a55caf2c77a21fcc3442bcde069b2601b Signed-off-by: Jérémie Galarneau --- src/common/format.hpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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; } }; -- 2.34.1