Skip to content

Commit

Permalink
fix(tags-dialog): crash on unicode tags
Browse files Browse the repository at this point in the history
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
  • Loading branch information
david-allison authored and BrayanDSO committed May 27, 2024
1 parent 5ebb74c commit fabbd7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.ichi2.anki.R
import com.ichi2.anki.analytics.AnalyticsDialogFragment
import com.ichi2.anki.model.CardStateFilter
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.utils.ext.convertToString
import com.ichi2.annotations.NeedsTest
import com.ichi2.utils.DisplayUtils.resizeWhenSoftInputShown
import com.ichi2.utils.TagsUtil
Expand Down Expand Up @@ -415,13 +414,13 @@ class TagsFile(path: String) : File(path), Parcelable {
// https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/formats.md
val string = Json.encodeToString(data)
Timber.d("persisting tags to disk, length: %d", string.length)
outputStream.writeBytes(string)
outputStream.writeUTF(string)
}
}

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

override fun describeContents(): Int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.kotlin.whenever
import timber.log.Timber
import java.util.concurrent.atomic.AtomicReference

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -636,6 +637,21 @@ class TagsDialogTest : RobolectricTest() {
}
}

@Test
fun `unicode tags can be serialized 16576`() {
val type = TagsDialog.DialogType.FILTER_BY_TAG
val allTags = listOf("02动作状态")

val args = TagsDialog(ParametersUtils.whatever())
.withTestArguments(type, emptyList(), allTags)
.arguments
val mockListener = Mockito.mock(TagsDialogListener::class.java)
val factory = TagsDialogFactory(mockListener)
val scenario = FragmentScenario.launch(TagsDialog::class.java, args, R.style.Theme_Light, factory)
scenario.moveToState(Lifecycle.State.STARTED)
scenario.onFragment { Timber.d("Dialog successfully opened") }
}

// these are called 'withTestArguments' due to "extension is shadowed by a member" warnings
// this is needed so we can pass in 'targetContext' for context.cacheDir
private fun TagsDialog.withTestArguments(
Expand Down

0 comments on commit fabbd7a

Please sign in to comment.