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

Commit 5991c48a authored by Songchun Fan's avatar Songchun Fan Committed by Song Chun Fan
Browse files

[SettingsProvider] fix potential MemoryIntArray leakage

Although it should not happen, for completeness, patch the code up in
case the MemoryIntArray that is sent to the client over Parcel is not closed properly.

BUG: 277448672
Test: presubmit

Change-Id: Ia712f9a9d7ffa0f531265bd7351358da5f582551
parent 63e18c96
Loading
Loading
Loading
Loading
+20 −9
Original line number Diff line number Diff line
@@ -3176,15 +3176,7 @@ public final class Settings {
        }
        public void destroy() {
            try {
                // If this process is the system server process, mArray is the same object as
                // the memory int array kept inside SettingsProvider, so skipping the close()
                if (!Settings.isInSystemServer() && !mArray.isClosed()) {
                    mArray.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "Error closing backing array", e);
            }
            maybeCloseGenerationArray(mArray);
        }
        @Override
@@ -3197,6 +3189,21 @@ public final class Settings {
        }
    }
    private static void maybeCloseGenerationArray(@Nullable MemoryIntArray array) {
        if (array == null) {
            return;
        }
        try {
            // If this process is the system server process, the MemoryIntArray received from Parcel
            // is the same object as the one kept inside SettingsProvider, so skipping the close().
            if (!Settings.isInSystemServer() && !array.isClosed()) {
                array.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "Error closing the generation tracking array", e);
        }
    }
    private static final class ContentProviderHolder {
        private final Object mLock = new Object();
@@ -3504,6 +3511,8 @@ public final class Settings {
                                        mGenerationTrackers.put(name, new GenerationTracker(name,
                                                array, index, generation,
                                                mGenerationTrackerErrorHandler));
                                    } else {
                                        maybeCloseGenerationArray(array);
                                    }
                                }
                                if (mGenerationTrackers.get(name) != null
@@ -3723,6 +3732,8 @@ public final class Settings {
                                    new GenerationTracker(prefix, array, index, generation,
                                            mGenerationTrackerErrorHandler));
                            currentGeneration = generation;
                        } else {
                            maybeCloseGenerationArray(array);
                        }
                    }
                    if (mGenerationTrackers.get(prefix) != null && currentGeneration