diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2022-08-19 15:04:03 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2022-08-19 15:04:03 +0200 |
commit | 77f7206d91a12abd4effd5c20188653e83faa54b (patch) | |
tree | 6b3afceaa076bca87354eda6aa3354aef40cdb2b /src/account-server/storage.cpp | |
parent | 5c44b1a4e438fc28729de9bdb632907638ede60c (diff) | |
download | manaserv-77f7206d91a12abd4effd5c20188653e83faa54b.tar.gz manaserv-77f7206d91a12abd4effd5c20188653e83faa54b.tar.bz2 manaserv-77f7206d91a12abd4effd5c20188653e83faa54b.tar.xz manaserv-77f7206d91a12abd4effd5c20188653e83faa54b.zip |
Fixed possible leak in AccountHandler::handleUnregisterMessage
Fixed by changing account instances to be managed by std::unique_ptr, so
we don't forget to delete them somewhere, like in that function as well
as during shutdown in AccountHandler.
Diffstat (limited to 'src/account-server/storage.cpp')
-rw-r--r-- | src/account-server/storage.cpp | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/src/account-server/storage.cpp b/src/account-server/storage.cpp index fd9160d3..ea2b0eff 100644 --- a/src/account-server/storage.cpp +++ b/src/account-server/storage.cpp @@ -162,7 +162,7 @@ void Storage::close() mDb->disconnect(); } -Account *Storage::getAccountBySQL() +std::unique_ptr<Account> Storage::getAccountBySQL() { try { @@ -178,7 +178,7 @@ Account *Storage::getAccountBySQL() // Create an Account instance // and initialize it with information about the user. - Account *account = new Account(id); + std::unique_ptr<Account> account { new Account(id) }; account->setName(accountInfo(0, 1)); account->setPassword(accountInfo(0, 2)); account->setEmail(accountInfo(0, 3)); @@ -223,7 +223,7 @@ Account *Storage::getAccountBySQL() for (int k = 0; k < size; ++k) { if (CharacterData *ptr = - getCharacter(characterIDs[k], account)) + getCharacter(characterIDs[k], account.get())) { characters[ptr->getCharacterSlot()] = ptr; } @@ -315,7 +315,7 @@ void Storage::fixCharactersSlot(int accountId) } } -Account *Storage::getAccount(const std::string &userName) +std::unique_ptr<Account> Storage::getAccount(const std::string &userName) { std::ostringstream sql; sql << "SELECT * FROM " << ACCOUNTS_TBL_NAME << " WHERE username = ?"; @@ -327,7 +327,7 @@ Account *Storage::getAccount(const std::string &userName) return 0; } -Account *Storage::getAccount(int accountID) +std::unique_ptr<Account> Storage::getAccount(int accountID) { std::ostringstream sql; sql << "SELECT * FROM " << ACCOUNTS_TBL_NAME << " WHERE id = ?"; @@ -892,9 +892,9 @@ bool Storage::updateCharacter(CharacterData *character) return true; } -void Storage::addAccount(Account *account) +void Storage::addAccount(Account &account) { - assert(account->getCharacters().size() == 0); + assert(account.getCharacters().size() == 0); using namespace dal; @@ -906,18 +906,18 @@ void Storage::addAccount(Account *account) << " (username, password, email, level, " << "banned, registration, lastlogin)" << " VALUES (?, ?, ?, " - << account->getLevel() << ", 0, " - << account->getRegistrationDate() << ", " - << account->getLastLogin() << ");"; + << account.getLevel() << ", 0, " + << account.getRegistrationDate() << ", " + << account.getLastLogin() << ");"; if (mDb->prepareSql(sql.str())) { - mDb->bindValue(1, account->getName()); - mDb->bindValue(2, account->getPassword()); - mDb->bindValue(3, account->getEmail()); + mDb->bindValue(1, account.getName()); + mDb->bindValue(2, account.getPassword()); + mDb->bindValue(3, account.getEmail()); mDb->processSql(); - account->setID(mDb->getLastId()); + account.setID(mDb->getLastId()); } else { @@ -931,9 +931,9 @@ void Storage::addAccount(Account *account) } } -void Storage::flush(Account *account) +void Storage::flush(const Account &account) { - assert(account->getID() >= 0); + assert(account.getID() >= 0); using namespace dal; @@ -950,12 +950,12 @@ void Storage::flush(Account *account) if (mDb->prepareSql(sqlUpdateAccountTable.str())) { - mDb->bindValue(1, account->getName()); - mDb->bindValue(2, account->getPassword()); - mDb->bindValue(3, account->getEmail()); - mDb->bindValue(4, account->getLevel()); - mDb->bindValue(5, account->getLastLogin()); - mDb->bindValue(6, account->getID()); + mDb->bindValue(1, account.getName()); + mDb->bindValue(2, account.getPassword()); + mDb->bindValue(3, account.getEmail()); + mDb->bindValue(4, account.getLevel()); + mDb->bindValue(5, account.getLastLogin()); + mDb->bindValue(6, account.getID()); mDb->processSql(); } @@ -966,7 +966,7 @@ void Storage::flush(Account *account) } // Get the list of characters that belong to this account. - Characters &characters = account->getCharacters(); + const Characters &characters = account.getCharacters(); // Insert or update the characters. for (Characters::const_iterator it = characters.begin(), @@ -988,12 +988,12 @@ void Storage::flush(Account *account) << " (user_id, name, gender, hair_style, hair_color," << " char_pts, correct_pts," << " x, y, map_id, slot) values (" - << account->getID() << ", ?, " + << account.getID() << ", ?, " << character->getGender() << ", " - << (int)character->getHairStyle() << ", " - << (int)character->getHairColor() << ", " - << (int)character->getAttributePoints() << ", " - << (int)character->getCorrectionPoints() << ", " + << character->getHairStyle() << ", " + << character->getHairColor() << ", " + << character->getAttributePoints() << ", " + << character->getCorrectionPoints() << ", " << character->getPosition().x << ", " << character->getPosition().y << ", " << character->getMapId() << ", " @@ -1029,7 +1029,7 @@ void Storage::flush(Account *account) std::ostringstream sqlSelectNameIdCharactersTable; sqlSelectNameIdCharactersTable << "select name, id from " << CHARACTERS_TBL_NAME - << " where user_id = '" << account->getID() << "';"; + << " where user_id = '" << account.getID() << "';"; const RecordSet& charInMemInfo = mDb->execSql(sqlSelectNameIdCharactersTable.str()); @@ -1069,7 +1069,7 @@ void Storage::flush(Account *account) } } -void Storage::delAccount(Account *account) +void Storage::delAccount(Account &account) { // Sync the account info into the database. flush(account); @@ -1079,11 +1079,11 @@ void Storage::delAccount(Account *account) // Delete the account. std::ostringstream sql; sql << "delete from " << ACCOUNTS_TBL_NAME - << " where id = '" << account->getID() << "';"; + << " where id = '" << account.getID() << "';"; mDb->execSql(sql.str()); // Remove the account's characters. - account->setCharacters(Characters()); + account.setCharacters(Characters()); } catch (const std::exception &e) { @@ -1091,14 +1091,14 @@ void Storage::delAccount(Account *account) } } -void Storage::updateLastLogin(const Account *account) +void Storage::updateLastLogin(const Account &account) { try { std::ostringstream sql; sql << "UPDATE " << ACCOUNTS_TBL_NAME - << " SET lastlogin = '" << account->getLastLogin() << "'" - << " WHERE id = '" << account->getID() << "';"; + << " SET lastlogin = '" << account.getLastLogin() << "'" + << " WHERE id = '" << account.getID() << "';"; mDb->execSql(sql.str()); } catch (const dal::DbSqlQueryExecFailure &e) |