contacts: vCard import no longer mangles literal backslash-n into a newline
- Commit
4b9f48555fc6c46c73450a72758e8151ffd812bf- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 00:38 UTC (5 days ago)
- Parents
3eaa423
unescapeV used chained String.replace calls โ backslash-n to LF first, double-backslash to backslash last โ so the second backslash of an escaped pair was re-interpreted as the start of a new escape. A note reading C:\new folder (whose own export writes NOTE:C:\\new folder) imported as 'C:' + newline + 'ew folder'; the same corruption hit literal backslash before N, comma or semicolon, and it compounded through every split component (N, ADR, GENDER, CATEGORIES) that funnels through this one function. Unescaping is now a single left-to-right pass: a backslash and the following character are consumed as one unit, unknown escapes and a trailing lone backslash are kept verbatim. Regression tests decode a foreign card and round-trip our own export (both failed pre-fix). Merge/dedup, search, sorting and photo import were audited and cleared. Verified: targeted vCard suites and lint pass; the full suite's 3 failures are pre-existing/flaky Compose UI tests in files carrying other agents' uncommitted changes, confirmed failing identically with the original decoder restored.
Diff
contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt b/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.ktindex 23ddbe4be6..4e26e56681 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt@@ -438,13 +438,28 @@ private class ContactBuilder { } private fun unescapeV(value: String): String {- // Unescape backslash LAST to avoid double-processing.- return value- .replace("\\n", "\n")- .replace("\\N", "\n")- .replace("\\,", ",")- .replace("\\;", ";")- .replace("\\\\", "\\")+ // Single left-to-right pass: chained replaces decoded the second+ // backslash of an escaped "\\" as the start of a new escape, so a+ // literal "\n" in the source (exported as "\\n", e.g. the path+ // "C:\new") came back as a real newline ("C:\" + LF + "ew").+ val out = StringBuilder(value.length)+ var i = 0+ while (i < value.length) {+ val c = value[i]+ if (c == '\\' && i + 1 < value.length) {+ when (value[i + 1]) {+ 'n', 'N' -> out.append('\n')+ ',', ';', '\\' -> out.append(value[i + 1])+ // Unknown escape: keep both characters verbatim.+ else -> out.append(c).append(value[i + 1])+ }+ i += 2+ } else {+ out.append(c)+ i+++ }+ }+ return out.toString() } private fun splitUnescaped(value: String, delimiter: Char): List<String> {
contacts/src/test/java/com/vayunmathur/contacts/pure/BugcheckVcardRegressionTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/BugcheckVcardRegressionTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/BugcheckVcardRegressionTest.ktindex 58f2884777..937795f98b 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/pure/BugcheckVcardRegressionTest.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/BugcheckVcardRegressionTest.kt@@ -112,6 +112,33 @@ class BugcheckVcardRegressionTest { assertEquals(7L, parsed.details.groups.single().groupId) } + @Test+ fun escapedBackslashBeforeNIsNotDecodedAsNewline() {+ // Foreign card: "C:\\new" is an escaped backslash followed by 'n',+ // i.e. the literal path C:\new โ not "C:\" + newline + "ew".+ val vcf = """+ BEGIN:VCARD+ VERSION:3.0+ FN:Path Note+ NOTE:C:\\new folder+ END:VCARD+ """.trimIndent()+ val parsed = VcfUtils.parseContacts(+ ByteArrayInputStream(vcf.toByteArray(Charsets.UTF_8))+ ).single()+ assertEquals("C:\\new folder", parsed.details.notes.single().content)+ }++ @Test+ fun literalBackslashNSurvivesExportImportRoundTrip() {+ val details = ContactDetails.empty().copy(+ names = listOf(Name(0, "", "Ada", "", "Lovelace", "")),+ notes = listOf(com.vayunmathur.contacts.data.Note(0, "C:\\new folder")),+ )+ val parsed = exportAndReparse(Contact(0L, null, null, false, details))+ assertEquals("C:\\new folder", parsed.details.notes.single().content)+ }+ @Test fun homeAddressStillRoundTripsAsHome() { val parsed = exportAndReparse(
contacts/src/test/java/com/vayunmathur/contacts/pure/VCardEscapeTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardEscapeTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardEscapeTest.ktindex 63834d1b71..f42f3e7843 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardEscapeTest.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardEscapeTest.kt@@ -17,12 +17,25 @@ class VCardEscapeTest { } private fun unescapeV(value: String): String {- return value- .replace("\\n", "\n")- .replace("\\N", "\n")- .replace("\\,", ",")- .replace("\\;", ";")- .replace("\\\\", "\\")+ // Single left-to-right pass, mirroring production: chained replaces+ // would decode the second backslash of "\\" as a new escape start.+ val out = StringBuilder(value.length)+ var i = 0+ while (i < value.length) {+ val c = value[i]+ if (c == '\\' && i + 1 < value.length) {+ when (value[i + 1]) {+ 'n', 'N' -> out.append('\n')+ ',', ';', '\\' -> out.append(value[i + 1])+ else -> out.append(c).append(value[i + 1])+ }+ i += 2+ } else {+ out.append(c)+ i+++ }+ }+ return out.toString() } @Test@@ -60,6 +73,14 @@ class VCardEscapeTest { assertEquals("a\\\nb", unescapeV("a\\\\\\nb")) } + @Test+ fun escapedBackslashNDecodesToLiteralBackslashN() {+ // "C:\\new" on the wire is an escaped backslash + 'n': the literal+ // path C:\new, not "C:\" + newline + "ew".+ assertEquals("C:\\new folder", unescapeV("C:\\\\new folder"))+ assertEquals("a\\nb", unescapeV(escapeV("a\\nb")))+ }+ @Test fun fullRoundTripPreservesEverySpecial() { val raw = "line1\nline2, with; specials \\ done"