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

Commit 364ad3e6 authored by Steven Terrell's avatar Steven Terrell
Browse files

Fix Possible Stack Overflow

While calling getJankTracker or reportAppJankStats its possible to
overflow the stack if the view has not been added to the view hierarchy.
This change checks if the view that getJankTracker is called on is also
the rootview and returns if true.

Bug: 382074138
Test: CoreAppJankTestCases:JankTrackerTest
Flag: EXEMPT bugfix.

Change-Id: I18ac27677689cdd9d1912adee3f88204a432f9e8
parent 3fa37b4f
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -34456,7 +34456,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     */
    @FlaggedApi(android.app.jank.Flags.FLAG_DETAILED_APP_JANK_METRICS_API)
    public void reportAppJankStats(@NonNull AppJankStats appJankStats) {
        getRootView().reportAppJankStats(appJankStats);
        View rootView = getRootView();
        if (rootView == this) return;
        rootView.reportAppJankStats(appJankStats);
    }
    /**
@@ -34464,6 +34467,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     * @hide
     */
    public @Nullable JankTracker getJankTracker() {
        return getRootView().getJankTracker();
        View rootView = getRootView();
        if (rootView == this) {
            return null;
        }
        return rootView.getJankTracker();
    }
}
+14 −2
Original line number Diff line number Diff line
@@ -18,10 +18,12 @@ package android.app.jank.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import android.app.jank.Flags;
import android.app.jank.JankTracker;
import android.app.jank.StateTracker;
import android.content.Context;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
@@ -55,10 +57,9 @@ public class JankTrackerTest {
     * Start an empty activity so decore view is not null when creating the JankTracker instance.
     */
    private static ActivityScenario<EmptyActivity> sEmptyActivityRule;

    private static String sActivityName;

    private static View sActivityDecorView;
    private static Context sContext;

    @BeforeClass
    public static void classSetup() {
@@ -66,6 +67,7 @@ public class JankTrackerTest {
        sEmptyActivityRule.onActivity(activity -> {
            sActivityDecorView = activity.getWindow().getDecorView();
            sActivityName = activity.toString();
            sContext = activity.getApplicationContext();
        });
    }

@@ -168,4 +170,14 @@ public class JankTrackerTest {

        assertNotNull(jankTracker);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_DETAILED_APP_JANK_METRICS_API)
    public void jankTracker_IsNull_WhenViewNotInHierarchy() {
        TestWidget testWidget = new TestWidget(sContext);
        JankTracker jankTracker = testWidget.getJankTracker();

        assertNull(jankTracker);
    }

}