Skip to content

Commit f5f767c

Browse files
authored
Fix use of uninitialized variable in PowerupManager (#5459)
Also avoid a potenial null pointer dereference Remove two unused includes Minor cleanups
1 parent 1ae2518 commit f5f767c

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/items/powerup_manager.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1818

1919
#include "items/powerup_manager.hpp"
20-
21-
#include <cinttypes>
22-
#include <stdexcept>
23-
2420
#include "config/stk_config.hpp"
2521
#include "graphics/irr_driver.hpp"
2622
#include "graphics/sp/sp_base.hpp"
@@ -41,7 +37,7 @@
4137

4238
#include <IMesh.h>
4339

44-
PowerupManager* powerup_manager=0;
40+
PowerupManager* powerup_manager = nullptr;
4541

4642
//-----------------------------------------------------------------------------
4743
/** The constructor initialises everything to zero. */
@@ -50,8 +46,8 @@ PowerupManager::PowerupManager()
5046
m_random_seed.store(0);
5147
for(int i=0; i<POWERUP_MAX; i++)
5248
{
53-
m_all_meshes[i] = NULL;
54-
m_all_icons[i] = (Material*)NULL;
49+
m_all_meshes[i] = nullptr;
50+
m_all_icons[i] = (Material*)nullptr;
5551
}
5652
} // PowerupManager
5753

@@ -94,7 +90,7 @@ void PowerupManager::unloadPowerups()
9490

9591
//FIXME: I'm not sure if this is OK or if I need to ->drop(),
9692
// or delete them, or...
97-
m_all_icons[i] = (Material*)NULL;
93+
m_all_icons[i] = (Material*)nullptr;
9894
}
9995
} // removeTextures
10096

@@ -107,7 +103,7 @@ PowerupManager::PowerupType
107103
PowerupManager::getPowerupType(const std::string &name) const
108104
{
109105
// Must match the order of PowerupType in powerup_manager.hpp!!
110-
static std::string powerup_names[] = {
106+
static const std::string powerup_names[] = {
111107
"", /* Nothing */
112108
"bubblegum", "cake", "bowling", "zipper", "plunger", "switch",
113109
"swatter", "rubber-ball", "parachute", "anchor"
@@ -185,6 +181,7 @@ void PowerupManager::loadWeights(const XMLNode *powerup_node,
185181
Log::fatal("PowerupManager",
186182
"Cannot find node '%s' in powerup.xml file.",
187183
class_name.c_str());
184+
return;
188185
}
189186

190187
for (unsigned int i = 0; i < node->getNumNodes(); i++)
@@ -232,12 +229,19 @@ void PowerupManager::WeightsData::readData(int num_karts, const XMLNode *node)
232229

233230
// Keep a reference for shorter access to the list
234231
std::vector<int> &l = m_weights_for_section.back();
235-
for(unsigned int i=0; i<l_string.size(); i++)
232+
for(unsigned int j=0; j < l_string.size(); j++)
236233
{
237-
if(l_string[i]=="") continue;
234+
if(l_string[j].empty()) continue;
238235
int n;
239-
StringUtils::fromString(l_string[i], n);
240-
l.push_back(n);
236+
if (StringUtils::fromString(l_string[j], n))
237+
{
238+
l.push_back(n);
239+
} else
240+
{
241+
Log::warn("PowerupManager",
242+
"Invalid integer value '%s' in powerup.xml for '%s'",
243+
l_string[j].c_str(), node->getName().c_str());
244+
}
241245
}
242246
// Make sure we have the right number of entries
243247
if (l.size() < 2 * (int)POWERUP_LAST)
@@ -472,8 +476,8 @@ void PowerupManager::loadPowerup(PowerupType type, const XMLNode &node)
472476
/*strip_path*/ false);
473477

474478

475-
assert(m_all_icons[type] != NULL);
476-
assert(m_all_icons[type]->getTexture() != NULL);
479+
assert(m_all_icons[type] != nullptr);
480+
assert(m_all_icons[type]->getTexture() != nullptr);
477481

478482
std::string model("");
479483
node.get("model", &model);

0 commit comments

Comments
 (0)