From e1454a007e7db1c9b22897213cd904f4ec95ff06 Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 25 Jan 2024 10:51:10 +0800 Subject: [PATCH] Fix memory corruption when vector being resized --- src/karts/kart_model.cpp | 14 ++++++++------ src/karts/kart_model.hpp | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/karts/kart_model.cpp b/src/karts/kart_model.cpp index d9c749a0a..933cd13a1 100644 --- a/src/karts/kart_model.cpp +++ b/src/karts/kart_model.cpp @@ -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")) @@ -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") @@ -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; @@ -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); } } diff --git a/src/karts/kart_model.hpp b/src/karts/kart_model.hpp index 6e9c19cbd..cc38b40f1 100644 --- a/src/karts/kart_model.hpp +++ b/src/karts/kart_model.hpp @@ -298,7 +298,7 @@ private: 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);