From a14bf30c43d87e6316fd984a6be76de0c9a9ba7a Mon Sep 17 00:00:00 2001 From: ylecollen Date: Mon, 9 Jan 2017 15:29:23 +0100 Subject: add sanity checks when releasing the objects --- .../java/org/matrix/olm/CommonSerializeUtils.java | 1 - .../src/main/java/org/matrix/olm/OlmAccount.java | 6 +++-- .../src/main/java/org/matrix/olm/OlmException.java | 8 ++----- .../org/matrix/olm/OlmInboundGroupSession.java | 4 +++- .../org/matrix/olm/OlmOutboundGroupSession.java | 6 +++-- .../src/main/java/org/matrix/olm/OlmSession.java | 27 +++++++++++++--------- .../src/main/java/org/matrix/olm/OlmUtility.java | 21 ++++++++++------- 7 files changed, 42 insertions(+), 31 deletions(-) diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java index 30f8352..93a85fd 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java @@ -17,7 +17,6 @@ package org.matrix.olm; -import android.text.TextUtils; import android.util.Log; import java.io.IOException; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 3e7d01d..6c1caf0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -77,8 +77,10 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * Release native account and invalid its JAVA reference counter part.
* Public API for {@link #releaseAccountJni()}. */ - public void releaseAccount(){ - releaseAccountJni(); + public void releaseAccount() { + if (0 != mNativeId) { + releaseAccountJni(); + } mNativeId = 0; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java index 155724b..93a2425 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -55,15 +55,11 @@ public class OlmException extends IOException { public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405; public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406; - public static final int EXCEPTION_CODE_UTILITY_CREATION = 501; - public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500; + public static final int EXCEPTION_CODE_UTILITY_CREATION = 500; + public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 501; // exception human readable messages - public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed"; - public static final String EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION = "createNewSession() failed"; public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid de-serialized parameters"; - public static final String EXCEPTION_MSG_INIT_ACCOUNT_CREATION = "initNewAccount() failed"; - public static final String EXCEPTION_MSG_INIT_SESSION_CREATION = "initNewSession() failed"; /** exception code to be taken from: {@link #EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION}, {@link #EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION}, * {@link #EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION}, {@link #EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION}..**/ diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java index d9075c8..09e5276 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java @@ -70,7 +70,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * Public API for {@link #releaseSessionJni()}. */ public void releaseSession(){ - releaseSessionJni(); + if (0 != mNativeId) { + releaseSessionJni(); + } mNativeId = 0; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java index a7972d3..d26b9b6 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java @@ -59,7 +59,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * Public API for {@link #releaseSessionJni()}. */ public void releaseSession() { - releaseSessionJni(); + if (0 != mNativeId) { + releaseSessionJni(); + } mNativeId = 0; } @@ -252,7 +254,7 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { createNewSession(); - String errorMsg = null; + String errorMsg; try { if ((null == aSerializedData) || (null == aKey)) { diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java index 170b1a5..92cc110 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java @@ -66,8 +66,10 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * Release native session and invalid its JAVA reference counter part.
* Public API for {@link #releaseSessionJni()}. */ - public void releaseSession(){ - releaseSessionJni(); + public void releaseSession() { + if (0 != mNativeId) { + releaseSessionJni(); + } mNativeId = 0; } @@ -262,7 +264,11 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { OlmMessage encryptedMsgRetValue = new OlmMessage(); try { - encryptedMsgRetValue.mCipherText = new String(encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue), "UTF-8"); + byte[] encryptedMessageBuffer = encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue); + + if (null != encryptedMessageBuffer) { + encryptedMsgRetValue.mCipherText = new String(encryptedMessageBuffer, "UTF-8"); + } } catch (Exception e) { Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage()); @@ -360,24 +366,23 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { createNewSession(); - StringBuffer errorMsg = new StringBuffer(); + String errorMsg; try { - String jniError; if ((null == aSerializedData) || (null == aKey)) { Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg.append("invalid input parameters"); - } else if (null != (jniError = deserializeJni(aSerializedData, aKey))) { - errorMsg.append(jniError); + errorMsg = "invalid input parameters"; + } else { + errorMsg = deserializeJni(aSerializedData, aKey); } } catch (Exception e) { Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg.append(e.getMessage()); + errorMsg = e.getMessage(); } - if (errorMsg.length() > 0) { + if (!TextUtils.isEmpty(errorMsg)) { releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg)); + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java index d520da6..32a5830 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java @@ -35,18 +35,21 @@ public class OlmUtility { **/ private long mNativeId; - public OlmUtility() { + public OlmUtility() throws OlmException { initUtility(); } /** * Create a native utility instance. * To be called before any other API call. - * @return true if init succeed, false otherwise. + * @exception OlmException the exception */ - private boolean initUtility() { - mNativeId = createUtilityJni(); - return (0 != mNativeId); + private void initUtility() throws OlmException { + try { + mNativeId = createUtilityJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_UTILITY_CREATION, e.getMessage()); + } } private native long createUtilityJni(); @@ -55,8 +58,10 @@ public class OlmUtility { * Release native instance.
* Public API for {@link #releaseUtilityJni()}. */ - public void releaseUtility(){ - releaseUtilityJni(); + public void releaseUtility() { + if (0 != mNativeId) { + releaseUtilityJni(); + } mNativeId = 0; } private native void releaseUtilityJni(); @@ -111,7 +116,7 @@ public class OlmUtility { * @return hash value if operation succeed, null otherwise */ public String sha256(String aMessageToHash) { - String hashRetValue = null; + String hashRetValue = null; if (null != aMessageToHash) { try { -- cgit v1.2.3