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

Commit ba0821ed authored by Svet Ganov's avatar Svet Ganov
Browse files

Make read/write from/to XML persistent state more robust.

When writing critical state to XML an excpetion can lead to creating
a malformed XML that is later parsed and may put the device in a bad
state. Hence, on any error while writing we should bail out and drop
the partially write state on the floor.

Corollary, any error on parsing can lead to having a partially read
state that is not consistent which may lead to writing this bad state
back to disk. Hence, on any error while parsing we should bail as
our current state may be unrecoverable.

Change-Id: Ia050c16198cb583f8a51263ad2035dbb948052b8
parent 4f2dcfd4
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -383,8 +383,9 @@ final class SettingsState {
                Slog.i(LOG_TAG, "[PERSIST END]");
            }

        } catch (IOException e) {
            Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", e);
            // Any error while writing is fatal.
        } catch (Throwable t) {
            Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", t);
            destination.failWrite(out);
        } finally {
            IoUtils.closeQuietly(out);
@@ -406,9 +407,11 @@ final class SettingsState {
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(in, null);
            parseStateLocked(parser);
        } catch (XmlPullParserException | IOException ise) {

            // Any error while parsing is fatal.
        } catch (Throwable t) {
            throw new IllegalStateException("Failed parsing settings file: "
                    + mStatePersistFile , ise);
                    + mStatePersistFile , t);
        } finally {
            IoUtils.closeQuietly(in);
        }
+8 −4
Original line number Diff line number Diff line
@@ -4269,9 +4269,11 @@ final class Settings {
                serializer.endTag(null, TAG_RUNTIME_PERMISSIONS);
                serializer.endDocument();
                destination.finishWrite(out);
            } catch (IOException e) {

                // Any error while writing is fatal.
            } catch (Throwable t) {
                Slog.wtf(PackageManagerService.TAG,
                        "Failed to write settings, restoring backup", e);
                        "Failed to write settings, restoring backup", t);
                destination.failWrite(out);
            } finally {
                IoUtils.closeQuietly(out);
@@ -4319,9 +4321,11 @@ final class Settings {
                XmlPullParser parser = Xml.newPullParser();
                parser.setInput(in, null);
                parseRuntimePermissionsLPr(parser, userId);
            } catch (XmlPullParserException | IOException ise) {

                // Any error while parsing is fatal.
            } catch (Throwable t) {
                throw new IllegalStateException("Failed parsing permissions file: "
                        + permissionsFile , ise);
                        + permissionsFile , t);
            } finally {
                IoUtils.closeQuietly(in);
            }