Modern Apps git

← Modern-Apps.git

travel: guard cursor columns, saved-state restore and slice index

Commit
4129a32c4aa8c72c6f767e25bc130fd7dc18688f
Author
Vayun Mathur <[email protected]>
When
2026-11-19 01:58 UTC (17 days ago)
Parents
713278a
Three crash paths on external/stale data:

- ContactAutofill only checked the MIME cursor column is present. A contacts-picker provider answering the session URI without DATA1/DATA2/DATA3 reports -1 for them and Cursor.getString(-1)/getInt(-1) throws CursorIndexOutOfBounds when autofilling a passenger. The DATA columns are now each guarded before use (the display-name fallback already covers a missing structured name).
- FlightFormSaver/StayFormSaver restore cast list entries and called TripType/Cabin/MaxStops.valueOf unchecked. Saved state survives process death across app upgrades, so a renamed enum or an older list shape threw ClassCast/ IllegalArgument on restore and crashed the search form. Restore now runCatches and falls back to the default form.
- ChangePage indexed order.slices[selectedSliceIndex] in the button onClick; if the order reloads with fewer slices while an index is selected that is an IndexOutOfBounds. It now goes through getOrNull (matching the enabled check beside it) and no-ops when absent.

Verified: ./gradlew :travel:testDevUnitTest (six-module run, BUILD SUCCESSFUL) and ./check todo translate travel tuner vpn (exit 0).

Diff

