app: don't crash on broken module metadata

Modules are third-party, but some of their metadata could crash the
app:

- A malformed updateJson URL (e.g. ftp://) made Retrofit throw
  IllegalArgumentException, which LocalModule.fetch() didn't catch, so
  the app exited every time the module list loaded, even offline. A
  malformed changelog URL did the same when opening the update dialog.
- Two module directories with the same id in module.prop crashed the
  module list, which used the id as the item key.

Catch every non-cancellation exception when fetching update info and
changelogs, and identify modules by their directory name, which is
unique.

Assisted-by: Claude Opus 5.5
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 4384cf49fa
commit 18112c7366
4 changed files with 16 additions and 11 deletions
@@ -65,7 +65,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.IOException import kotlin.coroutines.cancellation.CancellationException
import kotlin.coroutines.resume import kotlin.coroutines.resume
sealed interface ConfirmResult { sealed interface ConfirmResult {
@@ -642,7 +642,10 @@ fun MarkdownTextAsync(
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
try { try {
mdText = withContext(Dispatchers.IO) { getMarkdownText() } mdText = withContext(Dispatchers.IO) { getMarkdownText() }
} catch (e: IOException) { } catch (e: CancellationException) {
throw e
} catch (e: Exception) {
// The text can come from a URL provided by a module, which can be malformed
Timber.e(e) Timber.e(e)
error = true error = true
} }
@@ -253,7 +253,8 @@ fun ModuleScreen(
) { ) {
items( items(
items = uiState.modules, items = uiState.modules,
key = { it.module.id }, // Module ids come from module.prop and can be duplicated, directory names can't
key = { it.module.base.name },
contentType = { "ModuleCard" } contentType = { "ModuleCard" }
) { item -> ) { item ->
val isLast = item == uiState.modules.lastOrNull() val isLast = item == uiState.modules.lastOrNull()
@@ -285,7 +286,7 @@ private fun ModuleCard(
val infoAlpha = if (!item.isRemoved && item.isEnabled && !item.showNotice) 1f else 0.5f val infoAlpha = if (!item.isRemoved && item.isEnabled && !item.showNotice) 1f else 0.5f
val strikeThrough = if (item.isRemoved) TextDecoration.LineThrough else TextDecoration.None val strikeThrough = if (item.isRemoved) TextDecoration.LineThrough else TextDecoration.None
val colorScheme = MaterialTheme.colorScheme val colorScheme = MaterialTheme.colorScheme
var expanded by rememberSaveable(item.module.id) { mutableStateOf(false) } var expanded by rememberSaveable(item.module.base.name) { mutableStateOf(false) }
val hasDescription = item.module.description.isNotBlank() val hasDescription = item.module.description.isNotBlank()
Card( Card(
@@ -127,7 +127,7 @@ class ModuleViewModel : AsyncLoadViewModel() {
_uiState.update { state -> _uiState.update { state ->
state.copy( state.copy(
modules = state.modules.map { modules = state.modules.map {
if (it.module.id == item.module.id) it.copy(isEnabled = newEnabled) else it if (it.module.base.name == item.module.base.name) it.copy(isEnabled = newEnabled) else it
} }
) )
} }
@@ -139,7 +139,7 @@ class ModuleViewModel : AsyncLoadViewModel() {
_uiState.update { state -> _uiState.update { state ->
state.copy( state.copy(
modules = state.modules.map { modules = state.modules.map {
if (it.module.id == item.module.id) it.copy(isRemoved = newRemoved) else it if (it.module.base.name == item.module.base.name) it.copy(isRemoved = newRemoved) else it
} }
) )
} }
@@ -1,6 +1,5 @@
package com.topjohnwu.magisk.core.model.module package com.topjohnwu.magisk.core.model.module
import com.squareup.moshi.JsonDataException
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.di.ServiceLocator
import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.magisk.core.utils.RootUtils
@@ -9,8 +8,8 @@ import com.topjohnwu.superuser.nio.ExtendedFile
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.IOException
import java.util.Locale import java.util.Locale
import kotlin.coroutines.cancellation.CancellationException
data class LocalModule( data class LocalModule(
val base: ExtendedFile, val base: ExtendedFile,
@@ -110,9 +109,11 @@ data class LocalModule(
updateInfo = OnlineModule(this, json) updateInfo = OnlineModule(this, json)
outdated = json.versionCode > versionCode outdated = json.versionCode > versionCode
return true return true
} catch (e: IOException) { } catch (e: CancellationException) {
Timber.w(e) throw e
} catch (e: JsonDataException) { } catch (e: Exception) {
// The URL and the JSON come from the module, so anything can be wrong with them,
// e.g. a malformed URL makes Retrofit throw IllegalArgumentException
Timber.w(e) Timber.w(e)
} }