From 0da2bdd7aab5005efe9a7801fac4e9bd59a0bf75 Mon Sep 17 00:00:00 2001 From: Zach H Date: Sun, 19 Mar 2017 12:55:55 -0400 Subject: [PATCH] additional null checks in player, gameselector, remoteclient, log (#2514) --- cockatrice/src/gameselector.cpp | 6 ++++++ cockatrice/src/logger.cpp | 3 ++- cockatrice/src/player.cpp | 22 +++++++++++++++++----- cockatrice/src/remoteclient.cpp | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cockatrice/src/gameselector.cpp b/cockatrice/src/gameselector.cpp index dd88837e..13989a61 100644 --- a/cockatrice/src/gameselector.cpp +++ b/cockatrice/src/gameselector.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "tab_supervisor.h" #include "dlg_creategame.h" #include "dlg_filter_games.h" @@ -129,6 +130,11 @@ void GameSelector::actClearFilter() void GameSelector::actCreate() { + if (room == nullptr) { + qWarning() << "Attempted to create game, but the room was null"; + return; + } + DlgCreateGame dlg(room, room->getGameTypes(), this); dlg.exec(); } diff --git a/cockatrice/src/logger.cpp b/cockatrice/src/logger.cpp index d23ce7c6..fb500d47 100644 --- a/cockatrice/src/logger.cpp +++ b/cockatrice/src/logger.cpp @@ -54,7 +54,8 @@ void Logger::log(QtMsgType /* type */, const QMessageLogContext & /* ctx */, con if(logBuffer.size() > LOGGER_MAX_ENTRIES) logBuffer.removeFirst(); - emit logEntryAdded(message); + if (message.size() > 0) + emit logEntryAdded(message); std::cerr << message.toStdString() << std::endl; // Print to stdout diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 2d5c8c7d..b095bd06 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -481,8 +481,9 @@ void Player::clear() void Player::addPlayer(Player *player) { - if (player == this) + if (player == nullptr || player == this) return; + for (int i = 0; i < playerLists.size(); ++i) { QAction *newAction = playerLists[i]->addAction(player->getName()); newAction->setData(player->getId()); @@ -492,6 +493,9 @@ void Player::addPlayer(Player *player) void Player::removePlayer(Player *player) { + if (player == nullptr) + return; + for (int i = 0; i < playerLists.size(); ++i) { QList actionList = playerLists[i]->actions(); for (int j = 0; j < actionList.size(); ++j) @@ -1137,7 +1141,8 @@ void Player::actCreateAllRelatedCards() void Player::createCard(const CardItem *sourceCard, const QString &dbCardName) { CardInfo *cardInfo = db->getCard(dbCardName); - if(!cardInfo) + + if (cardInfo == nullptr || sourceCard == nullptr) return; // get the target token's location @@ -1172,6 +1177,9 @@ void Player::actSayMessage() void Player::setCardAttrHelper(const GameEventContext &context, CardItem *card, CardAttribute attribute, const QString &avalue, bool allCards) { + if (card == nullptr) + return; + bool moveCardContext = context.HasExtension(Context_MoveCard::ext); switch (attribute) { case AttrTapped: { @@ -1728,6 +1736,9 @@ void Player::processCardAttachment(const ServerInfo_Player &info) void Player::playCard(CardItem *c, bool faceDown, bool tapped) { + if (c == nullptr) + return; + Command_MoveCard cmd; cmd.set_start_player_id(c->getZone()->getPlayer()->getId()); cmd.set_start_zone(c->getZone()->getName().toStdString()); @@ -2347,6 +2358,9 @@ void Player::refreshShortcuts() void Player::updateCardMenu(const CardItem *card) { + if (card == nullptr) + return; + QMenu *cardMenu = card->getCardMenu(); QMenu *ptMenu = card->getPTMenu(); QMenu *moveMenu = card->getMoveMenu(); @@ -2438,10 +2452,8 @@ void Player::updateCardMenu(const CardItem *card) } void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu) { - if (!card || !cardMenu || !card->getInfo()) - { + if (card == nullptr || cardMenu == nullptr || card->getInfo() == nullptr) return; - } QStringList relatedCards = *new QStringList(); relatedCards.append(card->getInfo()->getRelatedCards()); diff --git a/cockatrice/src/remoteclient.cpp b/cockatrice/src/remoteclient.cpp index 5f15d1ea..53a75618 100644 --- a/cockatrice/src/remoteclient.cpp +++ b/cockatrice/src/remoteclient.cpp @@ -283,7 +283,7 @@ void RemoteClient::readData() #ifdef QT_DEBUG qDebug() << "IN" << messageLength << QString::fromStdString(newServerMessage.ShortDebugString()); #endif - inputBuffer.remove(0, messageLength); + qDebug() << inputBuffer.remove(0, messageLength); messageInProgress = false; processProtocolItem(newServerMessage);