Avoid usage of !!

This commit is contained in:
Benoit Marty 2020-10-30 14:29:44 +01:00
parent 1fcbf7ed42
commit 14e7e5e9fd

View file

@ -48,15 +48,12 @@ class OlmInboundGroupSessionWrapper2 : Serializable {
*/ */
val firstKnownIndex: Long? val firstKnownIndex: Long?
get() { get() {
if (null != olmInboundGroupSession) { return try {
try { return olmInboundGroupSession?.firstKnownIndex
return olmInboundGroupSession!!.firstKnownIndex } catch (e: Exception) {
} catch (e: Exception) { Timber.e(e, "## getFirstKnownIndex() : getFirstKnownIndex failed")
Timber.e(e, "## getFirstKnownIndex() : getFirstKnownIndex failed") null
}
} }
return null
} }
/** /**
@ -90,11 +87,13 @@ class OlmInboundGroupSessionWrapper2 : Serializable {
@Throws(Exception::class) @Throws(Exception::class)
constructor(megolmSessionData: MegolmSessionData) { constructor(megolmSessionData: MegolmSessionData) {
try { try {
olmInboundGroupSession = OlmInboundGroupSession.importSession(megolmSessionData.sessionKey!!) val safeSessionKey = megolmSessionData.sessionKey ?: throw Exception("invalid data")
olmInboundGroupSession = OlmInboundGroupSession.importSession(safeSessionKey)
if (olmInboundGroupSession!!.sessionIdentifier() != megolmSessionData.sessionId) { .also {
throw Exception("Mismatched group session Id") if (it.sessionIdentifier() != megolmSessionData.sessionId) {
} throw Exception("Mismatched group session Id")
}
}
senderKey = megolmSessionData.senderKey senderKey = megolmSessionData.senderKey
keysClaimed = megolmSessionData.senderClaimedKeys keysClaimed = megolmSessionData.senderClaimedKeys
@ -120,16 +119,18 @@ class OlmInboundGroupSessionWrapper2 : Serializable {
return null return null
} }
val wantedIndex = index ?: olmInboundGroupSession!!.firstKnownIndex val safeOlmInboundGroupSession = olmInboundGroupSession ?: return null
val wantedIndex = index ?: safeOlmInboundGroupSession.firstKnownIndex
MegolmSessionData( MegolmSessionData(
senderClaimedEd25519Key = keysClaimed?.get("ed25519"), senderClaimedEd25519Key = keysClaimed?.get("ed25519"),
forwardingCurve25519KeyChain = ArrayList(forwardingCurve25519KeyChain!!), forwardingCurve25519KeyChain = forwardingCurve25519KeyChain?.toList().orEmpty(),
senderKey = senderKey, senderKey = senderKey,
senderClaimedKeys = keysClaimed, senderClaimedKeys = keysClaimed,
roomId = roomId, roomId = roomId,
sessionId = olmInboundGroupSession!!.sessionIdentifier(), sessionId = safeOlmInboundGroupSession.sessionIdentifier(),
sessionKey = olmInboundGroupSession!!.export(wantedIndex), sessionKey = safeOlmInboundGroupSession.export(wantedIndex),
algorithm = MXCRYPTO_ALGORITHM_MEGOLM algorithm = MXCRYPTO_ALGORITHM_MEGOLM
) )
} catch (e: Exception) { } catch (e: Exception) {
@ -145,14 +146,11 @@ class OlmInboundGroupSessionWrapper2 : Serializable {
* @return the exported data * @return the exported data
*/ */
fun exportSession(messageIndex: Long): String? { fun exportSession(messageIndex: Long): String? {
if (null != olmInboundGroupSession) { return try {
try { return olmInboundGroupSession?.export(messageIndex)
return olmInboundGroupSession!!.export(messageIndex) } catch (e: Exception) {
} catch (e: Exception) { Timber.e(e, "## exportSession() : export failed")
Timber.e(e, "## exportSession() : export failed") null
}
} }
return null
} }
} }