todo: stop malformed todo.txt dates and rec counts crashing import
- Commit
960df1c4b05a4487d36ee1753c40b6d02d60233b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 01:57 UTC (17 days ago)
- Parents
c27c0ce
TodoTxt.toMs called LocalDate.parse on values that only passed a digit-shape regex, so an imported line carrying 2026-13-45 or 2026-02-30 threw DateTimeFormatException out of parse(); the recurrence count used toInt() on an unbounded digit run, so rec:999999999999d threw NumberFormatException. The ViewModel's runCatching turned either into an empty import, losing every valid task in the file. toMs is now nullable via runCatching (invalid calendar date means no date, matching TodoCsv.parseOrNull) and the recurrence count uses toIntOrNull (an overflowing count is not a recurrence). Verified: ./gradlew :todo:testDevUnitTest (in the six-module run, BUILD SUCCESSFUL) and ./check todo translate travel tuner vpn (exit 0).
Diff
todo/src/main/java/com/vayunmathur/todo/domain/format/TodoTxt.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/format/TodoTxt.kt b/todo/src/main/java/com/vayunmathur/todo/domain/format/TodoTxt.ktindex 2b9358f2a2..d9f34fe7de 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/format/TodoTxt.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/format/TodoTxt.kt@@ -129,7 +129,9 @@ object TodoTxt { private fun parseRec(value: String): String? { val (strict, n, unit) = REC.find(value)?.destructured ?: return null- val count = n.toInt().coerceAtLeast(1)+ // REC allows unbounded digits in an imported file; a count that does+ // not fit an Int is not a recurrence, not a NumberFormatException.+ val count = n.toIntOrNull()?.coerceAtLeast(1) ?: return null val r = when { strict.isEmpty() && unit == "d" -> Recurrence.AfterCompletion(count) strict.isEmpty() && unit == "w" -> Recurrence.AfterCompletion(count * DAYS_PER_WEEK)@@ -141,5 +143,9 @@ object TodoTxt { return r.serialize() } - private fun toMs(date: String, zone: TimeZone) = DueDates.startOfDay(LocalDate.parse(date), zone)+ // The DATE regex only checks digit shape, so an imported file can carry+ // e.g. 2026-13-45 or 2026-02-30: LocalDate.parse throws on those. An+ // invalid calendar date means "no date", matching TodoCsv's parseOrNull.+ private fun toMs(date: String, zone: TimeZone): Long? =+ runCatching { DueDates.startOfDay(LocalDate.parse(date), zone) }.getOrNull() }