r23660 - gnucash/trunk/src/bin - Bug 721260 - Crash on startup: gnucash cannot handle default locale

John Ralls jralls at code.gnucash.org
Tue Dec 31 17:36:37 EST 2013


Author: jralls
Date: 2013-12-31 17:36:36 -0500 (Tue, 31 Dec 2013)
New Revision: 23660
Trac: http://svn.gnucash.org/trac/changeset/23660

Modified:
   gnucash/trunk/src/bin/gnucash-bin.c
Log:
Bug 721260 - Crash on startup: gnucash cannot handle default locale

Actually, Guile can't handle weird locales, which OS X 10.9 lets
one set. Guile will throw an exception.

This patch avoids the problem by ensuring that the locale is set to
something that setlocale() will work with, ideally in the language
indicated by defaults.

Modified: gnucash/trunk/src/bin/gnucash-bin.c
===================================================================
--- gnucash/trunk/src/bin/gnucash-bin.c	2013-12-31 03:27:12 UTC (rev 23659)
+++ gnucash/trunk/src/bin/gnucash-bin.c	2013-12-31 22:36:36 UTC (rev 23660)
@@ -187,7 +187,32 @@
     if ([locale_str isEqualToString: @"_"])
 	locale_str = @"en_US";
 
-    setlocale(LC_ALL, [locale_str UTF8String]);
+    if (!setlocale(LC_ALL, [locale_str UTF8String]))
+    {
+	NSArray *all_locales = [NSLocale availableLocaleIdentifiers];
+	NSEnumerator *locale_iter = [all_locales objectEnumerator];
+	NSString *this_locale, *new_locale = nil;
+	NSString *lang = [locale objectForKey: NSLocaleLanguageCode];
+	PWARN("Apple Locale is set to a value %s not supported"
+	      " by the C runtime", [locale_str UTF8String]);
+	while ((this_locale = (NSString*)[locale_iter nextObject]))
+	    if ([[[NSLocale componentsFromLocaleIdentifier: this_locale]
+		  objectForKey: NSLocaleLanguageCode]
+		 isEqualToString: lang] &&
+		setlocale (LC_ALL, [this_locale UTF8String]))
+	    {
+		new_locale = this_locale;
+		break;
+	    }
+	if (new_locale)
+	    locale_str = new_locale;
+	else
+	{
+	    locale_str = @"en_US";
+	    setlocale(LC_ALL, [locale_str UTF8String]);
+	}
+	PWARN("Using %s instead.", [locale_str UTF8String]);
+    }
     if (g_getenv("LANG") == NULL)
 	g_setenv("LANG", [locale_str UTF8String], TRUE);
 /* If the currency doesn't match the base locale, we need to find a locale that does match, because setlocale won't know what to do with just a currency identifier. */
@@ -199,12 +224,18 @@
 	NSString *currency = [locale objectForKey: NSLocaleCurrencyCode];
 	NSString *money_locale = nil;
 	while ((this_locale = (NSString*)[locale_iter nextObject]))
-	    if ([[[[NSLocale alloc] initWithLocaleIdentifier: this_locale]
-		   objectForKey: NSLocaleCurrencyCode]
-		 isEqualToString: currency]) {
+	{
+	    NSLocale *templocale = [[NSLocale alloc]
+				    initWithLocaleIdentifier: this_locale];
+	    if ([[templocale objectForKey: NSLocaleCurrencyCode]
+		 isEqualToString: currency])
+	    {
 		money_locale = this_locale;
+		[templocale release];
 		break;
 	    }
+	    [templocale release];
+	}
 	if (money_locale)
 	    setlocale(LC_MONETARY, [money_locale UTF8String]);
     }



More information about the gnucash-changes mailing list