Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57b6d4b5 authored by Songchun Fan's avatar Songchun Fan Committed by Automerger Merge Worker
Browse files

[SettingsProvider] limit individual setting length am: eabdf5da

parents fb2aaf9d eabdf5da
Loading
Loading
Loading
Loading
+28 −8
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ final class SettingsState {

    static final int SETTINGS_VERSION_NEW_ENCODING = 121;

    public static final int MAX_LENGTH_PER_STRING = 32768;
    private static final long WRITE_SETTINGS_DELAY_MILLIS = 200;
    private static final long MAX_WRITE_SETTINGS_DELAY_MILLIS = 2000;

@@ -430,6 +431,19 @@ final class SettingsState {
            return false;
        }

        final boolean isNameTooLong = name.length() > SettingsState.MAX_LENGTH_PER_STRING;
        final boolean isValueTooLong =
                value != null && value.length() > SettingsState.MAX_LENGTH_PER_STRING;
        if (isNameTooLong || isValueTooLong) {
            // only print the first few bytes of the name in case it is long
            final String errorMessage = "The " + (isNameTooLong ? "name" : "value")
                    + " of your setting ["
                    + (name.length() > 20 ? (name.substring(0, 20) + "...") : name)
                    + "] is too long. The max length allowed for the string is "
                    + MAX_LENGTH_PER_STRING + ".";
            throw new IllegalArgumentException(errorMessage);
        }

        Setting oldState = mSettings.get(name);
        String oldValue = (oldState != null) ? oldState.value : null;
        String oldDefaultValue = (oldState != null) ? oldState.defaultValue : null;
@@ -860,7 +874,6 @@ final class SettingsState {

                final int settingCount = settings.size();
                for (int i = 0; i < settingCount; i++) {

                    Setting setting = settings.valueAt(i);
                    if (setting.isTransient()) {
                        if (DEBUG_PERSISTENCE) {
@@ -869,8 +882,11 @@ final class SettingsState {
                        continue;
                    }

                    if (writeSingleSetting(mVersion, serializer, setting.getId(), setting.getName(),
                            setting.getValue(), setting.getDefaultValue(), setting.getPackageName(),
                    try {
                        if (writeSingleSetting(mVersion, serializer, setting.getId(),
                                setting.getName(),
                                setting.getValue(), setting.getDefaultValue(),
                                setting.getPackageName(),
                                setting.getTag(), setting.isDefaultFromSystem(),
                                setting.isValuePreservedInRestore())) {
                            if (DEBUG_PERSISTENCE) {
@@ -878,6 +894,10 @@ final class SettingsState {
                                        + setting.getValue());
                            }
                        }
                    } catch (IOException ex) {
                        Slog.e(LOG_TAG, "[SKIPPED PERSISTING]" + setting.getName()
                                + " due to error writing to disk", ex);
                    }
                }
                serializer.endTag(null, TAG_SETTINGS);

+66 −20
Original line number Diff line number Diff line
@@ -408,4 +408,50 @@ public class SettingsStateTest extends AndroidTestCase {
        }
        assertEquals(expectedMemUsage, settingsState.getMemoryUsage(TEST_PACKAGE));
    }

    public void testLargeSettingKey() {
        SettingsState settingsState = new SettingsState(getContext(), mLock, mSettingsFile, 1,
                SettingsState.MAX_BYTES_PER_APP_PACKAGE_LIMITED, Looper.getMainLooper());
        final String largeKey = Strings.repeat("A", SettingsState.MAX_LENGTH_PER_STRING + 1);
        final String testValue = "testValue";
        synchronized (mLock) {
            // Test system package
            try {
                settingsState.insertSettingLocked(largeKey, testValue, null, true, SYSTEM_PACKAGE);
                fail("Should throw because it exceeded max string length");
            } catch (IllegalArgumentException ex) {
                assertTrue(ex.getMessage().contains("The max length allowed for the string is "));
            }
            // Test non system package
            try {
                settingsState.insertSettingLocked(largeKey, testValue, null, true, TEST_PACKAGE);
                fail("Should throw because it exceeded max string length");
            } catch (IllegalArgumentException ex) {
                assertTrue(ex.getMessage().contains("The max length allowed for the string is "));
            }
        }
    }

    public void testLargeSettingValue() {
        SettingsState settingsState = new SettingsState(getContext(), mLock, mSettingsFile, 1,
                SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
        final String testKey = "testKey";
        final String largeValue = Strings.repeat("A", SettingsState.MAX_LENGTH_PER_STRING + 1);
        synchronized (mLock) {
            // Test system package
            try {
                settingsState.insertSettingLocked(testKey, largeValue, null, true, SYSTEM_PACKAGE);
                fail("Should throw because it exceeded max string length");
            } catch (IllegalArgumentException ex) {
                assertTrue(ex.getMessage().contains("The max length allowed for the string is "));
            }
            // Test non system package
            try {
                settingsState.insertSettingLocked(testKey, largeValue, null, true, TEST_PACKAGE);
                fail("Should throw because it exceeded max string length");
            } catch (IllegalArgumentException ex) {
                assertTrue(ex.getMessage().contains("The max length allowed for the string is "));
            }
        }
    }
}