Return copies of private array members in Display.java to protect them from being modified from the outside.

Bug: 278237145
Test: atest DisplayTest.java
Change-Id: I21c670d45c9282c555c98924bf55f5147c5c7ba7
This commit is contained in:
Marc Kassis
2023-04-14 17:30:15 +02:00
parent a6b13ba0c1
commit dc403705fd
2 changed files with 36 additions and 4 deletions

View File

@@ -2188,7 +2188,7 @@ public final class Display {
*/
@NonNull
public float[] getAlternativeRefreshRates() {
return mAlternativeRefreshRates;
return Arrays.copyOf(mAlternativeRefreshRates, mAlternativeRefreshRates.length);
}
/**
@@ -2197,7 +2197,7 @@ public final class Display {
@NonNull
@HdrCapabilities.HdrType
public int[] getSupportedHdrTypes() {
return mSupportedHdrTypes;
return Arrays.copyOf(mSupportedHdrTypes, mSupportedHdrTypes.length);
}
/**
@@ -2497,8 +2497,10 @@ public final class Display {
* @deprecated use {@link Display#getMode()}
* and {@link Mode#getSupportedHdrTypes()} instead
*/
public @HdrType int[] getSupportedHdrTypes() {
return mSupportedHdrTypes;
@Deprecated
@HdrType
public int[] getSupportedHdrTypes() {
return Arrays.copyOf(mSupportedHdrTypes, mSupportedHdrTypes.length);
}
/**
* Returns the desired content max luminance data in cd/m2 for this display.

View File

@@ -460,6 +460,36 @@ public class DisplayTest {
assertArrayEquals(sortedHdrTypes, displayMode.getSupportedHdrTypes());
}
@Test
public void testGetSupportedHdrTypesReturnsCopy() {
int[] hdrTypes = new int[]{1, 2, 3};
Display.Mode displayMode = new Display.Mode(0, 0, 0, 0, new float[0], hdrTypes);
int[] hdrTypesCopy = displayMode.getSupportedHdrTypes();
hdrTypesCopy[0] = 0;
assertArrayEquals(hdrTypes, displayMode.getSupportedHdrTypes());
}
@Test
public void testGetAlternativeRefreshRatesReturnsCopy() {
float[] alternativeRates = new float[]{1.0f, 2.0f};
Display.Mode displayMode = new Display.Mode(0, 0, 0, 0, alternativeRates, new int[0]);
float[] alternativeRatesCopy = displayMode.getAlternativeRefreshRates();
alternativeRatesCopy[0] = 0.0f;
assertArrayEquals(alternativeRates, displayMode.getAlternativeRefreshRates(), 0.0f);
}
@Test
public void testHdrCapabilitiesGetSupportedHdrTypesReturnsCopy() {
int[] hdrTypes = new int[]{1, 2, 3};
Display.HdrCapabilities hdrCapabilities = new Display.HdrCapabilities(hdrTypes, 0, 0, 0);
int[] hdrTypesCopy = hdrCapabilities.getSupportedHdrTypes();
hdrTypesCopy[0] = 0;
assertArrayEquals(hdrTypes, hdrCapabilities.getSupportedHdrTypes());
}
// Given rotated display dimensions, calculate the letterboxed app bounds.
private static Rect buildAppBounds(int displayWidth, int displayHeight) {
final int midWidth = displayWidth / 2;