Modern Apps git

← Modern-Apps.git

logviewer: budget snapshot filenames in bytes, not chars

Commit
069325de16c262aca6232496115fdfe2b9d88b49
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:08 UTC (29 days ago)
Parents
cfc8ee4
Symptom: Save could fail (and the share display name garble)
for logs whose title is heavy in multibyte characters. Titles
are built from the crashed app's label plus the user's filter
regex, so a CJK label or regex is enough.

Root cause: snapshotFileName truncated the title to 200 chars,
but filename limits count UTF-8 bytes (255 on ext4/F2FS and the
providers behind the document picker). 200 three-byte chars is a
600-byte title - 617 bytes with the uuid suffix - and providers
reject or mangle names like that.

Fix: trim the title to a 200-byte UTF-8 budget, walking code
points so a surrogate pair at the boundary is dropped whole
rather than split. ASCII titles (all existing behaviour and
tests) are byte-for-char identical.

Regression: LogDocumentExtraTest asserts a 300-char CJK title
yields a name within 217 UTF-8 bytes with exactly the 66 chars
that fit; domain suite green.

Diff

logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.ktindex 37fa4531ea..b558902e9d 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt@@ -118,9 +118,14 @@ class LogDocument(         /**          * A name for the saved or shared file: the title, then twelve hex characters so two saves          * of the same log do not collide.+         *+         * The title budget is in UTF-8 bytes, not chars: filename limits (255 bytes on the+         * filesystems behind the document picker) count bytes, so 200 three-byte chars made a+         * 600-byte name that providers reject. Titles reach here from app labels and from the+         * user's own filter regex, so multibyte titles are not exotic.          */         fun snapshotFileName(title: String): String =-            trimToSize(title, 200) + ' ' + UUID.randomUUID().toString().substring(24) + ".txt"+            trimToUtf8Size(title, 200) + ' ' + UUID.randomUUID().toString().substring(24) + ".txt"          /**          * Trailing empty strings are dropped, so a body ending in a newline does not gain a blank@@ -153,10 +158,22 @@ class LogDocument(             return bytes         } -        /** [android.text.TextUtils.trimToSize], reimplemented so this file stays Android-free. */-        private fun trimToSize(s: String, maxLength: Int): String {-            if (s.length <= maxLength) return s-            val end = if (Character.isHighSurrogate(s[maxLength - 1])) maxLength - 1 else maxLength+        /** Longest prefix of [s] fitting in [maxBytes] of UTF-8, never splitting a code point. */+        private fun trimToUtf8Size(s: String, maxBytes: Int): String {+            var bytes = 0+            var end = 0+            while (end < s.length) {+                val codePoint = Character.codePointAt(s, end)+                val codePointBytes = when {+                    codePoint < 0x80 -> 1+                    codePoint < 0x800 -> 2+                    codePoint < 0x10000 -> 3+                    else -> 4+                }+                if (bytes + codePointBytes > maxBytes) break+                bytes += codePointBytes+                end += Character.charCount(codePoint)+            }             return s.substring(0, end)         }     }
logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt
diff --git a/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt b/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.ktindex dd84f0c6ce..223ce05807 100644--- a/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt+++ b/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt@@ -113,6 +113,17 @@ class LogDocumentExtraTest {         assertTrue(name.startsWith("a".repeat(200)))     } +    @Test+    fun `snapshot file name budget is in utf-8 bytes not chars`() {+        // 300 three-byte chars: a 200-char budget would emit a 600-byte filename,+        // over the 255-byte limit of the filesystems saves land on.+        val name = LogDocument.snapshotFileName("一".repeat(300))+        assertTrue(name.toByteArray(Charsets.UTF_8).size <= 200 + 1 + 12 + 4)+        assertTrue(name.endsWith(".txt"))+        // 66 chars fit in 200 bytes (198); the 67th does not, and nothing is mangled.+        assertEquals(66, name.count { it == '一' })+    }+     @Test     fun `snapshot file name does not split a surrogate pair at the boundary`() {         // Char 200 (index 199) is a high surrogate: trimming must drop it too.