Skip to content

Commit fabbd7a

Browse files
david-allisonBrayanDSO
authored andcommitted
fix(tags-dialog): crash on unicode tags
When deserializing, we called the wrong method so there was an unknown byte as a prefix This then failed the string -> JSON conversion fixes 16476
1 parent 5ebb74c commit fabbd7a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import com.ichi2.anki.R
3131
import com.ichi2.anki.analytics.AnalyticsDialogFragment
3232
import com.ichi2.anki.model.CardStateFilter
3333
import com.ichi2.anki.snackbar.showSnackbar
34-
import com.ichi2.anki.utils.ext.convertToString
3534
import com.ichi2.annotations.NeedsTest
3635
import com.ichi2.utils.DisplayUtils.resizeWhenSoftInputShown
3736
import com.ichi2.utils.TagsUtil
@@ -415,13 +414,13 @@ class TagsFile(path: String) : File(path), Parcelable {
415414
// https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/formats.md
416415
val string = Json.encodeToString(data)
417416
Timber.d("persisting tags to disk, length: %d", string.length)
418-
outputStream.writeBytes(string)
417+
outputStream.writeUTF(string)
419418
}
420419
}
421420

422421
fun getData() = DataInputStream(FileInputStream(this)).use { inputStream ->
423422
// PERF!!: This takes ~2 seconds with AnKing
424-
Json.decodeFromString<TagsData>(inputStream.convertToString())
423+
Json.decodeFromString<TagsData>(inputStream.readUTF())
425424
}
426425

427426
override fun describeContents(): Int = 0

AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import org.junit.Test
4444
import org.junit.runner.RunWith
4545
import org.mockito.Mockito
4646
import org.mockito.kotlin.whenever
47+
import timber.log.Timber
4748
import java.util.concurrent.atomic.AtomicReference
4849

4950
@RunWith(AndroidJUnit4::class)
@@ -636,6 +637,21 @@ class TagsDialogTest : RobolectricTest() {
636637
}
637638
}
638639

640+
@Test
641+
fun `unicode tags can be serialized 16576`() {
642+
val type = TagsDialog.DialogType.FILTER_BY_TAG
643+
val allTags = listOf("02动作状态")
644+
645+
val args = TagsDialog(ParametersUtils.whatever())
646+
.withTestArguments(type, emptyList(), allTags)
647+
.arguments
648+
val mockListener = Mockito.mock(TagsDialogListener::class.java)
649+
val factory = TagsDialogFactory(mockListener)
650+
val scenario = FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory)
651+
scenario.moveToState(Lifecycle.State.STARTED)
652+
scenario.onFragment { Timber.d("Dialog successfully opened") }
653+
}
654+
639655
// these are called 'withTestArguments' due to "extension is shadowed by a member" warnings
640656
// this is needed so we can pass in 'targetContext' for context.cacheDir
641657
private fun TagsDialog.withTestArguments(

0 commit comments

Comments
 (0)