From 65352d05aa1c9cc607a1bc2c560ac3c6e6ebf22e Mon Sep 17 00:00:00 2001 From: ylecollen Date: Tue, 3 Jan 2017 09:32:59 +0100 Subject: ->Replace 2 spaces tabs by 4 spaces. ->fix the NULL and 0 pointer comparisons mixes --- .../src/main/java/org/matrix/olm/OlmSession.java | 2 +- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 31 +- .../src/main/jni/olm_inbound_group_session.cpp | 62 ++-- .../olm-sdk/src/main/jni/olm_jni_helper.cpp | 7 +- .../OlmLibSdk/olm-sdk/src/main/jni/olm_manager.cpp | 16 +- .../src/main/jni/olm_outbound_group_session.cpp | 171 +++++----- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp | 375 +++++++++++---------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp | 80 +++-- 8 files changed, 372 insertions(+), 372 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk') 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 139cbb6..23b2091 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 @@ -335,7 +335,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { public OlmMessage encryptMessage(String aClearMsg) { OlmMessage encryptedMsgRetValue = new OlmMessage(); - if(0 != encryptMessageJni(aClearMsg, encryptedMsgRetValue)){ + if (0 != encryptMessageJni(aClearMsg, encryptedMsgRetValue)){ encryptedMsgRetValue = null; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index 47c616d..a44d9db 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -19,7 +19,6 @@ using namespace AndroidOlmSdk; - /** * Init memory allocation for account creation. * @return valid memory allocation, NULL otherwise @@ -43,7 +42,6 @@ OlmAccount* initializeAccountMemory() return accountPtr; } - JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz) { LOGD("## createNewAccountJni(): IN"); @@ -53,7 +51,6 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject t return (jlong)(intptr_t)accountPtr; } - /** * Release the account allocation made by initializeAccountMemory().
* This method MUST be called when java counter part account instance is done. @@ -65,7 +62,7 @@ JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); - if(!accountPtr) + if (!accountPtr) { LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); } @@ -147,7 +144,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject jbyteArray byteArrayRetValue = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); - if (NULL == accountPtr) + if (!accountPtr) { LOGE("## identityKeys(): failure - invalid Account ptr=NULL"); } @@ -168,7 +165,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject // retrieve key pairs in identityKeysBytesPtr size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); - if(keysResult == olm_error()) + if (keysResult == olm_error()) { LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); } @@ -177,7 +174,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject // allocate the byte array to be returned to java byteArrayRetValue = env->NewByteArray(identityKeysLength); - if(NULL == byteArrayRetValue) + if (!byteArrayRetValue) { LOGE("## identityKeys(): failure - return byte array OOM"); } @@ -243,7 +240,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject uint8_t *randomBufferPtr = NULL; - if ( (0!=randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) + if ((0 != randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) { LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); } @@ -304,7 +301,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t { // retrieve key pairs in keysBytesPtr size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); - if(keysResult == olm_error()) { + if (keysResult == olm_error()) { LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); } else @@ -388,7 +385,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, { size_t result = olm_account_mark_keys_as_published(accountPtr); - if(result == olm_error()) + if (result == olm_error()) { LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); retCode = ERROR_CODE_KO; @@ -417,7 +414,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz { LOGE("## signMessageJni(): failure - invalid aMessage param"); } - else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) + else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { LOGE("## signMessageJni(): failure - invalid account ptr"); } @@ -490,23 +487,23 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job { LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); } - else if(!aErrorMsg) + else if (!aErrorMsg) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); } - else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) + else if (!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid account ptr"); } - else if(!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) + else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); } - else if(!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) + else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); } - else if(!(keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); } @@ -604,7 +601,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_account_last_error(accountPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp index 4ccb571..535fd48 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp @@ -26,28 +26,28 @@ using namespace AndroidOlmSdk; */ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) { - OlmInboundGroupSession* sessionPtr = NULL; + OlmInboundGroupSession* sessionPtr = NULL; - LOGD("## releaseSessionJni(): InBound group session IN"); + LOGD("## releaseSessionJni(): InBound group session IN"); - if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) - { - LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); - } - else - { - LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); + if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) + { + LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); + } + else + { + LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); #ifdef ENABLE_JNI_LOG - size_t retCode = olm_clear_inbound_group_session(sessionPtr); - LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",static_cast(retCode)); + size_t retCode = olm_clear_inbound_group_session(sessionPtr); + LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",static_cast(retCode)); #else - olm_clear_inbound_group_session(sessionPtr); + olm_clear_inbound_group_session(sessionPtr); #endif - LOGD(" ## releaseSessionJni(): free IN"); - free(sessionPtr); - LOGD(" ## releaseSessionJni(): free OUT"); - } + LOGD(" ## releaseSessionJni(): free IN"); + free(sessionPtr); + LOGD(" ## releaseSessionJni(): free OUT"); + } } /** @@ -64,18 +64,18 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv * LOGD("## createNewSessionJni(): inbound group session IN"); sessionSize = olm_inbound_group_session_size(); - if(0 == sessionSize) + if (!sessionSize) { LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0"); } - else if(NULL != (sessionPtr=(OlmInboundGroupSession*)malloc(sessionSize))) + else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize))) { - sessionPtr = olm_inbound_group_session(sessionPtr); - LOGD(" ## createNewSessionJni(): success - inbound group session size=%lu",static_cast(sessionSize)); + sessionPtr = olm_inbound_group_session(sessionPtr); + LOGD(" ## createNewSessionJni(): success - inbound group session size=%lu",static_cast(sessionSize)); } else { - LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); + LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); } return (jlong)(intptr_t)sessionPtr; @@ -95,15 +95,15 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes LOGD("## initInboundGroupSessionWithSessionKeyJni(): inbound group session IN"); - if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"); } - else if(0 == aSessionKey) + else if (!aSessionKey) { LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); } - else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0))) + else if (!(sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0))) { LOGE(" ## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"); } @@ -113,7 +113,7 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes LOGD(" ## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",static_cast(sessionKeyLength)); sessionResult = olm_init_inbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength); - if(sessionResult == olm_error()) { + if (sessionResult == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); LOGE(" ## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); } @@ -125,7 +125,7 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes } // free local alloc - if(NULL!= sessionKeyPtr) + if (sessionKeyPtr) { env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr); } @@ -140,12 +140,11 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) { OlmInboundGroupSession *sessionPtr = NULL; - uint8_t *sessionIdPtr = NULL; jstring returnValueStr=0; LOGD("## sessionIdentifierJni(): inbound group session IN"); - if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance"); } @@ -155,13 +154,16 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast(lengthSessionId)); - if(NULL == (sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) + uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); + + if (!sessionIdPtr) { LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); } else { size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); + if (result == olm_error()) { LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); @@ -402,7 +404,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JN keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index eaa7edf..767fad4 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -36,7 +36,7 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize) { LOGE("## setRandomInBuffer(): failure - aBuffer=NULL"); } - else if(!aRandomSize) + else if (!aRandomSize) { LOGE("## setRandomInBuffer(): failure - random size=0"); } @@ -113,7 +113,8 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize) jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass) { jlong instanceId = 0; - if (aJniEnv) + + if (aJniEnv) { jclass requiredClass = aJniEnv->FindClass(aCallingClass); jclass loaderClass = 0; @@ -227,7 +228,7 @@ jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLengt { LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)"); } - else if (!(tempByteArray=env->NewByteArray(aMsgLength))) + else if (!(tempByteArray = env->NewByteArray(aMsgLength))) { LOGE("## javaCStringToUtf8(): failure - return byte array OOM"); } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_manager.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_manager.cpp index e72824f..8ee0df7 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_manager.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_manager.cpp @@ -21,15 +21,15 @@ using namespace AndroidOlmSdk; JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject thiz) { - uint8_t majorVer=0, minorVer=0, patchVer=0; - jstring returnValueStr=0; - char buff[150]; + uint8_t majorVer=0, minorVer=0, patchVer=0; + jstring returnValueStr=0; + char buff[150]; - olm_get_library_version(&majorVer, &minorVer, &patchVer); - LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer); + olm_get_library_version(&majorVer, &minorVer, &patchVer); + LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer); - snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer); - returnValueStr = env->NewStringUTF((const char*)buff); + snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer); + returnValueStr = env->NewStringUTF((const char*)buff); - return returnValueStr; + return returnValueStr; } \ No newline at end of file diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp index 91504de..2dad00a 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp @@ -26,29 +26,29 @@ using namespace AndroidOlmSdk; */ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) { - OlmOutboundGroupSession* sessionPtr = NULL; + LOGD("## releaseSessionJni(): OutBound group session IN"); - LOGD("## releaseSessionJni(): OutBound group session IN"); + OlmOutboundGroupSession* sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) - { - LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance"); - } - else - { - LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); + if (!sessionPtr) + { + LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance"); + } + else + { + LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); #ifdef ENABLE_JNI_LOG - size_t retCode = olm_clear_outbound_group_session(sessionPtr); - LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast(retCode)); + size_t retCode = olm_clear_outbound_group_session(sessionPtr); + LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast(retCode)); #else - olm_clear_outbound_group_session(sessionPtr); + olm_clear_outbound_group_session(sessionPtr); #endif - LOGD(" ## releaseSessionJni(): free IN"); - free(sessionPtr); - LOGD(" ## releaseSessionJni(): free OUT"); - } + LOGD(" ## releaseSessionJni(): free IN"); + free(sessionPtr); + LOGD(" ## releaseSessionJni(): free OUT"); + } } /** @@ -65,18 +65,18 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv LOGD("## createNewSessionJni(): outbound group session IN"); sessionSize = olm_outbound_group_session_size(); - if(0 == sessionSize) + if (0 == sessionSize) { LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0"); } - else if(NULL != (sessionPtr=(OlmOutboundGroupSession*)malloc(sessionSize))) + else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize))) { - sessionPtr = olm_outbound_group_session(sessionPtr); - LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast(sessionSize)); + sessionPtr = olm_outbound_group_session(sessionPtr); + LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast(sessionSize)); } else { - LOGE(" ## createNewSessionJni(): failure - outbound group session OOM"); + LOGE(" ## createNewSessionJni(): failure - outbound group session OOM"); } return (jlong)(intptr_t)sessionPtr; @@ -89,12 +89,12 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz) { jint retCode = ERROR_CODE_KO; - OlmOutboundGroupSession *sessionPtr = NULL; - uint8_t *randomBuffPtr = NULL; LOGD("## initOutboundGroupSessionJni(): IN"); - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); + + if (!sessionPtr) { LOGE(" ## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"); } @@ -102,20 +102,24 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)( { // compute random buffer size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); + uint8_t *randomBuffPtr = NULL; + LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast(randomLength)); - if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) + + if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) { LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init"); } else { - if(0==randomLength) + if (0 == randomLength) { LOGW(" ## initOutboundGroupSessionJni(): random buffer is not required"); } size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); - if(sessionResult == olm_error()) { + + if (sessionResult == olm_error()) { LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } else @@ -123,12 +127,9 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)( retCode = ERROR_CODE_OK; LOGD(" ## initOutboundGroupSessionJni(): success - result=%lu", static_cast(sessionResult)); } - } - } - if(NULL != randomBuffPtr) - { - free(randomBuffPtr); + free(randomBuffPtr); + } } return retCode; @@ -139,13 +140,11 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)( */ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) { - OlmOutboundGroupSession *sessionPtr = NULL; - uint8_t *sessionIdPtr = NULL; - jstring returnValueStr=0; - LOGD("## sessionIdentifierJni(): outbound group session IN"); + OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); + jstring returnValueStr=0; - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!sessionPtr) { LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance"); } @@ -155,13 +154,16 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast(lengthSessionId)); - if(NULL == (sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) + uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); + + if (!sessionIdPtr) { LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); } else { size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); + if (result == olm_error()) { LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",reinterpret_cast(olm_outbound_group_session_last_error(sessionPtr))); @@ -196,7 +198,7 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, LOGD("## messageIndexJni(): IN"); - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance"); } @@ -204,24 +206,22 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, { indexRetValue = static_cast(olm_outbound_group_session_message_index(sessionPtr)); } + LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue); return indexRetValue; } - /** * Get the base64-encoded current ratchet key for this session.
*/ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) { - OlmOutboundGroupSession *sessionPtr = NULL; - uint8_t *sessionKeyPtr = NULL; - jstring returnValueStr=0; - LOGD("## sessionKeyJni(): outbound group session IN"); + OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); + jstring returnValueStr = 0; - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!sessionPtr) { LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance"); } @@ -231,13 +231,16 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast(sessionKeyLength)); - if(NULL == (sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t)))) + uint8_t *sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t)); + + if (!sessionKeyPtr) { LOGE(" ## sessionKeyJni(): failure - session key allocation OOM"); } else { size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength); + if (result == olm_error()) { LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); @@ -261,22 +264,21 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg) { + LOGD("## encryptMessageJni(): IN"); + jstring encryptedMsgRetValue = 0; OlmOutboundGroupSession *sessionPtr = NULL; const char *clearMsgPtr = NULL; - uint8_t *encryptedMsgPtr = NULL; - LOGD("## encryptMessageJni(): IN"); - - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL"); } - else if(0 == aClearMsg) + else if (!aClearMsg) { LOGE(" ## encryptMessageJni(): failure - invalid clear message"); } - else if(0 == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) + else if (!(clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) { LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM"); } @@ -288,7 +290,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv // compute max encrypted length size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); - if(NULL == (encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)))) + uint8_t *encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)); + + if (!encryptedMsgPtr) { LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); } @@ -301,7 +305,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv clearMsgLength, encryptedMsgPtr, encryptedMsgLength); - if(encryptedLength == olm_error()) + if (encryptedLength == olm_error()) { LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); } @@ -313,20 +317,17 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast(encryptedLength), reinterpret_cast(encryptedMsgPtr)); encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr); } - } + + free(encryptedMsgPtr); + } } // free alloc - if(NULL != clearMsgPtr) + if (clearMsgPtr) { env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); } - if(NULL != encryptedMsgPtr) - { - free(encryptedMsgPtr); - } - return encryptedMsgRetValue; } @@ -344,32 +345,31 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J jmethodID errorMsgMethodId = 0; jstring errorJstring = 0; const char *keyPtr = NULL; - void *pickledPtr = NULL; OlmOutboundGroupSession* sessionPtr = NULL; LOGD("## outbound group session serializeDataWithKeyJni(): IN"); - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr"); } - else if(0 == aKey) + else if (!aKey) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); } - else if(0 == aErrorMsg) + else if (!aErrorMsg) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); } - else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg))) + else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); } - else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) + else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); } - else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); } @@ -380,7 +380,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast(pickledLength), static_cast(keyLength)); LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr); - if(NULL == (pickledPtr = (void*)malloc((pickledLength+1)*sizeof(uint8_t)))) + void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + + if(!pickledPtr) { LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM"); } @@ -391,12 +393,12 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr); - if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr))) + if (!(errorJstring = env->NewStringUTF(errorMsgPtr))) { env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring); } @@ -409,17 +411,14 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); } } - } - // free alloc - if(NULL != keyPtr) - { - env->ReleaseStringUTFChars(aKey, keyPtr); + free(pickledPtr); } - if(NULL != pickledPtr) + // free alloc + if (keyPtr) { - free(pickledPtr); + env->ReleaseStringUTFChars(aKey, keyPtr); } return pickledDataRetValue; @@ -435,23 +434,23 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni) LOGD("## initWithSerializedDataJni(): IN"); - if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM"); } - else if(0 == aKey) + else if (!aKey) { LOGE(" ## initWithSerializedDataJni(): failure - invalid key"); } - else if(0 == aSerializedData) + else if (!aSerializedData) { LOGE(" ## initWithSerializedDataJni(): failure - serialized data"); } - else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM"); } - else if(NULL == (pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) + else if (!(pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) { LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM"); } @@ -468,7 +467,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni) keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_outbound_group_session() Msg=%s",errorMsgPtr); @@ -481,12 +480,12 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni) } // free alloc - if(NULL != keyPtr) + if (keyPtr) { env->ReleaseStringUTFChars(aKey, keyPtr); } - if(NULL != pickledPtr) + if (pickledPtr) { env->ReleaseStringUTFChars(aSerializedData, pickledPtr); } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp index 7ec46df..28edae1 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp @@ -26,17 +26,18 @@ using namespace AndroidOlmSdk; **/ OlmSession* initializeSessionMemory() { - OlmSession* sessionPtr = NULL; size_t sessionSize = olm_session_size(); + OlmSession* sessionPtr = (OlmSession*)malloc(sessionSize); - if(NULL != (sessionPtr=(OlmSession*)malloc(sessionSize))) - { // init session object - sessionPtr = olm_session(sessionPtr); - LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast(sessionSize)); + if (sessionPtr) + { + // init session object + sessionPtr = olm_session(sessionPtr); + LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast(sessionSize)); } else { - LOGE("## initializeSessionMemory(): failure - OOM"); + LOGE("## initializeSessionMemory(): failure - OOM"); } return sessionPtr; @@ -53,21 +54,20 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) { - OlmSession* sessionPtr = NULL; - - LOGD("## releaseSessionJni(): IN"); + LOGD("## releaseSessionJni(): IN"); + OlmSession* sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) - { - LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); - } - else - { - olm_clear_session(sessionPtr); + if (!sessionPtr) + { + LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); + } + else + { + olm_clear_session(sessionPtr); - // even if free(NULL) does not crash, logs are performed for debug purpose - free(sessionPtr); - } + // even if free(NULL) does not crash, logs are performed for debug purpose + free(sessionPtr); + } } /** @@ -78,18 +78,18 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz **/ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) { - OlmSession* sessionPtr = NULL; - LOGD("## initNewSessionJni(): OlmSession IN"); + OlmSession* sessionPtr = initializeSessionMemory(); + // init account memory allocation - if(NULL == (sessionPtr = initializeSessionMemory())) + if (!sessionPtr) { LOGE(" ## initNewSessionJni(): failure - init session OOM"); } else { - LOGD(" ## initNewSessionJni(): success - OLM session created"); + LOGD(" ## initNewSessionJni(): success - OLM session created"); } return (jlong)(intptr_t)sessionPtr; @@ -112,38 +112,41 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject jint retCode = ERROR_CODE_KO; OlmSession* sessionPtr = NULL; OlmAccount* accountPtr = NULL; - const char* theirIdentityKeyPtr = NULL; - const char* theirOneTimeKeyPtr = NULL; - uint8_t *randomBuffPtr = NULL; - size_t sessionResult; - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## initOutboundSessionJni(): failure - invalid Session ptr=NULL"); } - else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) + else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) { LOGE("## initOutboundSessionJni(): failure - invalid Account ptr=NULL"); } - else if((0==aTheirIdentityKey) || (0==aTheirOneTimeKey)) + else if (!aTheirIdentityKey || !aTheirOneTimeKey) { LOGE("## initOutboundSessionJni(): failure - invalid keys"); } else - { // allocate random buffer + { size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); + uint8_t *randomBuffPtr = NULL; + LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast(randomSize)); - if((0!=randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) + + if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) { LOGE("## initOutboundSessionJni(): failure - random buffer init"); } else - { // convert identity & one time keys to C strings - if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) + { + const char* theirIdentityKeyPtr = NULL; + const char* theirOneTimeKeyPtr = NULL; + + // convert identity & one time keys to C strings + if (!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) { LOGE("## initOutboundSessionJni(): failure - identityKey JNI allocation OOM"); } - else if(NULL == (theirOneTimeKeyPtr = env->GetStringUTFChars(aTheirOneTimeKey, 0))) + else if (!(theirOneTimeKeyPtr = env->GetStringUTFChars(aTheirOneTimeKey, 0))) { LOGE("## initOutboundSessionJni(): failure - one time Key JNI allocation OOM"); } @@ -153,7 +156,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject size_t theirOneTimeKeyLength = (size_t)env->GetStringUTFLength(aTheirOneTimeKey); LOGD("## initOutboundSessionJni(): identityKey=%s oneTimeKey=%s",theirIdentityKeyPtr,theirOneTimeKeyPtr); - sessionResult = olm_create_outbound_session(sessionPtr, + size_t sessionResult = olm_create_outbound_session(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, @@ -161,7 +164,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject theirOneTimeKeyLength, (void*)randomBuffPtr, randomSize); - if(sessionResult == olm_error()) { + if (sessionResult == olm_error()) { LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else @@ -170,24 +173,23 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject LOGD("## initOutboundSessionJni(): success - result=%lu", static_cast(sessionResult)); } } - } - } - // **** free mem alloc *** - if(NULL!= randomBuffPtr) - { - free(randomBuffPtr); - } + if (theirIdentityKeyPtr) + { + env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); + } - if(NULL!= theirIdentityKeyPtr) - { - env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); - } + if (theirOneTimeKeyPtr) + { + env->ReleaseStringUTFChars(aTheirOneTimeKey, theirOneTimeKeyPtr); + } - if(NULL!= theirOneTimeKeyPtr) - { - env->ReleaseStringUTFChars(aTheirOneTimeKey, theirOneTimeKeyPtr); - } + if (randomBuffPtr) + { + free(randomBuffPtr); + } + } + } return retCode; } @@ -208,24 +210,25 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; OlmAccount *accountPtr = NULL; - const char *messagePtr = NULL; size_t sessionResult; - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## initInboundSessionJni(): failure - invalid Session ptr=NULL"); } - else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) + else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) { LOGE("## initInboundSessionJni(): failure - invalid Account ptr=NULL"); } - else if(0==aOneTimeKeyMsg) + else if (!aOneTimeKeyMsg) { LOGE("## initInboundSessionJni(): failure - invalid message"); } else - { // convert message to C strings - if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) + { + const char *messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0); + + if (!messagePtr) { LOGE("## initInboundSessionJni(): failure - message JNI allocation OOM"); } @@ -235,7 +238,9 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject LOGD("## initInboundSessionJni(): messageLength=%lu message=%s", static_cast(messageLength), messagePtr); sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength); - if(sessionResult == olm_error()) { + + if (sessionResult == olm_error()) + { LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else @@ -268,27 +273,27 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, const char *theirIdentityKeyPtr = NULL; size_t sessionResult; - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); } - else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) + else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) { LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Account ptr=NULL"); } - else if(0 == aTheirIdentityKey) + else if (!aTheirIdentityKey) { LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); } - else if(0==aOneTimeKeyMsg) + else if (!aOneTimeKeyMsg) { LOGE("## initInboundSessionJni(): failure - invalid one time key message"); } - else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) + else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) { LOGE("## initInboundSessionFromIdKeyJni(): failure - message JNI allocation OOM"); } - else if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) + else if(!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) { LOGE("## initInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); } @@ -300,7 +305,8 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, LOGD("## initInboundSessionFromIdKeyJni(): message=%s messageLength=%lu",messagePtr,static_cast(messageLength)); sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength); - if(sessionResult == olm_error()) { + if (sessionResult == olm_error()) + { LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else @@ -311,11 +317,12 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, } // free local alloc - if(NULL!= messagePtr) + if (messagePtr) { env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); } - if(NULL!= theirIdentityKeyPtr) + + if (theirIdentityKeyPtr) { env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); } @@ -335,15 +342,15 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje OlmSession *sessionPtr = NULL; const char *messagePtr = NULL; - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## matchesInboundSessionJni(): failure - invalid Session ptr=NULL"); } - else if(0==aOneTimeKeyMsg) + else if (!aOneTimeKeyMsg) { LOGE("## matchesInboundSessionJni(): failure - invalid one time key message"); } - else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) + else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) { LOGE("## matchesInboundSessionJni(): failure - one time key JNI allocation OOM"); } @@ -354,7 +361,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength); //if(matchResult == olm_error()) { // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 - if(matchResult != 1) { + if (matchResult != 1) { LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else @@ -365,9 +372,9 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje } // free local alloc - if(NULL!= messagePtr) + if (messagePtr) { - env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); + env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); } return retCode; @@ -388,23 +395,23 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J const char *messagePtr = NULL; const char *theirIdentityKeyPtr = NULL; - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); } - else if(0 == aTheirIdentityKey) + else if (!aTheirIdentityKey) { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); } - else if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) + else if (!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); } - else if(0==aOneTimeKeyMsg) + else if (!aOneTimeKeyMsg) { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid one time key message"); } - else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) + else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - one time key JNI allocation OOM"); } @@ -412,11 +419,12 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J { size_t identityKeyLength = (size_t)env->GetStringUTFLength(aTheirIdentityKey); size_t messageLength = (size_t)env->GetStringUTFLength(aOneTimeKeyMsg); - size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength); + //if(matchResult == olm_error()) { // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 - if(matchResult != 1) { + if (matchResult != 1) + { LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else @@ -427,14 +435,14 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J } // free local alloc - if(NULL!= theirIdentityKeyPtr) + if (theirIdentityKeyPtr) { - env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); + env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); } - if(NULL!= messagePtr) + if (messagePtr) { - env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); + env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); } return retCode; @@ -452,39 +460,37 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; const char *clearMsgPtr = NULL; - uint8_t *randomBuffPtr = NULL; - void *encryptedMsgPtr = NULL; jclass encryptedMsgJClass = 0; jfieldID encryptedMsgFieldId; jfieldID typeMsgFieldId; LOGD("## encryptMessageJni(): IN "); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL"); } - else if(0 == aClearMsg) + else if (!aClearMsg) { LOGE("## encryptMessageJni(): failure - invalid clear message"); } - else if(0 == aEncryptedMsg) + else if (!aEncryptedMsg) { LOGE("## encryptMessageJni(): failure - invalid encrypted message"); } - else if(NULL == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) + else if (!(clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) { LOGE("## encryptMessageJni(): failure - clear message JNI allocation OOM"); } - else if(0 == (encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) + else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) { LOGE("## encryptMessageJni(): failure - unable to get crypted message class"); } - else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) + else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) { LOGE("## encryptMessageJni(): failure - unable to get message field"); } - else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) + else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) { LOGE("## encryptMessageJni(): failure - unable to get message type field"); } @@ -492,13 +498,16 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz { // get message type size_t messageType = olm_encrypt_message_type(sessionPtr); + uint8_t *randomBuffPtr = NULL; // compute random buffer // Note: olm_encrypt_random_length() can return 0, which means // it just does not need new random data to encrypt a new message size_t randomLength = olm_encrypt_random_length(sessionPtr); + LOGD("## encryptMessageJni(): randomLength=%lu", static_cast(randomLength)); - if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) + + if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) { LOGE("## encryptMessageJni(): failure - random buffer init"); } @@ -507,13 +516,16 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz // alloc buffer for encrypted message size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg); size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); - if(NULL == (encryptedMsgPtr = (void*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)))) + + void *encryptedMsgPtr = malloc((encryptedMsgLength+1)*sizeof(uint8_t)); + + if (!encryptedMsgPtr) { LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); } else { - if(0==randomLength) + if (0 == randomLength) { LOGW("## encryptMessageJni(): random buffer is not required"); } @@ -527,7 +539,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz randomLength, encryptedMsgPtr, encryptedMsgLength); - if(result == olm_error()) + if (result == olm_error()) { LOGE("## encryptMessageJni(): failure - Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } @@ -546,30 +558,23 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz retCode = ERROR_CODE_OK; LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", static_cast(result), static_cast(messageType), static_cast((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr); } + + free(encryptedMsgPtr); } + + free(randomBuffPtr); } } // free alloc - if(NULL != clearMsgPtr) - { - env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); - } - - if(NULL != randomBuffPtr) - { - free(randomBuffPtr); - } - - if(NULL != encryptedMsgPtr) + if (clearMsgPtr) { - free(encryptedMsgPtr); + env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); } return retCode; } - /** * Decrypt a message using the session.
* @param aEncryptedMsg message to decrypt @@ -591,31 +596,31 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t LOGD("## decryptMessageJni(): IN - OlmSession"); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); } - else if(0 == aEncryptedMsg) + else if (!aEncryptedMsg) { LOGE("## decryptMessageJni(): failure - invalid encrypted message"); } - else if(0 == (encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) + else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) { LOGE("## decryptMessageJni(): failure - unable to get encrypted message class"); } - else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) + else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) { LOGE("## decryptMessageJni(): failure - unable to get message field"); } - else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) + else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) { LOGE("## decryptMessageJni(): failure - unable to get message type field"); } - else if(0 == (encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId))) + else if (!(encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId))) { LOGE("## decryptMessageJni(): failure - JNI encrypted object "); } - else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) + else if (!(encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) { LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); } @@ -638,7 +643,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t encryptedMsgLength); // Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length() - if(maxPlainTextLength == olm_error()) + if (maxPlainTextLength == olm_error()) { LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } @@ -657,14 +662,14 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t encryptedMsgLength, plainTextMsgPtr, maxPlainTextLength); - if(plaintextLength == olm_error()) + if (plaintextLength == olm_error()) { LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",(const char *)olm_session_last_error(sessionPtr)); } else { // UTF-8 conversion workaround for issue on Android versions older than Marshmallow (23) - if(aIsUtf8ConversionRequired) + if (aIsUtf8ConversionRequired) { decryptedMsgRetValue = javaCStringToUtf8(env, plainTextMsgPtr, plaintextLength); if(0 == decryptedMsgRetValue) @@ -689,17 +694,17 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t } // free alloc - if(NULL != encryptedMsgPtr) + if (encryptedMsgPtr) { env->ReleaseStringUTFChars(encryptedMsgJstring, encryptedMsgPtr); } - if(NULL != tempEncryptedPtr) + if (tempEncryptedPtr) { free(tempEncryptedPtr); } - if(NULL != plainTextMsgPtr) + if (plainTextMsgPtr) { free(plainTextMsgPtr); } @@ -714,47 +719,49 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t */ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) { - OlmSession *sessionPtr = NULL; - void *sessionIdPtr = NULL; - jstring returnValueStr=0; + jstring returnValueStr=0; - LOGD("## getSessionIdentifierJni(): IN "); + LOGD("## getSessionIdentifierJni(): IN "); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) - { - LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL"); - } - else - { - // get the size to alloc to contain the id - size_t lengthSessionId = olm_session_id_length(sessionPtr); - LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast(lengthSessionId)); + OlmSession *sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz); - if(NULL == (sessionIdPtr = (void*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) - { - LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); - } - else - { - size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - - if (result == olm_error()) - { - LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr)); - } - else - { - // update length - (static_cast(sessionIdPtr))[result] = static_cast('\0'); - - LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast(result), (char*)sessionIdPtr); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); - } - free(sessionIdPtr); - } - } + if (!sessionPtr) + { + LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL"); + } + else + { + // get the size to alloc to contain the id + size_t lengthSessionId = olm_session_id_length(sessionPtr); + LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast(lengthSessionId)); + + void *sessionIdPtr = malloc((lengthSessionId+1)*sizeof(uint8_t)); + + if (!sessionIdPtr) + { + LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); + } + else + { + size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); + + if (result == olm_error()) + { + LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr)); + } + else + { + // update length + (static_cast(sessionIdPtr))[result] = static_cast('\0'); + + LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast(result), (char*)sessionIdPtr); + returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + } + free(sessionIdPtr); + } + } - return returnValueStr; + return returnValueStr; } @@ -771,32 +778,31 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job jmethodID errorMsgMethodId = 0; jstring errorJstring = 0; const char *keyPtr = NULL; - void *pickledPtr = NULL; OlmSession* sessionPtr = NULL; LOGD("## serializeDataWithKeyJni(): IN"); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr"); } - else if(0 == aKey) + else if (!aKey) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); } - else if(0 == aErrorMsg) + else if (!aErrorMsg) { LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); } - else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg))) + else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); } - else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) + else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) { LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); } - else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); } @@ -807,7 +813,9 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast(pickledLength), static_cast(keyLength)); LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr); - if(NULL == (pickledPtr = (void*)malloc((pickledLength+1)*sizeof(uint8_t)))) + void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + + if (!pickledPtr) { LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM"); } @@ -818,7 +826,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_session() Msg=%s",errorMsgPtr); @@ -835,18 +843,15 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr); LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); } + + free(pickledPtr); } } // free alloc - if(NULL != keyPtr) + if (keyPtr) { - env->ReleaseStringUTFChars(aKey, keyPtr); - } - - if(NULL != pickledPtr) - { - free(pickledPtr); + env->ReleaseStringUTFChars(aKey, keyPtr); } return pickledDataRetValue; @@ -862,23 +867,23 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j LOGD("## initWithSerializedDataJni(): IN"); - if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) + if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM"); } - else if(0 == aKey) + else if (!aKey) { LOGE(" ## initWithSerializedDataJni(): failure - invalid key"); } - else if(0 == aSerializedData) + else if (!aSerializedData) { LOGE(" ## initWithSerializedDataJni(): failure - serialized data"); } - else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM"); } - else if(NULL == (pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) + else if (!(pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) { LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM"); } @@ -895,7 +900,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j keyLength, (void*)pickledPtr, pickledLength); - if(result == olm_error()) + if (result == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr); @@ -909,12 +914,12 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j } // free alloc - if(NULL != keyPtr) + if (keyPtr) { env->ReleaseStringUTFChars(aKey, keyPtr); } - if(NULL != pickledPtr) + if (pickledPtr) { env->ReleaseStringUTFChars(aSerializedData, pickledPtr); } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp index 4770280..8a7607c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp @@ -21,17 +21,17 @@ using namespace AndroidOlmSdk; OlmUtility* initializeUtilityMemory() { - OlmUtility* utilityPtr = NULL; size_t utilitySize = olm_utility_size(); + OlmUtility* utilityPtr = (OlmUtility*)malloc(utilitySize); - if(NULL != (utilityPtr=(OlmUtility*)malloc(utilitySize))) + if (utilityPtr) { - utilityPtr = olm_utility(utilityPtr); - LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",static_cast(utilitySize)); + utilityPtr = olm_utility(utilityPtr); + LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",static_cast(utilitySize)); } else { - LOGE("## initializeUtilityMemory(): failure - OOM"); + LOGE("## initializeUtilityMemory(): failure - OOM"); } return utilityPtr; @@ -39,12 +39,12 @@ OlmUtility* initializeUtilityMemory() JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz) { - OlmUtility* utilityPtr = NULL; + OlmUtility* utilityPtr = initializeUtilityMemory(); LOGD("## initUtilityJni(): IN"); // init account memory allocation - if(NULL == (utilityPtr = initializeUtilityMemory())) + if (!utilityPtr) { LOGE(" ## initUtilityJni(): failure - init OOM"); } @@ -59,19 +59,19 @@ JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz) JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz) { - OlmUtility* utilityPtr = NULL; - - LOGD("## releaseUtilityJni(): IN"); - - if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) - { - LOGE("## releaseUtilityJni(): failure - utility ptr=NULL"); - } - else - { - olm_clear_utility(utilityPtr); - free(utilityPtr); - } + OlmUtility* utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz); + + LOGD("## releaseUtilityJni(): IN"); + + if (!utilityPtr) + { + LOGE("## releaseUtilityJni(): failure - utility ptr=NULL"); + } + else + { + olm_clear_utility(utilityPtr); + free(utilityPtr); + } } @@ -95,23 +95,23 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j LOGD("## verifyEd25519SignatureJni(): IN"); - if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) + if (!(utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) { LOGE(" ## verifyEd25519SignatureJni(): failure - invalid utility ptr=NULL"); } - else if((0 == aSignature) || (0 == aKey) || (0 == aMessage)) + else if (!aSignature || !aKey || !aMessage) { LOGE(" ## verifyEd25519SignatureJni(): failure - invalid input parameters "); } - else if(0 == (signaturePtr = env->GetStringUTFChars(aSignature, 0))) + else if (!(signaturePtr = env->GetStringUTFChars(aSignature, 0))) { LOGE(" ## verifyEd25519SignatureJni(): failure - signature JNI allocation OOM"); } - else if(0 == (keyPtr = env->GetStringUTFChars(aKey, 0))) + else if (!(keyPtr = env->GetStringUTFChars(aKey, 0))) { LOGE(" ## verifyEd25519SignatureJni(): failure - key JNI allocation OOM"); } - else if(0 == (messagePtr = env->GetStringUTFChars(aMessage, 0))) + else if (!(messagePtr = env->GetStringUTFChars(aMessage, 0))) { LOGE(" ## verifyEd25519SignatureJni(): failure - message JNI allocation OOM"); } @@ -130,7 +130,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j messageLength, (void*)signaturePtr, signatureLength); - if(result == olm_error()) { + if (result == olm_error()) { const char *errorMsgPtr = olm_utility_last_error(utilityPtr); errorMessageRetValue = env->NewStringUTF(errorMsgPtr); LOGE("## verifyEd25519SignatureJni(): failure - olm_ed25519_verify Msg=%s",errorMsgPtr); @@ -142,17 +142,17 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j } // free alloc - if(NULL != signaturePtr) + if (signaturePtr) { env->ReleaseStringUTFChars(aSignature, signaturePtr); } - if(NULL != keyPtr) + if (keyPtr) { env->ReleaseStringUTFChars(aKey, keyPtr); } - if(NULL != messagePtr) + if (messagePtr) { env->ReleaseStringUTFChars(aMessage, messagePtr); } @@ -171,19 +171,18 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst jstring sha256RetValue = 0; OlmUtility* utilityPtr = NULL; const char* messagePtr = NULL; - void *hashValuePtr = NULL; LOGD("## sha256Jni(): IN"); - if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) + if (!(utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) { LOGE(" ## sha256Jni(): failure - invalid utility ptr=NULL"); } - else if(0 == aMessageToHash) + else if(!aMessageToHash) { LOGE(" ## sha256Jni(): failure - invalid message parameters "); } - else if(0 == (messagePtr = env->GetStringUTFChars(aMessageToHash, 0))) + else if(!(messagePtr = env->GetStringUTFChars(aMessageToHash, 0))) { LOGE(" ## sha256Jni(): failure - message JNI allocation OOM"); } @@ -192,8 +191,9 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst // get lengths size_t messageLength = (size_t)env->GetStringUTFLength(aMessageToHash); size_t hashLength = olm_sha256_length(utilityPtr); + void* hashValuePtr = malloc((hashLength+1)*sizeof(uint8_t)); - if(NULL == (hashValuePtr = static_cast(malloc((hashLength+1)*sizeof(uint8_t))))) + if (!hashValuePtr) { LOGE("## sha256Jni(): failure - hash value allocation OOM"); } @@ -204,7 +204,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst messageLength, (void *)hashValuePtr, hashLength); - if(result == olm_error()) + if (result == olm_error()) { LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr)); } @@ -216,16 +216,12 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst LOGD("## sha256Jni(): success - result=%lu hashValue=%s",static_cast(result), (char*)hashValuePtr); sha256RetValue = env->NewStringUTF((const char*)hashValuePtr); } - } + free(hashValuePtr); + } } - if(NULL != hashValuePtr) - { - free(hashValuePtr); - } - - if(NULL != messagePtr) + if (messagePtr) { env->ReleaseStringUTFChars(aMessageToHash, messagePtr); } -- cgit v1.2.3