mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
[CardDB] Guard guessCard and findPrintingWithId against missing cards (#7592)
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
Build Desktop / Configure (push) Has been cancelled
Build Desktop / Prune caches (push) Has been cancelled
Build Docker / Servatrice (arm) (push) Has been cancelled
Build Docker / Servatrice (x86) (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / Ubuntu 26.04 cover (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 26 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
Build Docker / Publish multi-platform Servatrice image (push) Has been cancelled
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
Build Desktop / Configure (push) Has been cancelled
Build Desktop / Prune caches (push) Has been cancelled
Build Docker / Servatrice (arm) (push) Has been cancelled
Build Docker / Servatrice (x86) (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / Ubuntu 26.04 cover (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 26 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
Build Docker / Publish multi-platform Servatrice image (push) Has been cancelled
* [CardDB] Guard guessCard and findPrintingWithId against missing cards guessCard() dereferenced a null CardInfoPtr through findPrintingWithId() when the name was unknown and a providerId was given. Return an empty ExactCard instead, and make findPrintingWithId() null-safe for every caller. Add regression tests covering both paths. * [CardDB] Rely on PrintingInfo's default null set in findPrintingWithId The explicit nullptr was redundant: the constructor already defaults the set to null. --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
9980c90cea
commit
0fe4fd0d8c
2 changed files with 36 additions and 1 deletions
|
|
@ -133,6 +133,10 @@ ExactCard CardDatabaseQuerier::getCard(const CardRef &cardRef) const
|
||||||
ExactCard CardDatabaseQuerier::guessCard(const CardRef &cardRef) const
|
ExactCard CardDatabaseQuerier::guessCard(const CardRef &cardRef) const
|
||||||
{
|
{
|
||||||
auto card = lookupCardByName(cardRef.name);
|
auto card = lookupCardByName(cardRef.name);
|
||||||
|
if (!card) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
auto printing =
|
auto printing =
|
||||||
cardRef.providerId.isEmpty() ? getPreferredPrinting(card) : findPrintingWithId(card, cardRef.providerId);
|
cardRef.providerId.isEmpty() ? getPreferredPrinting(card) : findPrintingWithId(card, cardRef.providerId);
|
||||||
|
|
||||||
|
|
@ -173,10 +177,14 @@ ExactCard CardDatabaseQuerier::getCardFromSameSet(const QString &cardName, const
|
||||||
*
|
*
|
||||||
* @param cardInfo The CardInfo to search
|
* @param cardInfo The CardInfo to search
|
||||||
* @param providerId The uuid to look for
|
* @param providerId The uuid to look for
|
||||||
* @return The PrintingInfo, or a default-constructed PrintingInfo if not found.
|
* @return The PrintingInfo, or a default-constructed PrintingInfo if not found or if cardInfo is null.
|
||||||
*/
|
*/
|
||||||
PrintingInfo CardDatabaseQuerier::findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId) const
|
PrintingInfo CardDatabaseQuerier::findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId) const
|
||||||
{
|
{
|
||||||
|
if (!cardInfo) {
|
||||||
|
return PrintingInfo();
|
||||||
|
}
|
||||||
|
|
||||||
for (const auto &printings : cardInfo->getSets()) {
|
for (const auto &printings : cardInfo->getSets()) {
|
||||||
for (const auto &printing : printings) {
|
for (const auto &printing : printings) {
|
||||||
if (printing.getUuid() == providerId) {
|
if (printing.getUuid() == providerId) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "libcockatrice/card/database/card_database.h"
|
#include "libcockatrice/card/database/card_database.h"
|
||||||
#include "libcockatrice/card/database/card_database_loader.h"
|
#include "libcockatrice/card/database/card_database_loader.h"
|
||||||
#include "libcockatrice/card/database/card_database_querier.h"
|
#include "libcockatrice/card/database/card_database_querier.h"
|
||||||
|
#include "libcockatrice/card/printing/exact_card.h"
|
||||||
#include "libcockatrice/card/set/card_set.h"
|
#include "libcockatrice/card/set/card_set.h"
|
||||||
#include "libcockatrice/card/set/card_set_list.h"
|
#include "libcockatrice/card/set/card_set_list.h"
|
||||||
#include "test_card_database_path_provider.h"
|
#include "test_card_database_path_provider.h"
|
||||||
|
|
@ -29,6 +30,7 @@
|
||||||
#include <libcockatrice/interfaces/interface_card_set_priority_controller.h>
|
#include <libcockatrice/interfaces/interface_card_set_priority_controller.h>
|
||||||
#include <libcockatrice/interfaces/noop_card_preference_provider.h>
|
#include <libcockatrice/interfaces/noop_card_preference_provider.h>
|
||||||
#include <libcockatrice/interfaces/noop_card_set_priority_controller.h>
|
#include <libcockatrice/interfaces/noop_card_set_priority_controller.h>
|
||||||
|
#include <libcockatrice/utility/card_ref.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
|
@ -60,6 +62,31 @@ TEST(CardDatabaseTest, LoadXml)
|
||||||
ASSERT_EQ(NotLoaded, db->getLoadStatus()) << "Incorrect status after clear";
|
ASSERT_EQ(NotLoaded, db->getLoadStatus()) << "Incorrect status after clear";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CardDatabaseTest, GuessCardWithProviderIdOnUnknownCardReturnsEmpty)
|
||||||
|
{
|
||||||
|
// An unknown name plus a providerId used to dereference the missing card
|
||||||
|
// inside findPrintingWithId(); the lookup must come back empty instead.
|
||||||
|
NoopCardPreferenceProvider prefs;
|
||||||
|
TestCardDatabasePathProvider pathProvider;
|
||||||
|
NoopCardSetPriorityController controller;
|
||||||
|
CardDatabase db(nullptr, &prefs, &pathProvider, &controller);
|
||||||
|
|
||||||
|
const ExactCard guessed = db.query()->guessCard({"Not a Card", "card_Not a Card_fixture"});
|
||||||
|
|
||||||
|
EXPECT_TRUE(guessed.isEmpty());
|
||||||
|
EXPECT_FALSE(guessed);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(CardDatabaseTest, FindPrintingWithIdOnNullCardInfoReturnsEmpty)
|
||||||
|
{
|
||||||
|
NoopCardPreferenceProvider prefs;
|
||||||
|
TestCardDatabasePathProvider pathProvider;
|
||||||
|
NoopCardSetPriorityController controller;
|
||||||
|
CardDatabase db(nullptr, &prefs, &pathProvider, &controller);
|
||||||
|
|
||||||
|
EXPECT_TRUE(db.query()->findPrintingWithId(nullptr, "card_Not a Card_fixture").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(CardDatabaseTest, Xml4LocalizedDataRoundTrip)
|
TEST(CardDatabaseTest, Xml4LocalizedDataRoundTrip)
|
||||||
{
|
{
|
||||||
NoopCardSetPriorityController controller;
|
NoopCardSetPriorityController controller;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue