Skip to content

Commit

Permalink
Merge pull request #1712 from matrix-org/andy/crashes
Browse files Browse the repository at this point in the history
Fix some crashes
  • Loading branch information
Anderas authored Feb 10, 2023
2 parents f5a4ac9 + a0d5132 commit 7d59096
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion MatrixSDK/Background/Crypto/MXLegacyBackgroundCrypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MXLegacyBackgroundCrypto: MXBackgroundCrypto {
let decryptionResult = MXEventDecryptionResult()
decryptionResult.clearEvent = olmResult.payload
decryptionResult.senderCurve25519Key = olmResult.senderKey
decryptionResult.claimedEd25519Key = olmResult.keysClaimed["ed25519"] as? String
decryptionResult.claimedEd25519Key = olmResult.keysClaimed?["ed25519"] as? String
decryptionResult.forwardingCurve25519KeyChain = olmResult.forwardingCurve25519KeyChain
decryptionResult.isUntrusted = olmResult.isUntrusted
event.setClearData(decryptionResult)
Expand Down
25 changes: 16 additions & 9 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct MXCryptoRequests {
}

func sendToDevice(request: ToDeviceRequest) async throws {
return try await performCallbackRequest {
try await performCallbackRequest {
restClient.sendDirectToDevice(
payload: .init(
eventType: request.eventType,
Expand All @@ -54,7 +54,7 @@ struct MXCryptoRequests {
}

func uploadKeys(request: UploadKeysRequest) async throws -> MXKeysUploadResponse {
return try await performCallbackRequest {
try await performCallbackRequest {
restClient.uploadKeys(
request.deviceKeys,
oneTimeKeys: request.oneTimeKeys,
Expand Down Expand Up @@ -101,25 +101,32 @@ struct MXCryptoRequests {
}

func claimKeys(request: ClaimKeysRequest) async throws -> MXKeysClaimResponse {
return try await performCallbackRequest {
try await performCallbackRequest {
restClient.claimOneTimeKeys(for: request.devices, completion: $0)
}
}

@MainActor
// Calling methods on `MXRoom` has various state side effects so should be called on the main thread
func roomMessage(request: RoomMessageRequest) async throws -> String? {
var event: MXEvent?
return try await performCallbackRequest {
try await withCheckedThrowingContinuation { continuation in
var event: MXEvent?
request.room.sendEvent(
MXEventType(identifier: request.eventType),
content: request.content,
localEcho: &event,
completion: $0
)
localEcho: &event) { response in
switch response {
case .success(let value):
continuation.resume(returning: value)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}

func backupKeys(request: KeysBackupRequest) async throws -> [AnyHashable: Any] {
return try await performCallbackRequest { continuation in
try await performCallbackRequest { continuation in
restClient.sendKeysBackup(
request.keysBackupData,
version: request.version,
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDK/Crypto/MXCryptoV2Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import Foundation

private func migrateIfNecessary(legacyStore: MXCryptoStore, updateProgress: @escaping (Double) -> Void) {
guard legacyStore.cryptoVersion.rawValue < MXCryptoVersion.versionLegacyDeprecated.rawValue else {
log.debug("Legacy crypto has already been deprecatd, no need to migrate")
log.debug("Legacy crypto has already been deprecated, no need to migrate")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ import Foundation
/// Class that computes verification state for a request_id by comparing all related events
/// and taking whichever final event (e.g. cancelled, done), when the request is no longer
/// active.
@objcMembers
public class MXKeyVerificationStateResolver: NSObject {
actor MXKeyVerificationStateResolver {
private let myUserId: String
private let aggregations: MXAggregations
private var states: [String: MXKeyVerificationState]
private let log = MXNamedLog(name: "MXKeyVerificationStateResolver")

public init(myUserId: String, aggregations: MXAggregations) {
init(myUserId: String, aggregations: MXAggregations) {
self.myUserId = myUserId
self.aggregations = aggregations
self.states = [:]
}

public func verificationState(flowId: String, roomId: String) async throws -> MXKeyVerificationState {
func verificationState(flowId: String, roomId: String) async throws -> MXKeyVerificationState {
log.debug("->")

if let state = states[flowId] {
Expand Down Expand Up @@ -67,6 +66,7 @@ public class MXKeyVerificationStateResolver: NSObject {
}
}

nonisolated
private func resolvedState(for events: [MXEvent]) -> MXKeyVerificationState {
var defaultState = MXKeyVerificationState.transactionStarted
for event in events {
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ - (void)setMatrixSession:(MXSession *)mxSession

- (void)save:(BOOL)commit
{
if (!NSThread.isMainThread)
{
// Saving on the main thread is not ideal, but is currently the only safe way, given the mutation
// of internal state and posting notifications observed by UI without double-checking which thread
// the notification arrives on.
MXLogFailure(@"[MXRoomSummary] save: Saving room summary should happen from the main thread")
dispatch_async(dispatch_get_main_queue(), ^{
[self save:commit];
});
return;
}

_dataTypes = self.calculateDataTypes;
_sentStatus = self.calculateSentStatus;
_favoriteTagOrder = self.room.accountData.tags[kMXRoomTagFavourite].order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ internal class MXBreadcrumbsRoomListDataFetcher: NSObject, MXRoomListDataFetcher

if let query = fetchOptions.filterOptions.query?.lowercased(), !query.isEmpty {
recentRoomIds = recentRoomIds.filter({ roomId in
guard let summary = session?.roomSummary(withRoomId: roomId) else {
guard let displayname = session?.roomSummary(withRoomId: roomId).displayname else {
return false
}
return summary.displayname.lowercased().contains(query)
return displayname.lowercased().contains(query)
})
}

Expand Down
15 changes: 14 additions & 1 deletion MatrixSDK/Utils/Logs/MXAnalyticsDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,25 @@ class MXAnalyticsDestination: BaseDestination {

private func shouldTrackIssue(with context: Any?) -> Bool {
// We will track all issues except for those with a cancellation error
guard let error = context as? NSError else {
guard let error = errorFromContext(context: context) else {
return true
}
return !error.isCancelledError
}

private func errorFromContext(context: Any?) -> NSError? {
if let error = context as? NSError {
return error
} else if let dictionary = context as? [AnyHashable: Any] {
for (_, value) in dictionary {
if let error = value as? NSError {
return error
}
}
}
return nil
}

private func formattedDetails(from context: Any?) -> [String: Any]? {
guard let context = context else {
return nil
Expand Down

0 comments on commit 7d59096

Please sign in to comment.