From 278f3eff15ced0fd51c54336e17945eaa6b17583 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 20 Dec 2021 11:40:52 +0530 Subject: [PATCH] fix: crash caused by app.getLocaleCountryCode() CFLocaleGetValue() returned null and crashed the process when app.getLocaleCountryCode() was run on a CircleCI metal resource class macOS instance with Xcode 12.5.1. This change fixes that logic and adds further checks to make the code future-proof. Here too people are complaining that the returned country code migth be null: https://stackoverflow.com/questions/15202454/nslocalecountrycode-returns-nil Signed-off-by: Darshan Sen --- shell/browser/api/electron_api_app.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 1ecc88721c445..a9262afd261f8 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1054,11 +1054,14 @@ std::string App::GetLocaleCountryCode() { CFLocaleRef locale = CFLocaleCopyCurrent(); CFStringRef value = CFStringRef( static_cast(CFLocaleGetValue(locale, kCFLocaleCountryCode))); - const CFIndex kCStringSize = 128; - char temporaryCString[kCStringSize] = {0}; - CFStringGetCString(value, temporaryCString, kCStringSize, - kCFStringEncodingUTF8); - region = temporaryCString; + if (value != nil) { + char temporaryCString[3]; + const CFIndex kCStringSize = sizeof(temporaryCString); + if (CFStringGetCString(value, temporaryCString, kCStringSize, + kCFStringEncodingUTF8)) { + region = temporaryCString; + } + } #else const char* locale_ptr = setlocale(LC_TIME, nullptr); if (!locale_ptr)