]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/charset.c
gdb/gdbsupport: make xstrprintf and xstrvprintf return a unique_ptr
[thirdparty/binutils-gdb.git] / gdb / charset.c
index 9fb1a1f0b67e775afbe006012942353369e8f976..d1aebaa501165fd008a18d6f32e057dba38010ae 100644 (file)
@@ -960,35 +960,31 @@ intermediate_encoding (void)
 {
   iconv_t desc;
   static const char *stored_result = NULL;
-  char *result;
+  gdb::unique_xmalloc_ptr<char> result;
 
   if (stored_result)
     return stored_result;
   result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
                       ENDIAN_SUFFIX);
   /* Check that the name is supported by iconv_open.  */
-  desc = iconv_open (result, host_charset ());
+  desc = iconv_open (result.get (), host_charset ());
   if (desc != (iconv_t) -1)
     {
       iconv_close (desc);
-      stored_result = result;
-      return result;
+      stored_result = result.release ();
+      return stored_result;
     }
-  /* Not valid, free the allocated memory.  */
-  xfree (result);
   /* Second try, with UCS-2 type.  */
   result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
                       ENDIAN_SUFFIX);
   /* Check that the name is supported by iconv_open.  */
-  desc = iconv_open (result, host_charset ());
+  desc = iconv_open (result.get (), host_charset ());
   if (desc != (iconv_t) -1)
     {
       iconv_close (desc);
-      stored_result = result;
-      return result;
+      stored_result = result.release ();
+      return stored_result;
     }
-  /* Not valid, free the allocated memory.  */
-  xfree (result);
   /* No valid charset found, generate error here.  */
   error (_("Unable to find a valid charset for string conversions"));
 }