Merge pull request #6462 from vector-im/feature/fga/fix_6461

Timeline: fix validation of timeline event changes
This commit is contained in:
Adam Brown 2022-07-07 11:25:33 +01:00 committed by GitHub
commit 67d5289f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 34 deletions

1
changelog.d/6461.bugfix Normal file
View File

@ -0,0 +1 @@
Fix crashes on Timeline [Thread] due to range validation

View File

@ -490,38 +490,11 @@ internal class TimelineChunk(
private fun handleDatabaseChangeSet(results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) {
val insertions = changeSet.insertionRanges
for (range in insertions) {
// Check if the insertion's displayIndices match our expectations - or skip this insertion.
// Inconsistencies (missing messages) can happen otherwise if we get insertions before having loaded all timeline events of the chunk.
if (builtEvents.isNotEmpty()) {
// Check consistency to item before insertions
if (range.startIndex > 0) {
val firstInsertion = results[range.startIndex]!!
val lastBeforeInsertion = builtEvents[range.startIndex - 1]
if (firstInsertion.displayIndex + 1 != lastBeforeInsertion.displayIndex) {
Timber.i(
"handleDatabaseChangeSet: skip insertion at ${range.startIndex}/${builtEvents.size}, " +
"displayIndex mismatch at ${range.startIndex}: ${firstInsertion.displayIndex} -> ${lastBeforeInsertion.displayIndex}"
)
continue
}
}
// Check consistency to item after insertions
if (range.startIndex < builtEvents.size) {
val lastInsertion = results[range.startIndex + range.length - 1]!!
val firstAfterInsertion = builtEvents[range.startIndex]
if (firstAfterInsertion.displayIndex + 1 != lastInsertion.displayIndex) {
Timber.i(
"handleDatabaseChangeSet: skip insertion at ${range.startIndex}/${builtEvents.size}, " +
"displayIndex mismatch at ${range.startIndex + range.length}: " +
"${firstAfterInsertion.displayIndex} -> ${lastInsertion.displayIndex}"
)
continue
}
}
}
if (!validateInsertion(range, results)) continue
val newItems = results
.subList(range.startIndex, range.startIndex + range.length)
.map { it.buildAndDecryptIfNeeded() }
builtEventsIndexes.entries.filter { it.value >= range.startIndex }.forEach { it.setValue(it.value + range.length) }
newItems.mapIndexed { index, timelineEvent ->
if (timelineEvent.root.type == EventType.STATE_ROOM_CREATE) {
@ -536,12 +509,9 @@ internal class TimelineChunk(
for (range in modifications) {
for (modificationIndex in (range.startIndex until range.startIndex + range.length)) {
val updatedEntity = results[modificationIndex] ?: continue
val displayIndex = builtEventsIndexes[updatedEntity.eventId]
if (displayIndex == null) {
continue
}
val builtEventIndex = builtEventsIndexes[updatedEntity.eventId] ?: continue
try {
builtEvents[displayIndex] = updatedEntity.buildAndDecryptIfNeeded()
builtEvents[builtEventIndex] = updatedEntity.buildAndDecryptIfNeeded()
} catch (failure: Throwable) {
Timber.v("Fail to update items at index: $modificationIndex")
}
@ -558,6 +528,21 @@ internal class TimelineChunk(
}
}
private fun validateInsertion(range: OrderedCollectionChangeSet.Range, results: RealmResults<TimelineEventEntity>): Boolean {
// Insertion can only happen from LastForward chunk after a sync.
if (isLastForward.get()) {
val firstBuiltEvent = builtEvents.firstOrNull()
if (firstBuiltEvent != null) {
val lastInsertion = results[range.startIndex + range.length - 1] ?: return false
if (firstBuiltEvent.displayIndex + 1 != lastInsertion.displayIndex) {
Timber.v("There is no continuation in the chunk, chunk is not fully loaded yet, skip insert.")
return false
}
}
}
return true
}
private fun getNextDisplayIndex(direction: Timeline.Direction): Int? {
if (timelineEventEntities.isEmpty()) {
return null