From b78aad77508bc493d86c298edfa00fda015bc7bf Mon Sep 17 00:00:00 2001 From: John Rodriguez Date: Mon, 20 Aug 2018 14:11:26 -0400 Subject: [PATCH 1/3] Don't NPE when getSystemService returns null on connectivity change --- sdk/src/main/java/com/bugsnag/android/Client.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sdk/src/main/java/com/bugsnag/android/Client.java b/sdk/src/main/java/com/bugsnag/android/Client.java index ff5727e10c..f684b7e914 100644 --- a/sdk/src/main/java/com/bugsnag/android/Client.java +++ b/sdk/src/main/java/com/bugsnag/android/Client.java @@ -233,6 +233,10 @@ private class ConnectivityChangeReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm == null) { + return; + } + NetworkInfo networkInfo = cm.getActiveNetworkInfo(); boolean retryReports = networkInfo != null && networkInfo.isConnectedOrConnecting(); From a8d3637626b92010b26b6ce22677e6794880ff4c Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Tue, 21 Aug 2018 11:00:00 +0100 Subject: [PATCH 2/3] fix: prevent NPE by guarding access to fields obtained through context.getSystemService context.getSystemService can return a nullable value if the service was not found. Therefore the notifier should guard any access to these values with a null check to prevent NPEs. --- .../main/java/com/bugsnag/android/DefaultDelivery.java | 9 +++++++-- sdk/src/main/java/com/bugsnag/android/DeviceData.java | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/com/bugsnag/android/DefaultDelivery.java b/sdk/src/main/java/com/bugsnag/android/DefaultDelivery.java index 3c65791d7c..5095306801 100644 --- a/sdk/src/main/java/com/bugsnag/android/DefaultDelivery.java +++ b/sdk/src/main/java/com/bugsnag/android/DefaultDelivery.java @@ -2,6 +2,7 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.support.annotation.Nullable; import java.io.BufferedWriter; import java.io.IOException; @@ -14,9 +15,9 @@ class DefaultDelivery implements Delivery { - private final ConnectivityManager connectivityManager; + @Nullable private final ConnectivityManager connectivityManager; - DefaultDelivery(ConnectivityManager connectivityManager) { + DefaultDelivery(@Nullable ConnectivityManager connectivityManager) { this.connectivityManager = connectivityManager; } @@ -85,6 +86,10 @@ int deliver(String urlString, } private void checkHasNetworkConnection() throws DeliveryFailureException { + if (connectivityManager == null) { + return; // unlikely case, allow delivery attempt without checking connection first + } + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); // conserve device battery by avoiding radio use diff --git a/sdk/src/main/java/com/bugsnag/android/DeviceData.java b/sdk/src/main/java/com/bugsnag/android/DeviceData.java index 9bea89259c..ed8c91036b 100644 --- a/sdk/src/main/java/com/bugsnag/android/DeviceData.java +++ b/sdk/src/main/java/com/bugsnag/android/DeviceData.java @@ -365,6 +365,11 @@ private String getNetworkAccess() { try { ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE); + + if (cm == null) { + return null; + } + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) { if (activeNetwork.getType() == 1) { From 023f8ffc289d9c4b77cffac00ba4bd9a5c211d5f Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Tue, 21 Aug 2018 11:12:57 +0100 Subject: [PATCH 3/3] docs: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ad50ea93f..e027e8689a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * Catch Exception within DefaultDelivery class [#361](https://github.com/bugsnag/bugsnag-android/pull/361) +* Add Null check when accessing system service [#367](https://github.com/bugsnag/bugsnag-android/pull/367) ## 4.6.0 (2018-08-02)