format: use unique_ptr to wrap unmangled string
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 8 Mar 2024 17:06:30 +0000 (12:06 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 8 Mar 2024 21:16:05 +0000 (16:16 -0500)
Change-Id: I8459507a55caf2c77a21fcc3442bcde069b2601b
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/format.hpp

index c1ab3d54ddc33cad2fb4a2b1c91aeab7ae2338dc..07673e7a98166ad9acd5b7d714693be9d3b9b8df 100644 (file)
@@ -20,6 +20,8 @@ DIAGNOSTIC_IGNORE_DUPLICATED_BRANCHES
 #include <vendor/fmt/core.h>
 DIAGNOSTIC_POP
 
+#include <common/make-unique-wrapper.hpp>
+
 /*
  * 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<std::type_info> : formatter<std::string> {
                                                    FormatContextType& ctx)
        {
                int status;
-               auto demangled_name =
-                       abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status);
-               auto it = status == 0 ? formatter<std::string>::format(demangled_name, ctx) :
-                                       formatter<std::string>::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<char, lttng::free>(
+                       abi::__cxa_demangle(type_info.name(), nullptr, nullptr, &status));
 
-               free(demangled_name);
+               auto it = status == 0 ? formatter<std::string>::format(demangled_name.get(), ctx) :
+                                       formatter<std::string>::format(type_info.name(), ctx);
                return it;
        }
 };
This page took 0.025367 seconds and 4 git commands to generate.