Modern Apps git

← Modern-Apps.git

logviewer: reject an uncompilable filter regex with a message

Commit
4c20d0590e4c89ba3008f1a8ba2a0141364e7f22
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:02 UTC (29 days ago)
Parents
688ed60
Symptom: typing a malformed regex in Set filter (the app's only
free-text input) re-ran logcat with a --regex it rejects, and the
user got a blank log with the bad pattern printed in the title as
if it had been applied - indistinguishable from a valid filter
that matched nothing.

Root cause: nothing validated the pattern. The text went from
TextInputDialog straight into the logcat command.

Fix: LogcatReader.isValidFilterRegex compiles the pattern (empty
stays valid: it means no --regex). LogcatActivity.setFilter checks
it before reloading and, on failure, shows "Invalid regular
expression" via AppMessages and keeps the current log and filter
on screen. JVM-vs-native regex dialects differ at the margins; a
pattern accepted here but rejected by logcat now fails the load
via the executeCommand exit-code check instead of faking an
empty result.

Regression: LogcatReaderTest covers empty/valid/invalid patterns.
LogcatReaderTest + LogcatActivityTest green.

Diff

logviewer/src/main/java/com/vayunmathur/logviewer/intents/LogcatActivity.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/intents/LogcatActivity.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/intents/LogcatActivity.ktindex 024ee42da5..842e83f115 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/intents/LogcatActivity.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/intents/LogcatActivity.kt@@ -8,6 +8,8 @@ import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import com.vayunmathur.library.ui.DynamicTheme+import com.vayunmathur.library.util.AppMessages+import com.vayunmathur.logviewer.R import com.vayunmathur.logviewer.domain.LogDocument import com.vayunmathur.logviewer.domain.LogLevel import com.vayunmathur.logviewer.platform.LogLoadResult@@ -85,6 +87,14 @@ class LogcatActivity : ComponentActivity() {          override fun setLevel(level: LogLevel) = reload { copy(level = level) } -        override fun setFilter(regex: String) = reload { copy(filterRegex = regex) }+        override fun setFilter(regex: String) {+            // Keep the current log and filter when the new one cannot compile: reloading+            // would replace a good log with a failed load over a typo.+            if (!LogcatReader.isValidFilterRegex(regex)) {+                AppMessages.show(getString(R.string.error_invalid_filter_regex))+                return+            }+            reload { copy(filterRegex = regex) }+        }     } }
logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.ktindex df6c6bd975..70ca52a6ca 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/platform/LogcatReader.kt@@ -40,6 +40,17 @@ internal object LogcatReader {         val filterRegex: String,     ) +    /**+     * Whether [regex] compiles. Checked before anything is re-read: logcat rejects a bad+     * `--regex` outright, so without this the only feedback for a typo was an empty log+     * (now an Unavailable load). The empty filter is valid - it means no `--regex` at all.+     * Dialects differ at the margins (this compiles with the JVM engine, logcat with the+     * native one); a rare pattern accepted here but rejected there still fails the load+     * via the exit-code check in [executeCommand] rather than faking an empty result.+     */+    fun isValidFilterRegex(regex: String): Boolean =+        regex.isEmpty() || runCatching { Regex(regex) }.isSuccess+     /**      * Runs [command] and streams its stdout lines. Injectable for tests; production execs      * `logcat`. A [Sequence] rather than a [List] so [read] parses lines as they arrive:
logviewer/src/main/res/values/strings.xml
diff --git a/logviewer/src/main/res/values/strings.xml b/logviewer/src/main/res/values/strings.xmlindex 051840997f..09d11bad29 100644--- a/logviewer/src/main/res/values/strings.xml+++ b/logviewer/src/main/res/values/strings.xml@@ -39,6 +39,9 @@     <string name="set_filter">Set filter</string>     <!-- Hint inside the empty "Set filter" text field. -->     <string name="set_filter_editor_hint">Regex</string>+    <!-- Message when the filter text is not a compilable regular expression; the previous+         log and filter stay on screen. -->+    <string name="error_invalid_filter_regex">Invalid regular expression</string>      <!-- Shown instead of the device log when a secondary user or work profile asks for it. The          device log contains other users' activity, so only the primary user may read it. -->
logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogcatReaderTest.kt
diff --git a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogcatReaderTest.kt b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogcatReaderTest.ktindex 7423b13045..adae1dddea 100644--- a/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogcatReaderTest.kt+++ b/logviewer/src/test/java/com/vayunmathur/logviewer/platform/LogcatReaderTest.kt@@ -208,6 +208,16 @@ class LogcatReaderTest {         assertEquals(2, pulled)     } +    @Test+    fun `filter regex validation accepts empty and compilable patterns only`() {+        assertTrue(LogcatReader.isValidFilterRegex(""))+        assertTrue(LogcatReader.isValidFilterRegex("Foo.*"))+        assertTrue(LogcatReader.isValidFilterRegex("(?i)error|failure"))+        assertEquals(false, LogcatReader.isValidFilterRegex("("))+        assertEquals(false, LogcatReader.isValidFilterRegex("a{2,1}"))+        assertEquals(false, LogcatReader.isValidFilterRegex("[unclosed"))+    }+     @Test     fun `request is a data holder`() {         val request = LogcatReader.Request("pkg", listOf("main"), LogLevel.Info, "x")