From e96dfadae7db9b5b1d37bec060747c8929bb22a8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 18 Jun 2020 20:15:01 +0530 Subject: [PATCH] Ensure cocoa_get_lang() returns a valid locale or None See #1233 --- kitty/cocoa_window.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 867b21cbf75..fe7e9640f94 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -14,6 +14,7 @@ // Needed for _NSGetProgname #include #include +#include #if (MAC_OS_X_VERSION_MAX_ALLOWED < 101200) #define NSWindowStyleMaskResizable NSResizableWindowMask @@ -419,7 +420,13 @@ - (void)openFilesFromPasteboard:(NSPasteboard *)pasteboard type:(int)type { locale = [[NSLocale currentLocale] localeIdentifier]; } if (!locale) { Py_RETURN_NONE; } - return Py_BuildValue("s", [locale UTF8String]); + // Make sure the locale value is valid, that is it can be used + // to construct an actual locale + const char* locale_utf8 = [locale UTF8String]; + locale_t test_locale = newlocale(LC_ALL_MASK, locale_utf8, NULL); + if (!test_locale) { Py_RETURN_NONE; } + freelocale(test_locale); + return Py_BuildValue("s", locale_utf8); } // autoreleasepool }