[DeckList] Extract card parsing from zone XML reader (#7319)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-09-19 10:35:03 +02:00 committed by GitHub
parent faffb5a837
commit 1a6d9d7749
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 8 deletions

View file

@ -151,24 +151,29 @@ bool InnerDecklistNode::compareName(AbstractDecklistNode *other) const
}
}
int InnerDecklistNode::readCardElement(QXmlStreamReader *xml, int remainingBudget)
{
const int amount = qMin(xml->attributes().value("number").toString().toInt(), remainingBudget);
new DecklistCardNode(xml->attributes().value("name").toString(), amount, this, -1,
xml->attributes().value("setShortName").toString(),
xml->attributes().value("collectorNumber").toString(),
xml->attributes().value("uuid").toString());
return amount;
}
int InnerDecklistNode::readElement(QXmlStreamReader *xml, int limit)
{
int totalCards = 0;
while (!xml->atEnd()) {
xml->readNext();
const QString childName = xml->name().toString();
const int remainingBudget = limit - totalCards;
if (xml->isStartElement()) {
if (childName == "zone") {
auto *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
totalCards += newZone->readElement(xml, limit - totalCards);
totalCards += newZone->readElement(xml, remainingBudget);
} else if (childName == "card") {
int amount = xml->attributes().value("number").toString().toInt();
amount = qMin(amount, limit - totalCards);
new DecklistCardNode(xml->attributes().value("name").toString(), amount, this, -1,
xml->attributes().value("setShortName").toString(),
xml->attributes().value("collectorNumber").toString(),
xml->attributes().value("uuid").toString());
totalCards += amount;
totalCards += readCardElement(xml, remainingBudget);
}
} else if (xml->isEndElement() && (childName == "zone")) {
return totalCards;

View file

@ -236,6 +236,18 @@ public:
* @param xml Writer to append elements to.
*/
void writeElement(QXmlStreamWriter *xml) override;
private:
/**
* @brief Reads a single `card` element and appends it to this node.
*
* The card's quantity is capped at @p remainingBudget so a malicious or
* oversized deck file cannot push the total card count past the deck size
* limit.
*
* @return The amount of cards actually added.
*/
int readCardElement(QXmlStreamReader *xml, int remainingBudget);
};
#endif // COCKATRICE_INNER_DECK_LIST_NODE_H