add sanity checks when releasing the objects
This commit is contained in:
parent
26a7ef8ddc
commit
a14bf30c43
7 changed files with 42 additions and 31 deletions
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.matrix.olm;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
|
@ -77,8 +77,10 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
|
|||
* Release native account and invalid its JAVA reference counter part.<br>
|
||||
* Public API for {@link #releaseAccountJni()}.
|
||||
*/
|
||||
public void releaseAccount(){
|
||||
releaseAccountJni();
|
||||
public void releaseAccount() {
|
||||
if (0 != mNativeId) {
|
||||
releaseAccountJni();
|
||||
}
|
||||
mNativeId = 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -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}..**/
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -66,8 +66,10 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
|||
* Release native session and invalid its JAVA reference counter part.<br>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.<br>
|
||||
* 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 {
|
||||
|
|
Loading…
Reference in a new issue