add sanity checks when releasing the objects

This commit is contained in:
ylecollen 2017-01-09 15:29:23 +01:00
parent 26a7ef8ddc
commit a14bf30c43
7 changed files with 42 additions and 31 deletions

View file

@ -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;

View file

@ -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() {
releaseAccountJni(); if (0 != mNativeId) {
releaseAccountJni();
}
mNativeId = 0; mNativeId = 0;
} }

View file

@ -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}..**/

View file

@ -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(){
releaseSessionJni(); if (0 != mNativeId) {
releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }

View file

@ -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() {
releaseSessionJni(); if (0 != mNativeId) {
releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }
@ -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)) {

View file

@ -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() {
releaseSessionJni(); if (0 != mNativeId) {
releaseSessionJni();
}
mNativeId = 0; mNativeId = 0;
} }
@ -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 {
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);
} }
} }

View file

@ -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 {
mNativeId = createUtilityJni(); try {
return (0 != mNativeId); mNativeId = createUtilityJni();
} 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 {
* Release native instance.<br> * Release native instance.<br>
* Public API for {@link #releaseUtilityJni()}. * Public API for {@link #releaseUtilityJni()}.
*/ */
public void releaseUtility(){ public void releaseUtility() {
releaseUtilityJni(); if (0 != mNativeId) {
releaseUtilityJni();
}
mNativeId = 0; mNativeId = 0;
} }
private native void releaseUtilityJni(); private native void releaseUtilityJni();
@ -111,7 +116,7 @@ public class OlmUtility {
* @return hash value if operation succeed, null otherwise * @return hash value if operation succeed, null otherwise
*/ */
public String sha256(String aMessageToHash) { public String sha256(String aMessageToHash) {
String hashRetValue = null; String hashRetValue = null;
if (null != aMessageToHash) { if (null != aMessageToHash) {
try { try {