Merge pull request #3393 from megamage/master

Fix possible total price overflow when buying items. Fix #3137.
This commit is contained in:
Shocker
2011-10-07 09:54:37 -07:00
+16 -7
View File
@@ -20515,16 +20515,25 @@ bool Player::BuyItemFromVendorSlot(uint64 vendorguid, uint32 vendorslot, uint32
} }
} }
uint32 price = crItem->IsGoldRequired(pProto) ? pProto->BuyPrice * count : 0; uint32 price = 0;
if(crItem->IsGoldRequired(pProto) && pProto->BuyPrice > 0) //Assume price cannot be negative (do not know why it is int32)
{
uint32 maxCount = MAX_MONEY_AMOUNT / pProto->BuyPrice;
if((uint32)count > maxCount)
{
sLog->outError("Player %s tried to buy %u item id %u, causing overflow", GetName(), (uint32)count, pProto->ItemId);
count = (uint8)maxCount;
}
price = pProto->BuyPrice * count; //it should not exceed MAX_MONEY_AMOUNT
// reputation discount // reputation discount
if (price)
price = uint32(floor(price * GetReputationPriceDiscount(creature))); price = uint32(floor(price * GetReputationPriceDiscount(creature)));
if (!HasEnoughMoney(price)) if (!HasEnoughMoney(price))
{ {
SendBuyError(BUY_ERR_NOT_ENOUGHT_MONEY, creature, item, 0); SendBuyError(BUY_ERR_NOT_ENOUGHT_MONEY, creature, item, 0);
return false; return false;
}
} }
if ((bag == NULL_BAG && slot == NULL_SLOT) || IsInventoryPos(bag, slot)) if ((bag == NULL_BAG && slot == NULL_SLOT) || IsInventoryPos(bag, slot))