From 9f990d29cbdcd5eb2c762d752c4d160f7e032674 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Mon, 26 Jul 2021 15:03:55 +0300 Subject: [PATCH 1/2] Safe access to bubbles array --- MatrixKit/Models/Room/MXKRoomDataSource.m | 63 ++++++++++++----------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.m b/MatrixKit/Models/Room/MXKRoomDataSource.m index 8a6e2fca..e7c48536 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.m +++ b/MatrixKit/Models/Room/MXKRoomDataSource.m @@ -1607,45 +1607,48 @@ - (void)paginateToFillRect:(CGRect)rect direction:(MXTimelineDirection)direction CGFloat minMessageHeight = CGFLOAT_MAX; CGFloat bubblesTotalHeight = 0; - // Check whether data has been aldready loaded - if (bubbles.count) + @synchronized(bubbles) { - NSUInteger eventsCount = 0; - for (NSInteger i = bubbles.count - 1; i >= 0; i--) + // Check whether data has been aldready loaded + if (bubbles.count) { - id bubbleData = bubbles[i]; - eventsCount += bubbleData.events.count; - - CGFloat bubbleHeight = [self cellHeightAtIndex:i withMaximumWidth:rect.size.width]; - // Sanity check - if (bubbleHeight) + NSUInteger eventsCount = 0; + for (NSInteger i = bubbles.count - 1; i >= 0; i--) { - bubblesTotalHeight += bubbleHeight; - - if (bubblesTotalHeight > rect.size.height) + id bubbleData = bubbles[i]; + eventsCount += bubbleData.events.count; + + CGFloat bubbleHeight = [self cellHeightAtIndex:i withMaximumWidth:rect.size.width]; + // Sanity check + if (bubbleHeight) { - // No need to compute more cells heights, there are enough to fill the rect - MXLogDebug(@"[MXKRoomDataSource] -> %tu already loaded bubbles (%tu events) are enough to fill the screen", bubbles.count - i, eventsCount); - break; + bubblesTotalHeight += bubbleHeight; + + if (bubblesTotalHeight > rect.size.height) + { + // No need to compute more cells heights, there are enough to fill the rect + MXLogDebug(@"[MXKRoomDataSource] -> %tu already loaded bubbles (%tu events) are enough to fill the screen", bubbles.count - i, eventsCount); + break; + } + + // Compute the minimal height an event takes + minMessageHeight = MIN(minMessageHeight, bubbleHeight / bubbleData.events.count); } - - // Compute the minimal height an event takes - minMessageHeight = MIN(minMessageHeight, bubbleHeight / bubbleData.events.count); } } - } - else if (minRequestMessagesCount && [self canPaginate:direction]) - { - MXLogDebug(@"[MXKRoomDataSource] paginateToFillRect: Prefill with data from the store"); - // Give a chance to load data from the store before doing homeserver requests - // Reuse minRequestMessagesCount because we need to provide a number. - [self paginate:minRequestMessagesCount direction:direction onlyFromStore:YES success:^(NSUInteger addedCellNumber) { + else if (minRequestMessagesCount && [self canPaginate:direction]) + { + MXLogDebug(@"[MXKRoomDataSource] paginateToFillRect: Prefill with data from the store"); + // Give a chance to load data from the store before doing homeserver requests + // Reuse minRequestMessagesCount because we need to provide a number. + [self paginate:minRequestMessagesCount direction:direction onlyFromStore:YES success:^(NSUInteger addedCellNumber) { - // Then retry - [self paginateToFillRect:rect direction:direction withMinRequestMessagesCount:minRequestMessagesCount success:success failure:failure]; + // Then retry + [self paginateToFillRect:rect direction:direction withMinRequestMessagesCount:minRequestMessagesCount success:success failure:failure]; - } failure:failure]; - return; + } failure:failure]; + return; + } } // Is there enough cells to cover all the requested height? From 712d5b532c2f399438f4cdffa9a0fb3484060c9b Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Mon, 26 Jul 2021 15:04:55 +0300 Subject: [PATCH 2/2] Update CHANGES.rst --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index d02e21c4..542b8bbd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,7 @@ Changes to be released in next version * MXKAccount: added generic `others` storage dictionary (vector-im/element-ios/issues/4578) 🐛 Bugfix - * + * MXKRoomDataSource: Safe access to `bubbles` array on pagination (vector-im/element-ios/issues/4605). ⚠️ API Changes *