duplocale: Fix test failure on AIX 7.2 with xlclang.
authorBruno Haible <bruno@clisp.org>
Thu, 12 Dec 2019 16:52:50 +0000 (17:52 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 12 Dec 2019 16:52:50 +0000 (17:52 +0100)
* lib/duplocale.c: Include <stdlib.h>.
(rpl_duplocale): Use a heap-allocated copy of the first setlocale return
value.

ChangeLog
lib/duplocale.c

index edbcab28e20f55091f26bd76d8476c2b86f23aef..6f4f764de7c5eb52013ca7582e558b5f73d85f94 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2019-12-12  Bruno Haible  <bruno@clisp.org>
+
+       duplocale: Fix test failure on AIX 7.2 with xlclang.
+       * lib/duplocale.c: Include <stdlib.h>.
+       (rpl_duplocale): Use a heap-allocated copy of the first setlocale return
+       value.
+
 2019-12-12  Bruno Haible  <bruno@clisp.org>
 
        stddef: Document the AIX xlc issue.
index eaecf8cb443aea94dbb987a348bac5fdf2b5f322..3b958bab510ffb23209b54b1bcb0325be0ef86a9 100644 (file)
@@ -22,6 +22,7 @@
 #include <locale.h>
 
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
@@ -67,14 +68,21 @@ rpl_duplocale (locale_t locale)
           , { LC_IDENTIFICATION, LC_IDENTIFICATION_MASK }
 #endif
         };
-      const char *base_name;
+      char *base_name;
       locale_t base_copy;
       unsigned int i;
 
-      base_name = setlocale (LC_CTYPE, NULL);
+      base_name = strdup (setlocale (LC_CTYPE, NULL));
+      if (base_name == NULL)
+        return NULL;
       base_copy = newlocale (LC_ALL_MASK, base_name, NULL);
       if (base_copy == NULL)
-        return NULL;
+        {
+          int saved_errno = errno;
+          free (base_name);
+          errno = saved_errno;
+          return NULL;
+        }
 
       for (i = 0; i < SIZEOF (categories); i++)
         {
@@ -88,6 +96,7 @@ rpl_duplocale (locale_t locale)
                 {
                   int saved_errno = errno;
                   freelocale (base_copy);
+                  free (base_name);
                   errno = saved_errno;
                   return NULL;
                 }
@@ -97,6 +106,7 @@ rpl_duplocale (locale_t locale)
             }
         }
 
+      free (base_name);
       return base_copy;
     }