Avoid crashes due to null SettingsSections

If a SettingsFile had at least one section, it was assumed all sections
were correctly filled out. This caused crashes when opening the settings
menus if that was not the case - for example the GFX.ini settings empty
sections are removed by the main dolphin app, putting the .ini file in a
state that would crash the settings window if at least one setting was
changed in it from the default, some sections were left as default.

This adds a subclass of HashMap<String, SettingSection> that constructs a
new SettingSection instead of returning 'null' if the key isn't found,
so the mSettings.get(FILE).get(SECTION).get(SETTING) pattern can be
safely used.
This commit is contained in:
Jonathan Hamilton 2017-09-07 09:50:27 -07:00
parent ce670c1851
commit 221462808d
1 changed files with 27 additions and 1 deletions

View File

@ -22,6 +22,32 @@ import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
/**
* A HashMap<String, SettingSection> that constructs a new SettingSection instead of returning null
* when getting a key not already in the map
*/
final class SettingsSectionMap extends HashMap<String, SettingSection>
{
@Override
public SettingSection get(Object key)
{
if (!(key instanceof String))
{
return null;
}
String stringKey = (String)key;
if (!super.containsKey(stringKey))
{
SettingSection section = new SettingSection(stringKey);
super.put(stringKey, section);
return section;
}
return super.get(key);
}
}
/**
* Contains static methods for interacting with .ini files in which settings are stored.
*/
@ -256,7 +282,7 @@ public final class SettingsFile
*/
public static HashMap<String, SettingSection> readFile(final String fileName, SettingsActivityView view)
{
HashMap<String, SettingSection> sections = new HashMap<>();
HashMap<String, SettingSection> sections = new SettingsSectionMap();
File ini = getSettingsFile(fileName);