8000 Prevent systemPreferences.getUserDefault crash for missing arrays/dictionaries by kevinsawicki · Pull Request #7993 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prevent systemPreferences.getUserDefault crash for missing arrays/dictionaries #7993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
void SystemPreferences::PostNotification(const std::string& name,
const base::DictionaryValue& user_info) {
DoPostNotification(name, user_info, false);
}

void SystemPreferences::PostLocalNotification(const std::string& name,
void SystemPreferences::PostLocalNotification(const std::string& name,
const base::DictionaryValue& user_info) {
DoPostNotification(name, user_info, true);
}

void SystemPreferences::DoPostNotification(const std::string& name,
void SystemPreferences::DoPostNotification(const std::string& name,
const base::DictionaryValue& user_info, bool is_local) {
NSNotificationCenter* center = is_local ?
[NSNotificationCenter defaultCenter] :
Expand Down Expand Up @@ -128,11 +128,17 @@
return mate::ConvertToV8(isolate(),
net::GURLWithNSURL([defaults URLForKey:key]));
} else if (type == "array") {
return mate::ConvertToV8(isolate(),
*NSArrayToListValue([defaults arrayForKey:key]));
std::unique_ptr<base::ListValue> list =
NSArrayToListValue([defaults arrayForKey:key]);
if (list == nullptr)
list.reset(new base::ListValue());
return mate::ConvertToV8(isolate(), *list);
} else if (type == "dictionary") {
return mate::ConvertToV8(isolate(),
*NSDictionaryToDictionaryValue([defaults dictionaryForKey:key]));
std::unique_ptr<base::DictionaryValue> dictionary =
NSDictionaryToDictionaryValue([defaults dictionaryForKey:key]);
if (dictionary == nullptr)
dictionary.reset(new base::DictionaryValue());
return mate::ConvertToV8(isolate(), *dictionary);
} else {
return v8::Undefined(isolate());
}
Expand Down
20 changes: 16 additions & 4 deletions spec/api-system-preferences-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,26 @@ describe('systemPreferences module', function () {
}

it('returns values for known user defaults', function () {
let locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.notEqual(locale, null)
const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.equal(typeof locale, 'string')
assert(locale.length > 0)

let languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert.notEqual(languages, null)
const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert(Array.isArray(languages))
assert(languages.length > 0)
})

it('returns values for unknown user defaults', function () {
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype'), undefined)
assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array'), [])
assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary'), {})
})
})

describe('systemPreferences.isInvertedColorScheme()', function () {
Expand Down
0