From 221462808d96239bbfa7f862978a8b2846911d5b Mon Sep 17 00:00:00 2001 From: Jonathan Hamilton Date: Thu, 7 Sep 2017 09:50:27 -0700 Subject: [PATCH] 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 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. --- .../dolphinemu/utils/SettingsFile.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/SettingsFile.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/SettingsFile.java index 96f40c3296..dd4bbdfb66 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/SettingsFile.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/SettingsFile.java @@ -22,6 +22,32 @@ import java.util.HashMap; import java.util.Set; import java.util.TreeSet; +/** + * A HashMap that constructs a new SettingSection instead of returning null + * when getting a key not already in the map + */ +final class SettingsSectionMap extends HashMap +{ + @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 readFile(final String fileName, SettingsActivityView view) { - HashMap sections = new HashMap<>(); + HashMap sections = new SettingsSectionMap(); File ini = getSettingsFile(fileName);