From 99ff3d15c81b20bc1f4cfd62c33137db8c6e8044 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Mon, 9 Jan 2017 15:03:15 +0100 Subject: remove the useless malloc(len+1) + zero terminated --- .../src/main/java/org/matrix/olm/OlmSession.java | 6 ++-- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 15 ++------- .../src/main/jni/olm_inbound_group_session.cpp | 12 +++---- .../src/main/jni/olm_outbound_group_session.cpp | 26 +++++---------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp | 39 ++++++++-------------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.h | 2 +- 6 files changed, 34 insertions(+), 66 deletions(-) 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 2436f0d..170b1a5 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 @@ -262,7 +262,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { OlmMessage encryptedMsgRetValue = new OlmMessage(); try { - encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue); + encryptedMsgRetValue.mCipherText = new String(encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage()); @@ -271,7 +271,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { return encryptedMsgRetValue; } - private native void encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg); + private native byte[] encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg); /** * Decrypt a message using the session.
@@ -314,7 +314,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * @throws IOException exception * @throws ClassNotFoundException exception */ - private void readObject(ObjectInputStream aInStream) throws Exception { + private void readObject(ObjectInputStream aInStream) throws Exception { deserialize(aInStream); } 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 4da0d67..30828d9 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 @@ -462,8 +462,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t // signature memory allocation size_t signatureLength = olm_account_signature_length(accountPtr); - size_t bufferLen = signatureLength + 1; - void* signedMsgPtr = malloc(bufferLen * sizeof(uint8_t)); + void* signedMsgPtr = malloc(signatureLength * sizeof(uint8_t)); if (!signedMsgPtr) { @@ -486,14 +485,10 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t } else { - // info: signatureLength is always equal to resultSign - (static_cast(signedMsgPtr))[signatureLength] = static_cast('\0'); - LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast(resultSign), static_cast(signatureLength)); signedMsgRetValueBuffer = env->NewByteArray(signatureLength); env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr); - } free(signedMsgPtr); @@ -549,7 +544,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast(pickledLength), static_cast(keyLength)); - void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); if (!pickledPtr) { @@ -570,11 +565,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi } else { - // build success output - (static_cast(pickledPtr))[pickledLength] = static_cast('\0'); - - LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); - + LOGD(" ## serializeJni(): success - result=%lu pickled starts with = %10s", static_cast(result), static_cast(pickledPtr)); pickledDataRetValue = env->NewByteArray(pickledLength); env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr); } 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 7dd250b..2e6e4af 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 @@ -167,7 +167,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast(lengthSessionId)); - uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); + uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)); if (!sessionIdPtr) { @@ -185,8 +185,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN } else { - sessionIdPtr[result] = static_cast('\0'); - LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast(result), (char*)sessionIdPtr); + LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId starts with =%10s",static_cast(result), (char*)sessionIdPtr); returnValue = env->NewByteArray(result); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); @@ -280,7 +279,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn uint32_t messageIndex = 0; // allocate output decrypted message - uint8_t *plainTextMsgPtr = static_cast(malloc((maxPlainTextLength+1)*sizeof(uint8_t))); + uint8_t *plainTextMsgPtr = static_cast(malloc(maxPlainTextLength*sizeof(uint8_t))); // decrypt, but before reload encrypted buffer (previous one was destroyed) memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); @@ -370,7 +369,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu", static_cast(pickledLength), static_cast(keyLength)); - void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); if (!pickledPtr) { @@ -391,8 +390,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en } else { - (static_cast(pickledPtr))[pickledLength] = static_cast('\0'); - LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); + LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast(result), static_cast(pickledPtr)); pickledDataRet = env->NewByteArray(pickledLength); env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr); 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 bfe5a4e..827e8fd 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 @@ -161,7 +161,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast(lengthSessionId)); - uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); + uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)); if (!sessionIdPtr) { @@ -179,13 +179,10 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J } else { - // update length - sessionIdPtr[result] = static_cast('\0'); - returnValue = env->NewByteArray(result); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); - LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",static_cast(result), reinterpret_cast(sessionIdPtr)); + LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId starts with %10s",static_cast(result), reinterpret_cast(sessionIdPtr)); } // free alloc @@ -251,7 +248,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv * size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast(sessionKeyLength)); - uint8_t *sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t)); + uint8_t *sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t)); if (!sessionKeyPtr) { @@ -269,9 +266,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv * } else { - // update length - sessionKeyPtr[result] = static_cast('\0'); - LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",static_cast(result), reinterpret_cast(sessionKeyPtr)); + LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey starts with %10s",static_cast(result), reinterpret_cast(sessionKeyPtr)); returnValue = env->NewByteArray(result); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr); @@ -323,7 +318,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE // compute max encrypted length size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); - uint8_t *encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)); + uint8_t *encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t)); if (!encryptedMsgPtr) { @@ -348,10 +343,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE } else { - // update decrypted buffer size - encryptedMsgPtr[encryptedLength] = static_cast('\0'); - - LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast(encryptedLength), reinterpret_cast(encryptedMsgPtr)); + LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%10s",static_cast(encryptedLength), reinterpret_cast(encryptedMsgPtr)); encryptedMsgRet = env->NewByteArray(encryptedLength); env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr); @@ -412,7 +404,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast(pickledLength), static_cast(keyLength)); - void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); if(!pickledPtr) { @@ -433,9 +425,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e } else { - // build success output - (static_cast(pickledPtr))[pickledLength] = static_cast('\0'); - LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); + LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast(result), static_cast(pickledPtr)); returnValue = env->NewByteArray(pickledLength); env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)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 6e26071..d6ede41 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 @@ -461,15 +461,16 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J * Encrypt a message using the session.
* @param aClearMsg clear text message * @param [out] aEncryptedMsg ciphered message + * @return the encrypted message */ -JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg) +JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg) { + jbyteArray encryptedMsgRet = 0; const char* errorMessage = NULL; OlmSession *sessionPtr = getSessionInstanceId(env, thiz); jbyte *clearMsgPtr = NULL; jclass encryptedMsgJClass = 0; - jfieldID encryptedMsgFieldId; jfieldID typeMsgFieldId; LOGD("## encryptMessageJni(): IN "); @@ -498,11 +499,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz LOGE("## encryptMessageJni(): failure - unable to get crypted message class"); errorMessage = "unable to get crypted message class"; } - else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) - { - LOGE("## encryptMessageJni(): failure - unable to get message field"); - errorMessage = "unable to get message field"; - } else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) { LOGE("## encryptMessageJni(): failure - unable to get message type field"); @@ -532,7 +528,7 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer); size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); - void *encryptedMsgPtr = malloc((encryptedMsgLength+1)*sizeof(uint8_t)); + void *encryptedMsgPtr = malloc(encryptedMsgLength*sizeof(uint8_t)); if (!encryptedMsgPtr) { @@ -562,17 +558,13 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz } else { - // update encrypted buffer size - (static_cast(encryptedMsgPtr))[result] = static_cast('\0'); - // update message type: PRE KEY or normal env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType); - // update message: encryptedMsgPtr => encryptedJstring - jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr); - env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring); + encryptedMsgRet = env->NewByteArray(encryptedMsgLength); + env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedMsgLength, (jbyte*)encryptedMsgPtr); - 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); + LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg starts with %10s", static_cast(result), static_cast(messageType), static_cast((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr); } free(encryptedMsgPtr); @@ -592,6 +584,8 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz { env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); } + + return encryptedMsgRet; } /** @@ -682,7 +676,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobjec LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",static_cast(maxPlainTextLength)); // allocate output decrypted message - plainTextMsgPtr = static_cast(malloc((maxPlainTextLength+1)*sizeof(uint8_t))); + plainTextMsgPtr = static_cast(malloc(maxPlainTextLength*sizeof(uint8_t))); // decrypt, but before reload encrypted buffer (previous one was destroyed) memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); @@ -756,7 +750,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, size_t lengthSessionId = olm_session_id_length(sessionPtr); LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast(lengthSessionId)); - void *sessionIdPtr = malloc((lengthSessionId+1)*sizeof(uint8_t)); + void *sessionIdPtr = malloc(lengthSessionId*sizeof(uint8_t)); if (!sessionIdPtr) { @@ -774,10 +768,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, } else { - // update length - (static_cast(sessionIdPtr))[result] = static_cast('\0'); - - LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast(result), (char*)sessionIdPtr); + LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId= starts with %10s",static_cast(result), (char*)sessionIdPtr); returnValue = env->NewByteArray(result); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); @@ -832,7 +823,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast(pickledLength), static_cast(keyLength)); - void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); + void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); if (!pickledPtr) { @@ -853,9 +844,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi } else { - // build success output - (static_cast(pickledPtr))[pickledLength] = static_cast('\0'); - LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast(result), static_cast(pickledPtr)); + LOGD(" ## serializeJni(): success - result=%lu pickled starts with %s", static_cast(result), static_cast(pickledPtr)); returnValue = env->NewByteArray(pickledLength); env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h index 76f59c5..01cf96c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h @@ -43,7 +43,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg); // encrypt/decrypt -JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg); +JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg); JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz); -- cgit v1.2.3