From e1dbec0b78671a4e90e578a2ab67b7b114d3b57b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 16 May 2018 11:22:17 +0200 Subject: Fix memory leak reported by valgrind --- depends/odhtdb | 2 +- src/Cache.cpp | 4 ++-- src/Gif.cpp | 6 +++--- src/main.cpp | 56 ++++---------------------------------------------------- 4 files changed, 10 insertions(+), 58 deletions(-) diff --git a/depends/odhtdb b/depends/odhtdb index 7f19b68..05920c5 160000 --- a/depends/odhtdb +++ b/depends/odhtdb @@ -1 +1 @@ -Subproject commit 7f19b686cc7b47346ba8bab89bb34188f366db10 +Subproject commit 05920c5c5ffcedc435eeee29f6357c6b4fdc9c4f diff --git a/src/Cache.cpp b/src/Cache.cpp index 66b6f71..1295475 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -146,7 +146,7 @@ namespace dchat sf::Texture *texture = new sf::Texture(); if(texture->loadFromMemory(fileContent.data, fileContent.size)) { - delete fileContent.data; + delete[] fileContent.data; fileContent.data = nullptr; texture->setSmooth(true); texture->generateMipmap(); @@ -167,7 +167,7 @@ namespace dchat } } while(offset < fileContent.size); - delete fileContent.data; + delete[] fileContent.data; fileContent.data = nullptr; if(foundHtmlContent) diff --git a/src/Gif.cpp b/src/Gif.cpp index 69c0be8..afd5b9d 100644 --- a/src/Gif.cpp +++ b/src/Gif.cpp @@ -73,7 +73,7 @@ namespace dchat } catch(GifLoadException &e) { - delete fileContent.data; + delete[] fileContent.data; throw e; } } @@ -89,7 +89,7 @@ namespace dchat } catch(GifLoadException &e) { - delete fileContent.data; + delete[] fileContent.data; throw e; } } @@ -131,7 +131,7 @@ namespace dchat Gif::~Gif() { gif_finalise(&gif); - delete fileContent.data; + delete[] fileContent.data; } sf::Vector2u Gif::getSize() const diff --git a/src/main.cpp b/src/main.cpp index 249c5d4..39ccd43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,39 +25,6 @@ using namespace std; using namespace dchat; using namespace TinyProcessLib; -string createChannelJoinKey(const unique_ptr &databaseCreateResponse) -{ - string result; - result += databaseCreateResponse->getRequestHash()->toString(); - result += "&"; - result += odhtdb::bin2hex((const char*)databaseCreateResponse->getNodeEncryptionKey()->data, databaseCreateResponse->getNodeEncryptionKey()->size); - return result; -} - -string createJoinKeyFromDatabaseNode(const odhtdb::DatabaseNode &databaseNode) -{ - string result; - result += databaseNode.getRequestHash()->toString(); - result += "&"; - result += odhtdb::bin2hex((const char*)databaseNode.getNodeEncryptionKey()->data, databaseNode.getNodeEncryptionKey()->size); - return result; -} - -odhtdb::DatabaseNode createDatabaseNodeFromJoinKey(const string &joinKey) -{ - string nodeHashRaw = odhtdb::hex2bin(joinKey.c_str(), 64); - shared_ptr nodeHash = make_shared(); - memcpy(nodeHash->getData(), nodeHashRaw.data(), nodeHashRaw.size()); - - string nodeEncryptionKeyRaw = odhtdb::hex2bin(joinKey.c_str() + 65, 64); - shared_ptr encryptionKey = make_shared(); - encryptionKey->data = new char[nodeEncryptionKeyRaw.size()]; - memcpy(encryptionKey->data, nodeEncryptionKeyRaw.data(), nodeEncryptionKeyRaw.size()); - encryptionKey->size = nodeEncryptionKeyRaw.size(); - - return odhtdb::DatabaseNode(encryptionKey, nodeHash); -} - void channelChangeUserNickname(Channel *channel, const StringView data, const odhtdb::Signature::PublicKey &userPublicKey) { auto user = channel->getUserByPublicKey(userPublicKey); @@ -328,6 +295,7 @@ int main(int argc, char **argv) loggedIn = true; }); + // TODO: Use database->addData to change channel name // Create channel Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword](const vector &args) { @@ -345,7 +313,7 @@ int main(int argc, char **argv) } auto createResponse = database->create(); - printf("Created database '%s', join key: '%s'\n", args[0].c_str(), createChannelJoinKey(createResponse).c_str()); + printf("Created channel %s\n", createResponse->getRequestHash()->toString().c_str()); User *newLocalUser = new OnlineLocalUser("NoName", *createResponse->getNodeAdminKeyPair()); odhtdb::DatabaseNode databaseNode(createResponse->getNodeEncryptionKey(), createResponse->getRequestHash()); @@ -380,7 +348,7 @@ int main(int argc, char **argv) auto channelNodeHash = currentChannel->getNodeInfo().getRequestHash(); auto channelEncryptionKey = currentChannel->getNodeInfo().getNodeEncryptionKey(); - shared_ptr encryptionKey = make_shared(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE); + shared_ptr encryptionKey = make_shared(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE); odhtdb::Encryption::generateKey((unsigned char*)encryptionKey->data); string inviteKey = odhtdb::bin2hex((const char*)channelNodeHash->getData(), channelNodeHash->getSize()); @@ -476,7 +444,7 @@ int main(int argc, char **argv) } string encryptionKeyBinRaw = odhtdb::hex2bin(args[0].c_str() + 65, 64); - shared_ptr encryptionKey = make_shared(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE); + shared_ptr encryptionKey = make_shared(new u8[odhtdb::ENCRYPTION_KEY_BYTE_SIZE], odhtdb::ENCRYPTION_KEY_BYTE_SIZE); memcpy(encryptionKey->data, encryptionKeyBinRaw.data(), encryptionKeyBinRaw.size()); shared_ptr keyPair = make_shared(); @@ -676,22 +644,6 @@ int main(int argc, char **argv) Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); }); - // Get channel join key - Command::add("joinkey", [&offlineChannel](const vector &args) - { - Channel *currentChannel = Channel::getCurrent(); - if(!currentChannel || currentChannel == &offlineChannel) - { - Channel::getCurrent()->addLocalMessage("You are not inside a channel", Channel::getCurrent()->getSystemUser()); - return; - } - - string response = "Join key: "; - response += createJoinKeyFromDatabaseNode(currentChannel->getNodeInfo()); - printf("%s\n", response.c_str()); - Channel::getCurrent()->addLocalMessage(response, Channel::getCurrent()->getSystemUser()); - }); - sf::Event event; while (window.isOpen()) { -- cgit v1.2.3