Skip to content

Commit

Permalink
Fix memory corruption when vector being resized
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Jan 25, 2024
1 parent ec83629 commit e1454a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/karts/kart_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ void KartModel::loadInfo(const XMLNode &node)
{
if (const XMLNode *speed_weighted_objects_node = node.getNode("speed-weighted-objects"))
{
for (unsigned int i = 0 ;i < speed_weighted_objects_node->getNumNodes() ; i++)
unsigned speed_weighted_objects_size = speed_weighted_objects_node->getNumNodes();
m_speed_weighted_objects.resize(speed_weighted_objects_size);
for (unsigned int i = 0 ;i < speed_weighted_objects_size; i++)
{
loadSpeedWeightedInfo(speed_weighted_objects_node->getNode(i));
loadSpeedWeightedInfo(speed_weighted_objects_node->getNode(i), i);
}
}
if (const XMLNode* headlights_node = node.getNode("headlights"))
Expand Down Expand Up @@ -797,9 +799,9 @@ void KartModel::loadNitroEmitterInfo(const XMLNode &node,
} // loadNitroEmitterInfo

// ----------------------------------------------------------------------------

/** Loads a single speed weighted node. */
void KartModel::loadSpeedWeightedInfo(const XMLNode* speed_weighted_node)
void KartModel::loadSpeedWeightedInfo(const XMLNode* speed_weighted_node,
int index)
{
SpeedWeightedObject obj;
if (speed_weighted_node->getName() == "object")
Expand All @@ -824,7 +826,7 @@ void KartModel::loadSpeedWeightedInfo(const XMLNode* speed_weighted_node)
}
if (!obj.m_name.empty())
{
m_speed_weighted_objects.push_back(obj);
m_speed_weighted_objects[index] = obj;
float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f;
Expand All @@ -835,7 +837,7 @@ void KartModel::loadSpeedWeightedInfo(const XMLNode* speed_weighted_node)
speed_weighted_node->get("animated-by-step", &step);
if (dx != 0.0f || dy != 0.0f)
{
m_speed_weighted_objects.back().m_properties.m_moving_texture =
m_speed_weighted_objects[index].m_properties.m_moving_texture =
new MovingTexture(dx, dy, dt, step);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/karts/kart_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class KartModel : public scene::IAnimationEndCallBack, public NoCopy
void loadNitroEmitterInfo(const XMLNode &node,
const std::string &emitter_name, int index);

void loadSpeedWeightedInfo(const XMLNode* speed_weighted_node);
void loadSpeedWeightedInfo(const XMLNode* speed_weighted_node, int index);

void loadHeadlights(const XMLNode &node);

Expand Down

0 comments on commit e1454a0

Please sign in to comment.