Commit a14bf30c authored by ylecollen's avatar ylecollen
Browse files

add sanity checks when releasing the objects

parent 26a7ef8d
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.matrix.olm; package org.matrix.olm;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.io.IOException; import java.io.IOException;
......
...@@ -77,8 +77,10 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { ...@@ -77,8 +77,10 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* Release native account and invalid its JAVA reference counter part.<br> * Release native account and invalid its JAVA reference counter part.<br>
* Public API for {@link #releaseAccountJni()}. * Public API for {@link #releaseAccountJni()}.
*/ */
public void releaseAccount(){ public void releaseAccount() {
if (0 != mNativeId) {
releaseAccountJni(); releaseAccountJni();
}
mNativeId = 0; mNativeId = 0;
} }
......
...@@ -55,15 +55,11 @@ public class OlmException extends IOException { ...@@ -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_DECRYPT_MESSAGE = 405;
public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406; 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_CREATION = 500;
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500; public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 501;
// exception human readable messages // 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_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}, /** 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}..**/ * {@link #EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION}, {@link #EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION}..**/
......
...@@ -70,7 +70,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri ...@@ -70,7 +70,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.
*/ */
public void releaseSession(){ public void releaseSession(){
if (0 != mNativeId) {
releaseSessionJni(); releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }
......
...@@ -59,7 +59,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser ...@@ -59,7 +59,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.
*/ */
public void releaseSession() { public void releaseSession() {
if (0 != mNativeId) {
releaseSessionJni(); releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }
...@@ -252,7 +254,7 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser ...@@ -252,7 +254,7 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewSession(); createNewSession();
String errorMsg = null; String errorMsg;
try { try {
if ((null == aSerializedData) || (null == aKey)) { if ((null == aSerializedData) || (null == aKey)) {
......
...@@ -66,8 +66,10 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { ...@@ -66,8 +66,10 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
* Release native session and invalid its JAVA reference counter part.<br> * Release native session and invalid its JAVA reference counter part.<br>
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.
*/ */
public void releaseSession(){ public void releaseSession() {
if (0 != mNativeId) {
releaseSessionJni(); releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }
...@@ -262,7 +264,11 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { ...@@ -262,7 +264,11 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
OlmMessage encryptedMsgRetValue = new OlmMessage(); OlmMessage encryptedMsgRetValue = new OlmMessage();
try { 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) { } catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage()); Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage());
...@@ -360,24 +366,23 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { ...@@ -360,24 +366,23 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
createNewSession(); createNewSession();
StringBuffer errorMsg = new StringBuffer(); String errorMsg;
try { try {
String jniError;
if ((null == aSerializedData) || (null == aKey)) { if ((null == aSerializedData) || (null == aKey)) {
Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
errorMsg.append("invalid input parameters"); errorMsg = "invalid input parameters";
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) { } else {
errorMsg.append(jniError); errorMsg = deserializeJni(aSerializedData, aKey);
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
errorMsg.append(e.getMessage()); errorMsg = e.getMessage();
} }
if (errorMsg.length() > 0) { if (!TextUtils.isEmpty(errorMsg)) {
releaseSession(); releaseSession();
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg)); throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
} }
} }
......
...@@ -35,18 +35,21 @@ public class OlmUtility { ...@@ -35,18 +35,21 @@ public class OlmUtility {
**/ **/
private long mNativeId; private long mNativeId;
public OlmUtility() { public OlmUtility() throws OlmException {
initUtility(); initUtility();
} }
/** /**
* Create a native utility instance. * Create a native utility instance.
* To be called before any other API call. * To be called before any other API call.
* @return true if init succeed, false otherwise. * @exception OlmException the exception
*/ */
private boolean initUtility() { private void initUtility() throws OlmException {
try {
mNativeId = createUtilityJni(); mNativeId = createUtilityJni();
return (0 != mNativeId); } catch (Exception e) {
throw new OlmException(OlmException.EXCEPTION_CODE_UTILITY_CREATION, e.getMessage());
}
} }
private native long createUtilityJni(); private native long createUtilityJni();
...@@ -55,8 +58,10 @@ public class OlmUtility { ...@@ -55,8 +58,10 @@ public class OlmUtility {
* Release native instance.<br> * Release native instance.<br>
* Public API for {@link #releaseUtilityJni()}. * Public API for {@link #releaseUtilityJni()}.
*/ */
public void releaseUtility(){ public void releaseUtility() {
if (0 != mNativeId) {
releaseUtilityJni(); releaseUtilityJni();
}
mNativeId = 0; mNativeId = 0;
} }
private native void releaseUtilityJni(); private native void releaseUtilityJni();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment