notes: cap share filename bytes to fix ENAMETOOLONG on long titles
- Commit
f9e92ad207f705708f250510be840269821a707f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:23 UTC (28 days ago)
- Parents
7f89be1
Sharing a note with a very long title crashed requestShare: the title is used raw as the cache filename, and anything over the 255-byte NAME_MAX fails the cache write with FileNotFoundException (ENAMETOOLONG). shareMarkdownFileName now truncates the sanitized title to 200 UTF-8 bytes, cutting on a code-point boundary so multi-byte characters survive intact, and falls back to 'note' when nothing remains. A 300-char ASCII title and a 150-char CJK title both failed before this fix and now write successfully, covered by markdownCacheUriHandlesVeryLongTitle. Verified: full :notes:testDevUnitTest suite green (153 tests, 0 failures).
Diff
notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.ktindex 936067832d..8f12c8258a 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt@@ -58,10 +58,27 @@ fun exportNoteMarkdown(context: Context, note: Note): String = * it can contain `/` or `..`, which either makes the write fail (missing * parent dir, crashing the share) or escapes the shared_notes directory so * FileProvider refuses the URI. Strip path-hostile characters instead.+ *+ * Also caps the UTF-8 length well under NAME_MAX (255) so very long titles+ * don't fail the cache write with ENAMETOOLONG. */+private const val MAX_SHARE_FILENAME_BYTES = 200+ internal fun shareMarkdownFileName(title: String): String {- val cleaned = title.trim().replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_").trim('.')- return "${cleaned.ifBlank { "note" }}.md"+ val cleaned = title.trim()+ .replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_")+ .trim('.')+ val truncated = truncateUtf8Bytes(cleaned, MAX_SHARE_FILENAME_BYTES).trim('.')+ return "${truncated.ifBlank { "note" }}.md"+}++/** Prefix of [s] fitting in [maxBytes] UTF-8 bytes, cut on a code-point boundary. */+private fun truncateUtf8Bytes(s: String, maxBytes: Int): String {+ val bytes = s.toByteArray()+ if (bytes.size <= maxBytes) return s+ var end = maxBytes+ while (end > 0 && bytes[end].toInt() and 0xC0 == 0x80) end--+ return String(bytes, 0, end, Charsets.UTF_8) } fun markdownCacheUri(context: Context, note: Note, markdown: String): Uri {
notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.ktindex 483a2bacf4..fd49817012 100644--- a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt@@ -157,6 +157,18 @@ class NoteStoresTest { assertTrue(blank.toString().isNotEmpty()) } + @Test fun markdownCacheUriHandlesVeryLongTitle() {+ // A 300-char title exceeds the 255-byte NAME_MAX: the cache write+ // must still succeed instead of throwing (which crashes requestShare).+ for (title in listOf("x".repeat(300), "字".repeat(150))) {+ val uri = markdownCacheUri(context, Note(title = title, content = ""), "body")+ assertTrue(uri.toString().isNotEmpty())+ val written = File(File(context.cacheDir, "shared_notes"), shareMarkdownFileName(title))+ assertTrue(written.exists(), title.take(20))+ assertTrue(written.name.toByteArray().size <= 255, written.name)+ }+ }+ @Test fun exportInkAlphaAndFractionalValues() { val stroke = SerializedStroke("pen", 2.567f, 0x80102030.toInt(), 0.1f, listOf(SerializedPoint(1.234f, 2f, 0), SerializedPoint(3f, 4.891f, 1))) val note = Note(title = "", content = "").withBody(NoteBody(listOf(NoteBlock.Ink(listOf(stroke), heightDp = 10, id = "k"))))