travel/src/main/java/com/vayunmathur/travel/platform/ContactAutofill.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/platform/ContactAutofill.kt b/travel/src/main/java/com/vayunmathur/travel/platform/ContactAutofill.ktindex fd7b84dfe3..c777eb322d 100644--- a/travel/src/main/java/com/vayunmathur/travel/platform/ContactAutofill.kt+++ b/travel/src/main/java/com/vayunmathur/travel/platform/ContactAutofill.kt@@ -109,6 +109,9 @@ private fun parseContactCursor(c: Cursor): ContactInfo? {         data3 = c.getColumnIndex(ContactsContract.Data.DATA3),         displayName = c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME),     )+    // A provider that answers the session URI without every projected+    // column reports -1 for the missing ones; getString(-1) throws+    // CursorIndexOutOfBounds. Only the MIME column is load-bearing.     if (cols.mime < 0) return null     val acc = ContactAccumulator()     while (c.moveToNext()) {@@ -127,19 +130,19 @@ private fun ContactAccumulator.readDisplayName(c: Cursor, cols: ContactColumns) private fun ContactAccumulator.readRow(c: Cursor, cols: ContactColumns) {     when (c.getString(cols.mime)) {         StructuredName.CONTENT_ITEM_TYPE -> readStructuredName(c, cols)-        Email.CONTENT_ITEM_TYPE -> if (email.isBlank()) email = c.getString(cols.data1).orEmpty()-        Phone.CONTENT_ITEM_TYPE -> if (phone.isBlank()) phone = c.getString(cols.data1).orEmpty()+        Email.CONTENT_ITEM_TYPE -> if (email.isBlank() && cols.data1 >= 0) email = c.getString(cols.data1).orEmpty()+        Phone.CONTENT_ITEM_TYPE -> if (phone.isBlank() && cols.data1 >= 0) phone = c.getString(cols.data1).orEmpty()         Event.CONTENT_ITEM_TYPE -> readBirthday(c, cols)     } }  private fun ContactAccumulator.readStructuredName(c: Cursor, cols: ContactColumns) {-    given = c.getString(cols.data2).orEmpty()-    family = c.getString(cols.data3).orEmpty()+    if (cols.data2 >= 0) given = c.getString(cols.data2).orEmpty()+    if (cols.data3 >= 0) family = c.getString(cols.data3).orEmpty() }  private fun ContactAccumulator.readBirthday(c: Cursor, cols: ContactColumns) {-    if (birthday.isNotBlank() || !isBirthdayRow(c, cols)) return+    if (birthday.isNotBlank() || cols.data1 < 0 || !isBirthdayRow(c, cols)) return     birthday = isoBirthday(c.getString(cols.data1)) } 
travel/src/main/java/com/vayunmathur/travel/ui/ChangePage.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/ui/ChangePage.kt b/travel/src/main/java/com/vayunmathur/travel/ui/ChangePage.ktindex 9df398a663..2e1b9aed09 100644--- a/travel/src/main/java/com/vayunmathur/travel/ui/ChangePage.kt+++ b/travel/src/main/java/com/vayunmathur/travel/ui/ChangePage.kt@@ -104,15 +104,19 @@ fun ChangePage(                 DateField(stringResource(R.string.new_date), newDate, onDate = { newDate = it }, modifier = Modifier.fillMaxWidth())                 Button(                     onClick = {-                        val slice = order.slices[selectedSliceIndex]-                        viewModel.requestChange(-                            orderId = route.orderId,-                            removeSliceId = slice.id,-                            origin = slice.origin,-                            destination = slice.destination,-                            newDate = newDate,-                            cabin = "",-                        )+                        // The order can reload shorter while an index is+                        // selected; indexing directly would go out of bounds.+                        val slice = order.slices.getOrNull(selectedSliceIndex)+                        if (slice != null) {+                            viewModel.requestChange(+                                orderId = route.orderId,+                                removeSliceId = slice.id,+                                origin = slice.origin,+                                destination = slice.destination,+                                newDate = newDate,+                                cabin = "",+                            )+                        }                     },                     enabled = newDate.isNotBlank() && order.slices.getOrNull(selectedSliceIndex)?.id?.isNotBlank() == true,                     modifier = Modifier.fillMaxWidth(),
travel/src/main/java/com/vayunmathur/travel/ui/SearchFormState.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/ui/SearchFormState.kt b/travel/src/main/java/com/vayunmathur/travel/ui/SearchFormState.ktindex 993ae2cfbc..2e158b96ca 100644--- a/travel/src/main/java/com/vayunmathur/travel/ui/SearchFormState.kt+++ b/travel/src/main/java/com/vayunmathur/travel/ui/SearchFormState.kt@@ -49,9 +49,15 @@ val FlightFormSaver: Saver<FlightFormState, Any> = listSaver(             it.mix.infantsInSeat, it.mix.infantsOnLap, it.cabin.name, it.maxStops.name,         )     },-    restore = {-        @Suppress("UNCHECKED_CAST")-        FlightFormState(+    restore = { saved ->+        // Saved state survives process death across app upgrades: an enum+        // renamed/removed or a list written by an older version makes+        // valueOf / the casts below throw, crashing the form on restore.+        // Any mismatch restores the default form instead.+        runCatching {+            @Suppress("UNCHECKED_CAST")+            val it = saved+            FlightFormState(             tripType = TripType.valueOf(it[0] as String),             origin = it[1] as String,             originText = it[2] as String,@@ -67,7 +73,8 @@ val FlightFormSaver: Saver<FlightFormState, Any> = listSaver(             ),             cabin = Cabin.valueOf(it[11] as String),             maxStops = MaxStops.valueOf(it[12] as String),-        )+            )+        }.getOrDefault(FlightFormState())     }, ) @@ -78,8 +85,11 @@ val StayFormSaver: Saver<StayFormState, Any> = listSaver(             it.checkIn, it.checkOut, it.rooms, it.guests,         )     },-    restore = {-        StayFormState(+    restore = { saved ->+        // Same stale-saved-state hazard as FlightFormSaver above.+        runCatching {+            val it = saved+            StayFormState(             place = it[0] as String,             placeText = it[1] as String,             latitude = (it[2] as Double).takeUnless(Double::isNaN),@@ -88,6 +98,7 @@ val StayFormSaver: Saver<StayFormState, Any> = listSaver(             checkOut = it[5] as String,             rooms = it[6] as Int,             guests = it[7] as Int,-        )+            )+        }.getOrDefault(StayFormState())     }, )