From bdcd9257765ba26a09d23b7ceacd88e39571f9b3 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Mon, 24 Apr 2023 15:58:52 -0700 Subject: [PATCH] Fix connection enumeration not accounting for discontinuous IDs The data structure that contains mConnections is a sparse array. This means that you are allowed to insert devices with unique input IDs that are not continuous. However, when trying to perform the search for a specific input ID, the current logic assumes that the IDs are numerically continuous, causing issues (see ag/22753602). Bug: 278628657 Test: atest TvInputService Change-Id: I0311ac994feca73e4216744a49f1731c8e353ca1 --- .../com/android/server/tv/TvInputHardwareManager.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/tv/TvInputHardwareManager.java b/services/core/java/com/android/server/tv/TvInputHardwareManager.java index 98dfb009e4efa..3cb183fe07e22 100755 --- a/services/core/java/com/android/server/tv/TvInputHardwareManager.java +++ b/services/core/java/com/android/server/tv/TvInputHardwareManager.java @@ -459,9 +459,11 @@ class TvInputHardwareManager implements TvInputHal.Callback { private int findDeviceIdForInputIdLocked(String inputId) { for (int i = 0; i < mConnections.size(); ++i) { - Connection connection = mConnections.get(i); - if (connection.getInfoLocked().getId().equals(inputId)) { - return i; + int key = mConnections.keyAt(i); + Connection connection = mConnections.get(key); + if (connection != null && connection.getInfoLocked() != null + && connection.getInfoLocked().getId().equals(inputId)) { + return key; } } return -1;