Skip to content

Commit

Permalink
fix: treat NullPointerExceptions from ConnectivityManager.getActiveNe…
Browse files Browse the repository at this point in the history
…tworkInfo as null
  • Loading branch information
lemnik committed Jun 30, 2021
1 parent a5686ce commit 85ca690
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Register system callbacks on background thread
[#1292](https://github.com/bugsnag/bugsnag-android/pull/1292)

* Fix rare NullPointerExceptions from ConnectivityManager
[1311](https://github.com/bugsnag/bugsnag-android/pull/1311)

## 5.9.5 (2021-06-25)

* Unity: Properly handle ANRs after multiple calls to autoNotify and autoDetectAnrs
Expand Down
1 change: 1 addition & 0 deletions bugsnag-android-core/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ID>ProtectedMemberInFinalClass:EventInternal.kt$EventInternal$protected fun updateSeverityInternal(severity: Severity)</ID>
<ID>ReturnCount:DefaultDelivery.kt$DefaultDelivery$fun deliver( urlString: String, streamable: JsonStream.Streamable, headers: Map&lt;String, String?&gt; ): DeliveryStatus</ID>
<ID>SwallowedException:AppDataCollector.kt$AppDataCollector$catch (exception: Exception) { logger.w("Could not check lowMemory status") }</ID>
<ID>SwallowedException:ConnectivityCompat.kt$ConnectivityLegacy$catch (e: RuntimeException) { null }</ID>
<ID>SwallowedException:ContextExtensions.kt$catch (exc: RuntimeException) { null }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (exc: Exception) { false }</ID>
<ID>SwallowedException:DeviceDataCollector.kt$DeviceDataCollector$catch (exception: Exception) { logger.w("Could not get battery status") }</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ internal class ConnectivityLegacy(

private val changeReceiver = ConnectivityChangeReceiver(callback)

private val activeNetworkInfo: android.net.NetworkInfo?
get() = try {
cm.activeNetworkInfo
} catch (e: NullPointerException) {
// in some rare cases we get a remote NullPointerException via Parcel.readException
null
}

override fun registerForNetworkChanges() {
val intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
context.registerReceiverSafe(changeReceiver, intentFilter)
Expand All @@ -69,11 +77,11 @@ internal class ConnectivityLegacy(
override fun unregisterForNetworkChanges() = context.unregisterReceiverSafe(changeReceiver)

override fun hasNetworkConnection(): Boolean {
return cm.activeNetworkInfo?.isConnectedOrConnecting ?: false
return activeNetworkInfo?.isConnectedOrConnecting ?: false
}

override fun retrieveNetworkAccessState(): String {
return when (cm.activeNetworkInfo?.type) {
return when (activeNetworkInfo?.type) {
null -> "none"
ConnectivityManager.TYPE_WIFI -> "wifi"
ConnectivityManager.TYPE_ETHERNET -> "ethernet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ class ConnectivityLegacyTest {
Mockito.`when`(info.type).thenReturn(1)
assertEquals("wifi", conn.retrieveNetworkAccessState())
}

@Test
fun hasNetworkConnectionNullPointerException() {
val conn = ConnectivityLegacy(context, cm, null)

Mockito.`when`(cm.activeNetworkInfo).thenThrow(NullPointerException())
assertFalse(conn.hasNetworkConnection())
}

@Test
fun retrieveNetworkAccessStateNullPointerException() {
val conn = ConnectivityLegacy(context, cm, null)

Mockito.`when`(cm.activeNetworkInfo).thenThrow(NullPointerException())
assertEquals("none", conn.retrieveNetworkAccessState())
}
}

0 comments on commit 85ca690

Please sign in to comment.