Modern Apps git

← Modern-Apps.git

clock: validate SET_ALARM intent extras before constructing time

Commit
c1a7c7e51fb04028b50cbf993f83bf83ce0f8616
Author
Vayun Mathur <[email protected]>
When
2026-11-19 01:53 UTC (17 days ago)
Parents
cb61508
MainActivity is exported for AlarmClock.ACTION_SET_ALARM /
ACTION_SET_TIMER, so EXTRA_HOUR / EXTRA_MINUTES / EXTRA_DAYS come
from any sender holding the normal SET_ALARM permission.
handleIncomingIntent only filtered the -1 absent sentinel, so
EXTRA_HOUR=24 (or EXTRA_MINUTES=60) reached LocalTime(hour,
minutes) on the skip-UI path and rememberTimePickerState on the
dialog path, both of which throw IllegalArgumentException and
crash the activity in onCreateBeforeContent / composition.

Fix: treat hour values outside 0..23 and minute values outside
0..59 as absent (the dialog then defaults to the current time,
and skip-UI simply does not fire), and only map EXTRA_DAYS values
in Calendar.SUNDAY..SATURDAY (1..7) into the weekday bitmask in
both handleIncomingIntent and NewAlarmDialog. Previously a day
of 0 set bit 31 and silently corrupted the repeat mask.

Verification: ./gradlew :clock:testDevUnitTest and ./check clock
(run after this commit; see session report).

Diff

clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt b/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.ktindex df4817c77a..1edccb2a87 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt@@ -245,8 +245,11 @@ class ClockViewModel(         intent ?: return null         return when (intent.action) {             AlarmClock.ACTION_SET_ALARM -> {-                val hour = intent.getIntExtra(AlarmClock.EXTRA_HOUR, -1).takeIf { it != -1 }-                val minutes = intent.getIntExtra(AlarmClock.EXTRA_MINUTES, -1).takeIf { it != -1 }+                // Extras come from an exported intent, so a sender can put+                // any Int in them: LocalTime / the time picker throw on+                // out-of-range values, so treat those as absent instead.+                val hour = intent.getIntExtra(AlarmClock.EXTRA_HOUR, -1).takeIf { it in 0..23 }+                val minutes = intent.getIntExtra(AlarmClock.EXTRA_MINUTES, -1).takeIf { it in 0..59 }                 val message = intent.getStringExtra(AlarmClock.EXTRA_MESSAGE)                 val days = intent.getIntegerArrayListExtra(AlarmClock.EXTRA_DAYS)                 val skipUi = intent.getBooleanExtra(AlarmClock.EXTRA_SKIP_UI, false)@@ -254,7 +257,9 @@ class ClockViewModel(                 if (skipUi && hour != null && minutes != null) {                     val time = LocalTime(hour, minutes)                     var daysMask = 0-                    days?.forEach { day -> daysMask = daysMask or (1 shl (day - 1)) }+                    // Calendar.SUNDAY..SATURDAY are 1..7; anything else from+                    // the sender is not a weekday and must not set a bit.+                    days?.forEach { day -> if (day in 1..7) daysMask = daysMask or (1 shl (day - 1)) }                     val alarm = buildDefaultAlarm(time, message ?: "", daysMask)                     val ctx = getApplication<Application>()                     viewModelScope.launch(Dispatchers.IO) {
clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewAlarmDialog.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewAlarmDialog.kt b/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewAlarmDialog.ktindex 6c34bba181..cbfb8fe7f6 100644--- a/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewAlarmDialog.kt+++ b/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewAlarmDialog.kt@@ -82,8 +82,10 @@ fun NewAlarmDialog(                 onClick = {                     val time = LocalTime(timePickerState.hour, timePickerState.minute)                     var daysMask = 0+                    // Only Calendar.SUNDAY..SATURDAY (1..7) are weekdays;+                    // other values arriving via the SET_ALARM intent are ignored.                     initialDays?.forEach { day ->-                        daysMask = daysMask or (1 shl (day - 1))+                        if (day in 1..7) daysMask = daysMask or (1 shl (day - 1))                     }                     val newAlarm = clockViewModel.buildDefaultAlarm(time, nameState.text, daysMask)                     clockViewModel.upsert(newAlarm) { id ->