Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / locales.c
index 403199d0c9a2cc2d5fe1cfc6629f1bbd7d889ccf..c3800d5325fbc6e599fe7188b4150a52ee78b0e4 100644 (file)
@@ -479,22 +479,27 @@ get_darwin_locale (void)
 static char *
 get_posix_locale (void)
 {
-       const char *locale;
+       char *locale;
 
        locale = g_getenv ("LC_ALL");
        if (locale == NULL) {
                locale = g_getenv ("LANG");
-               if (locale == NULL)
-                       locale = setlocale (LC_ALL, NULL);
+               if (locale == NULL) {
+                       char *static_locale = setlocale (LC_ALL, NULL);
+                       if (static_locale)
+                               locale = g_strdup (static_locale);
+               }
        }
        if (locale == NULL)
                return NULL;
 
        /* Skip English-only locale 'C' */
-       if (strcmp (locale, "C") == 0)
+       if (strcmp (locale, "C") == 0) {
+               g_free (locale);
                return NULL;
+       }
 
-       return g_strdup (locale);
+       return locale;
 }