Commit 99ff3d15 authored by ylecollen's avatar ylecollen
Browse files

remove the useless malloc(len+1) + zero terminated

parent 30c8d069
......@@ -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.<br>
......@@ -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);
}
......
......@@ -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<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(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<long unsigned int>(pickledLength), static_cast<long unsigned int>(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<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
LOGD(" ## serializeJni(): success - result=%lu pickled starts with = %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
pickledDataRetValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr);
}
......
......@@ -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<long unsigned int>(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<char>('\0');
LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId starts with =%10s",static_cast<long unsigned int>(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<uint8_t*>(malloc((maxPlainTextLength+1)*sizeof(uint8_t)));
uint8_t *plainTextMsgPtr = static_cast<uint8_t*>(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<long unsigned int>(pickledLength), static_cast<long unsigned int>(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<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
pickledDataRet = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr);
......
......@@ -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<long unsigned int>(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<char>('\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<long unsigned int>(result), reinterpret_cast<char*>(sessionIdPtr));
LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId starts with %10s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(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<long unsigned int>(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<char>('\0');
LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionKeyPtr));
LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey starts with %10s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(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<char>('\0');
LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%10s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(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<long unsigned int>(pickledLength), static_cast<long unsigned int>(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<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
returnValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
......
......@@ -461,15 +461,16 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
* Encrypt a message using the session.<br>
* @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<char*>(encryptedMsgPtr))[result] = static_cast<char>('\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<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr);
LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg starts with %10s", static_cast<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((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<long unsigned int>(maxPlainTextLength));
// allocate output decrypted message
plainTextMsgPtr = static_cast<uint8_t*>(malloc((maxPlainTextLength+1)*sizeof(uint8_t)));
plainTextMsgPtr = static_cast<uint8_t*>(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<long unsigned int>(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<char*>(sessionIdPtr))[result] = static_cast<char>('\0');
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId= starts with %10s",static_cast<long unsigned int>(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<long unsigned int>(pickledLength), static_cast<long unsigned int>(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<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
LOGD(" ## serializeJni(): success - result=%lu pickled starts with %s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
returnValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
......
......@@ -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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment