1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-15 01:24:45 +02:00

Fix memory corruption during loading artifacts from mods

This commit is contained in:
Vadim Markovtsev
2016-10-22 10:00:55 +02:00
parent feaccead36
commit baa7a84db3
8 changed files with 37 additions and 28 deletions

View File

@ -439,13 +439,13 @@ void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node)
{ {
for (auto b : node["growing"]["bonusesPerLevel"].Vector()) for (auto b : node["growing"]["bonusesPerLevel"].Vector())
{ {
auto bonus = JsonUtils::parseBonus (b["bonus"]); art->bonusesPerLevel.push_back(std::pair <ui16, Bonus>(b["level"].Float(), Bonus()));
art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *bonus)); JsonUtils::parseBonus(b["bonus"], &art->bonusesPerLevel.back().second);
} }
for (auto b : node["growing"]["thresholdBonuses"].Vector()) for (auto b : node["growing"]["thresholdBonuses"].Vector())
{ {
auto bonus = JsonUtils::parseBonus (b["bonus"]); art->thresholdBonuses.push_back(std::pair <ui16, Bonus>(b["level"].Float(), Bonus()));
art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *bonus)); JsonUtils::parseBonus(b["bonus"], &art->thresholdBonuses.back().second);
} }
} }

View File

@ -54,8 +54,10 @@ void CIdentifierStorage::checkIdentifier(std::string & ID)
} }
} }
CIdentifierStorage::ObjectCallback::ObjectCallback(std::string localScope, std::string remoteScope, std::string type, CIdentifierStorage::ObjectCallback::ObjectCallback(
std::string name, const std::function<void(si32)> & callback, bool optional): std::string localScope, std::string remoteScope, std::string type,
std::string name, const std::function<void(si32)> & callback,
bool optional):
localScope(localScope), localScope(localScope),
remoteScope(remoteScope), remoteScope(remoteScope),
type(type), type(type),

View File

@ -41,7 +41,10 @@ class CIdentifierStorage
std::function<void(si32)> callback; std::function<void(si32)> callback;
bool optional; bool optional;
ObjectCallback(std::string localScope, std::string remoteScope, std::string type, std::string name, const std::function<void(si32)> & callback, bool optional); ObjectCallback(std::string localScope, std::string remoteScope,
std::string type, std::string name,
const std::function<void(si32)> & callback,
bool optional);
}; };
struct ObjectData // entry created on ID registration struct ObjectData // entry created on ID registration

View File

@ -1110,10 +1110,6 @@ Bonus::Bonus()
sid = 0; sid = 0;
} }
Bonus::~Bonus()
{
}
std::shared_ptr<Bonus> Bonus::addPropagator(TPropagatorPtr Propagator) std::shared_ptr<Bonus> Bonus::addPropagator(TPropagatorPtr Propagator)
{ {
propagator = Propagator; propagator = Propagator;

View File

@ -313,7 +313,6 @@ struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>
Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1); Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, ValueType ValType = ADDITIVE_VALUE); Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, ValueType ValType = ADDITIVE_VALUE);
Bonus(); Bonus();
~Bonus();
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {

View File

@ -420,8 +420,16 @@ void JsonUtils::resolveIdentifier (const JsonNode &node, si32 &var)
std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability) std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
{ {
auto b = std::make_shared<Bonus>(); auto b = std::make_shared<Bonus>();
if (!parseBonus(ability, b.get()))
{
return nullptr;
}
return b;
}
bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
{
const JsonNode *value; const JsonNode *value;
std::string type = ability["type"].String(); std::string type = ability["type"].String();
@ -429,7 +437,7 @@ std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
if (it == bonusNameMap.end()) if (it == bonusNameMap.end())
{ {
logGlobal->errorStream() << "Error: invalid ability type " << type; logGlobal->errorStream() << "Error: invalid ability type " << type;
return b; return false;
} }
b->type = it->second; b->type = it->second;
@ -542,7 +550,7 @@ std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
if (!value->isNull()) if (!value->isNull())
b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type "); b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
return b; return true;
} }
//returns first Key with value equal to given one //returns first Key with value equal to given one

View File

@ -131,7 +131,8 @@ namespace JsonUtils
/// ///
DLL_LINKAGE std::shared_ptr<Bonus> parseBonus(const JsonVector &ability_vec); DLL_LINKAGE std::shared_ptr<Bonus> parseBonus(const JsonVector &ability_vec);
DLL_LINKAGE std::shared_ptr<Bonus> parseBonus (const JsonNode &bonus); DLL_LINKAGE std::shared_ptr<Bonus> parseBonus(const JsonNode &ability);
DLL_LINKAGE bool parseBonus(const JsonNode &ability, Bonus *placement);
DLL_LINKAGE void unparseBonus (JsonNode &node, const std::shared_ptr<Bonus>& bonus); DLL_LINKAGE void unparseBonus (JsonNode &node, const std::shared_ptr<Bonus>& bonus);
DLL_LINKAGE void resolveIdentifier(si32 &var, const JsonNode &node, std::string name); DLL_LINKAGE void resolveIdentifier(si32 &var, const JsonNode &node, std::string name);
DLL_LINKAGE void resolveIdentifier(const JsonNode &node, si32 &var); DLL_LINKAGE void resolveIdentifier(const JsonNode &node, si32 &var);