Commit 41b2163c authored by Johan Pascal's avatar Johan Pascal

Memory leak

parent 1ab274fa
......@@ -130,7 +130,14 @@ int bzrtp_setZIDCache(bzrtpContext_t *context, void *zidCache, const char *selfU
/* zidCache pointer is actually a pointer to sqlite3 db, store it in context */
context->zidCache = (sqlite3 *)zidCache;
if (context->selfURI != NULL) {
free(context->selfURI);
}
context->selfURI = strdup(selfURI);
if (context->peerURI != NULL) {
free(context->peerURI);
}
context->peerURI = strdup(peerURI);
/* and init the cache(create needed tables if they don't exist) */
......
......@@ -418,6 +418,7 @@ int bzrtp_cache_getZuid(void *dbPointer, const char *selfURI, const char *peerUR
if (localZID==NULL) { /* this sip URI is not in our DB, do not create an association with the peer ZID/URI binding */
return BZRTP_ZIDCACHE_BADINPUTDATA;
} else { /* yes we know this URI on local device, add a row in the ziduri table */
free(localZID);
stmt = sqlite3_mprintf("INSERT INTO ziduri (zid,selfuri,peeruri) VALUES(?,?,?);");
ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
if (ret != SQLITE_OK) {
......@@ -531,6 +532,7 @@ int bzrtp_cache_write(void *dbPointer, int zuid, const char *tableName, const ch
stmt = sqlite3_mprintf("INSERT INTO %w (%s) VALUES(%s);", tableName, insertColumnsString, valuesBindingString);
free(insertColumnsString);
ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
sqlite3_free(stmt);
if (ret != SQLITE_OK) {
return BZRTP_ZIDCACHE_UNABLETOUPDATE;
}
......
......@@ -727,6 +727,12 @@ static void test_cache_enabled_exchange(void) {
BC_ASSERT_EQUAL(*colValuesAlice[2], 1, int, "%d");
BC_ASSERT_EQUAL(*colValuesBob[2], 1, int, "%d");
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesAlice[i]);
colValuesAlice[i]=NULL;
}
/* make a second exchange */
BC_ASSERT_EQUAL(multichannel_exchange(NULL, NULL, defaultCryptoAlgoSelection(), aliceDB, "alice@sip.linphone.org", bobDB, "bob@sip.linphone.org"), 0, int, "%x");
/* read new values in cache, ZIDs and zuids must be identical, read alice first to be able to check rs2 with old rs1 */
......@@ -736,6 +742,12 @@ static void test_cache_enabled_exchange(void) {
BC_ASSERT_EQUAL(colLengthAlice[1], 32, int, "%d");
BC_ASSERT_EQUAL(colLengthAlice[2], 1, int, "%d");
BC_ASSERT_EQUAL(memcmp(colValuesAlice[1], colValuesBob[0], 32), 0, int, "%d"); /* colValuesBob, still old values from before the second exchange */
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesBob[i]);
colValuesBob[i]=NULL;
}
/* so read bob updated values and compare rs1, rs2 and check pvs is still at 1 */
BC_ASSERT_EQUAL(bzrtp_cache_read((void *)bobDB, zuidBob, "zrtp", colNames, colValuesBob, colLengthBob, 3), 0, int, "%x");
BC_ASSERT_EQUAL(colLengthBob[0], 32, int, "%d");
......@@ -817,6 +829,15 @@ static void test_cache_mismatch_exchange(void) {
colValuesAlice[0][0] += 1;
BC_ASSERT_EQUAL(bzrtp_cache_write((void *)aliceDB, zuidAlice, "zrtp", colNames, colValuesAlice, colLengthAlice, 1), 0, int, "%x");
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesAlice[i]);
colValuesAlice[i]=NULL;
free(colValuesBob[i]);
colValuesBob[i]=NULL;
}
/* make a third exchange : we have a cache mismatch(on Bob side only), wich means rs1 will not be backed up in rs2 which shall be NULL again */
/* make a second exchange : we have a cache mismatch(both on Bob and Alice side), wich means rs1 will not be backed up in rs2 which shall be NULL again */
/* rs1 will be in sync has the SAS comparison will succeed and pvs will be set to 1*/
BC_ASSERT_EQUAL(multichannel_exchange(NULL, NULL, defaultCryptoAlgoSelection(), aliceDB, "alice@sip.linphone.org", bobDB, "bob@sip.linphone.org"), RET_CACHE_MISMATCH<<16|RET_CACHE_MISMATCH, int, "%x");
......@@ -843,6 +864,14 @@ static void test_cache_mismatch_exchange(void) {
colValuesAlice[2][0] = 0; /* reset pvs to 0 */
BC_ASSERT_EQUAL(bzrtp_cache_write((void *)aliceDB, zuidAlice, "zrtp", colNames, colValuesAlice, colLengthAlice, 3), 0, int, "%x");
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesAlice[i]);
colValuesAlice[i]=NULL;
free(colValuesBob[i]);
colValuesBob[i]=NULL;
}
/* make a third exchange : we have a cache mismatch(on Bob side only), wich means rs1 will not be backed up in rs2 which shall be NULL again */
/* rs1 will be in sync has the SAS comparison will succeed and pvs will be set to 1*/
BC_ASSERT_EQUAL(multichannel_exchange(NULL, NULL, defaultCryptoAlgoSelection(), aliceDB, "alice@sip.linphone.org", bobDB, "bob@sip.linphone.org"), RET_CACHE_MISMATCH<<16, int, "%x");
......@@ -892,6 +921,12 @@ static void test_cache_sas_not_confirmed(void) {
resetGlobalParams();
/* init columns values pointers */
for (i=0; i<3; i++) {
colValuesAlice[i] = NULL;
colValuesBob[i] = NULL;
}
/* create tempory DB files, just try to clean them from dir before, just in case */
remove("tmpZIDAlice_simpleCache.sqlite");
remove("tmpZIDBob_simpleCache.sqlite");
......@@ -926,6 +961,12 @@ static void test_cache_sas_not_confirmed(void) {
BC_ASSERT_EQUAL(colLengthBob[2], 1, int, "%d");
BC_ASSERT_EQUAL(*colValuesBob[2], 1, int, "%d");
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesAlice[i]);
colValuesAlice[i] = NULL;
}
/* make a second exchange, the PVS flag returned by both side shall be 0 as Alice did not validate hers on previous exchange */
/* but let them both validate this one */
BC_ASSERT_EQUAL(multichannel_exchange_pvs_params(NULL, NULL, defaultCryptoAlgoSelection(), aliceDB, "alice@sip.linphone.org", bobDB, "bob@sip.linphone.org", TRUE, 0, 0), 0, int, "%x");
......@@ -936,6 +977,13 @@ static void test_cache_sas_not_confirmed(void) {
BC_ASSERT_EQUAL(colLengthAlice[1], 32, int, "%d");
BC_ASSERT_EQUAL(colLengthAlice[2], 1, int, "%d");
BC_ASSERT_EQUAL(memcmp(colValuesAlice[1], colValuesBob[0], 32), 0, int, "%d"); /* colValuesBob, still old values from before the second exchange */
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesBob[i]);
colValuesBob[i] = NULL;
}
/* so read bob updated values and compare rs1, rs2 and check pvs is at 1 */
BC_ASSERT_EQUAL(bzrtp_cache_read((void *)bobDB, zuidBob, "zrtp", colNames, colValuesBob, colLengthBob, 3), 0, int, "%x");
BC_ASSERT_EQUAL(colLengthBob[0], 32, int, "%d");
......@@ -946,6 +994,12 @@ static void test_cache_sas_not_confirmed(void) {
BC_ASSERT_EQUAL(*colValuesAlice[2], 1, int, "%d");
BC_ASSERT_EQUAL(*colValuesBob[2], 1, int, "%d");
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesAlice[i]);
colValuesAlice[i] = NULL;
}
/* make a third exchange, the PVS flag returned by both side shall be 1 */
BC_ASSERT_EQUAL(multichannel_exchange_pvs_params(NULL, NULL, defaultCryptoAlgoSelection(), aliceDB, "alice@sip.linphone.org", bobDB, "bob@sip.linphone.org", TRUE, 1, 1), 0, int, "%x");
/* read new values in cache, ZIDs and zuids must be identical, read alice first to be able to check rs2 with old rs1 */
......@@ -955,6 +1009,13 @@ static void test_cache_sas_not_confirmed(void) {
BC_ASSERT_EQUAL(colLengthAlice[1], 32, int, "%d");
BC_ASSERT_EQUAL(colLengthAlice[2], 1, int, "%d");
BC_ASSERT_EQUAL(memcmp(colValuesAlice[1], colValuesBob[0], 32), 0, int, "%d"); /* colValuesBob, still old values from before the second exchange */
/* free buffers */
for (i=0; i<3; i++) {
free(colValuesBob[i]);
colValuesBob[i] = NULL;
}
/* so read bob updated values and compare rs1, rs2 and check pvs is at 1 */
/* so read bob updated values and compare rs1, rs2 and check pvs is still at 1 */
BC_ASSERT_EQUAL(bzrtp_cache_read((void *)bobDB, zuidBob, "zrtp", colNames, colValuesBob, colLengthBob, 3), 0, int, "%x");
BC_ASSERT_EQUAL(colLengthBob[0], 32, int, "%d");
......
......@@ -180,9 +180,10 @@ void test_parser_param(uint8_t hvi_trick) {
if (hvi_trick==0) {
BC_ASSERT_EQUAL(retval, 0, int, "%d");
if (retval != 0) goto error;
} else { /* when hvi trick is enable, the DH2 parsing shall fail and return BZRTP_PARSER_ERROR_UNMATCHINGHVI */
} else { /* when hvi trick is enabled, the DH2 parsing shall fail and return BZRTP_PARSER_ERROR_UNMATCHINGHVI */
if (zrtpPacket->messageType==MSGTYPE_DHPART2) {
BC_ASSERT_EQUAL(retval, BZRTP_PARSER_ERROR_UNMATCHINGHVI, int, "%d");
bzrtp_freeZrtpPacket(zrtpPacket);
goto error;
} else {
BC_ASSERT_EQUAL(retval, 0, int, "%d");
......@@ -241,10 +242,6 @@ void test_parser_param(uint8_t hvi_trick) {
BC_FAIL("Unable to build packet");
}
if (freePacketFlag == 1) {
bzrtp_freeZrtpPacket(zrtpPacket);
}
/* modify the hvi stored in the peerPackets, this shall result in parsing failure on DH2 packet */
if (hvi_trick == 1) {
if (zrtpPacket->messageType==MSGTYPE_COMMIT) {
......@@ -256,6 +253,10 @@ void test_parser_param(uint8_t hvi_trick) {
}
}
if (freePacketFlag == 1) {
bzrtp_freeZrtpPacket(zrtpPacket);
}
}
error:
......@@ -348,17 +349,11 @@ static void test_parserComplete() {
bzrtp_initBzrtpContext(contextBob, 0x87654321); /* Bob's SSRC of main channel is 87654321 */
/* now create Alice and BOB Hello packet */
alice_Hello = bzrtp_createZrtpPacket(contextAlice, contextAlice->channelContext[0], MSGTYPE_HELLO, &retval);
if (bzrtp_packetBuild(contextAlice, contextAlice->channelContext[0], alice_Hello, contextAlice->channelContext[0]->selfSequenceNumber) ==0) {
contextAlice->channelContext[0]->selfSequenceNumber++;
contextAlice->channelContext[0]->selfPackets[HELLO_MESSAGE_STORE_ID] = alice_Hello;
}
bob_Hello = bzrtp_createZrtpPacket(contextBob, contextBob->channelContext[0], MSGTYPE_HELLO, &retval);
if (bzrtp_packetBuild(contextBob, contextBob->channelContext[0], bob_Hello, contextBob->channelContext[0]->selfSequenceNumber) ==0) {
contextBob->channelContext[0]->selfSequenceNumber++;
contextBob->channelContext[0]->selfPackets[HELLO_MESSAGE_STORE_ID] = bob_Hello;
}
/* Hello packets are built during the context init(but we must still increase their sequence Number) */
alice_Hello = contextAlice->channelContext[0]->selfPackets[HELLO_MESSAGE_STORE_ID];
bob_Hello = contextBob->channelContext[0]->selfPackets[HELLO_MESSAGE_STORE_ID];
contextAlice->channelContext[0]->selfSequenceNumber++;
contextBob->channelContext[0]->selfSequenceNumber++;
/* now send Alice Hello's to Bob and vice-versa, so they parse them */
alice_HelloFromBob = bzrtp_packetCheck(bob_Hello->packetString, bob_Hello->messageLength+16, contextAlice->channelContext[0]->peerSequenceNumber, &retval);
......@@ -1882,6 +1877,10 @@ static void test_zrtphash(void) {
BC_ASSERT_EQUAL(retval, 0, int, "%d");
retval = bzrtp_packetParser(context12345678, context12345678->channelContext[0], HelloPacketZrtpHash, sizeof(HelloPacketZrtpHash), zrtpPacket);
BC_ASSERT_EQUAL(retval, 0, int, "%d");
/* cleaning */
bzrtp_destroyBzrtpContext(context12345678, 0x12345678);
bzrtp_freeZrtpPacket(zrtpPacket);
}
static test_t packet_parser_tests[] = {
......
......@@ -138,6 +138,12 @@ static void test_cache_getSelfZID(void) {
bzrtp_destroyBzrtpContext(aliceContext, 0); /* note: we didn't initialised any channel, so just give 0 to destroy, it will destroy the bzrtp context itself */
for (i=0; i<4; i++) {
free(readValues[i]);
}
sqlite3_close(aliceDB);
#else /* ZIDCACHE_ENABLED */
bzrtp_message("Test skipped as ZID cache is disabled\n");
#endif
......@@ -193,6 +199,9 @@ static void test_cache_zrtpSecrets(void) {
BC_ASSERT_EQUAL(aliceContext->cachedSecret.pbxsecretLength, 0, int, "%d");
BC_ASSERT_PTR_NULL(aliceContext->cachedSecret.pbxsecret);
BC_ASSERT_EQUAL(aliceContext->cachedSecret.previouslyVerifiedSas, 0, int, "%d");
bzrtp_destroyBzrtpContext(aliceContext, 0); /* note: we didn't initialised any channel, so just give 0 to destroy, it will destroy the bzrtp context itself */
sqlite3_close(aliceDB);
#else /* ZIDCACHE_ENABLED */
bzrtp_message("Test skipped as ZID cache is disabled\n");
#endif /* ZIDCACHE_ENABLED */
......
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