From edf6ccaeb3c66baa331aa55fe0fcdc5c4697f3e7 Mon Sep 17 00:00:00 2001 From: Nathan Harold Date: Fri, 7 Sep 2018 11:50:05 -0700 Subject: [PATCH] Don't Use NPE for Flow Control in getCellLocation Remove catch of NPE from getCellLocation and do explicit null checks. This will make it easier to differentiate between actual problems and local NPEs. In pre-P, there were multiple conditions that would intentionally return null over the binder interface. In Post-P null is returned when location is disabled, which means that null is a very common return value. We shouldn't use NPE to handle the case where location is disabled. Bug: 114294321 Test: manual w/ SL4A (no automated tests for TM) Change-Id: Ie5b3afa56f280ff901ff9fa7c93f958d754f109b --- .../java/android/telephony/TelephonyManager.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java index e6173c0717d41..3672671ccb5cb 100644 --- a/telephony/java/android/telephony/TelephonyManager.java +++ b/telephony/java/android/telephony/TelephonyManager.java @@ -1482,23 +1482,24 @@ public class TelephonyManager { Rlog.d(TAG, "getCellLocation returning null because telephony is null"); return null; } + Bundle bundle = telephony.getCellLocation(mContext.getOpPackageName()); - if (bundle.isEmpty()) { - Rlog.d(TAG, "getCellLocation returning null because bundle is empty"); + if (bundle == null || bundle.isEmpty()) { + Rlog.d(TAG, "getCellLocation returning null because CellLocation is unavailable"); return null; } + CellLocation cl = CellLocation.newFromBundle(bundle); - if (cl.isEmpty()) { - Rlog.d(TAG, "getCellLocation returning null because CellLocation is empty"); + if (cl == null || cl.isEmpty()) { + Rlog.d(TAG, "getCellLocation returning null because CellLocation is empty or" + + " phone type doesn't match CellLocation type"); return null; } + return cl; } catch (RemoteException ex) { Rlog.d(TAG, "getCellLocation returning null due to RemoteException " + ex); return null; - } catch (NullPointerException ex) { - Rlog.d(TAG, "getCellLocation returning null due to NullPointerException " + ex); - return null; } }