]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/python] Ensure locale is restored in do_start_initialization
authorTom de Vries <tdevries@suse.de>
Fri, 22 Nov 2024 16:34:50 +0000 (17:34 +0100)
committerTom de Vries <tdevries@suse.de>
Fri, 22 Nov 2024 16:34:50 +0000 (17:34 +0100)
I noticed in do_start_initialization:
...
  std::string oldloc = setlocale (LC_ALL, NULL);
  setlocale (LC_ALL, "");
  ...
  if (count == (size_t) -1)
    {
      fprintf (stderr, "Could not convert python path to string\n");
      return false;
    }
  setlocale (LC_ALL, oldloc.c_str ());
...
that the old locale is not restored if the "return false" is triggered.

Fix this by using SCOPE_EXIT.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/python/python.c

index 397431a4c8441af1163a931b65459afe13c66a2f..7489b530e70b64a805354b1719dc8849e8c6d49e 100644 (file)
@@ -2331,17 +2331,20 @@ do_start_initialization ()
      for Python versions that do not duplicate program_name.  */
   static wchar_t *progname_copy;
 
-  std::string oldloc = setlocale (LC_ALL, NULL);
-  setlocale (LC_ALL, "");
-  size_t progsize = strlen (progname.get ());
-  progname_copy = XNEWVEC (wchar_t, progsize + 1);
-  size_t count = mbstowcs (progname_copy, progname.get (), progsize + 1);
-  if (count == (size_t) -1)
-    {
-      fprintf (stderr, "Could not convert python path to string\n");
-      return false;
-    }
-  setlocale (LC_ALL, oldloc.c_str ());
+  {
+    std::string oldloc = setlocale (LC_ALL, NULL);
+    SCOPE_EXIT { setlocale (LC_ALL, oldloc.c_str ()); };
+
+    setlocale (LC_ALL, "");
+    size_t progsize = strlen (progname.get ());
+    progname_copy = XNEWVEC (wchar_t, progsize + 1);
+    size_t count = mbstowcs (progname_copy, progname.get (), progsize + 1);
+    if (count == (size_t) -1)
+      {
+       fprintf (stderr, "Could not convert python path to string\n");
+       return false;
+      }
+  }
 
   /* Py_SetProgramName was deprecated in Python 3.11.  Use PyConfig
      mechanisms for Python 3.10 and newer.  */