From 356807a4c1183304be11f0269cf6d61e2e5dba75 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Thu, 24 Nov 2011 05:59:54 +0800 Subject: Prevented circular include in sprite definitions When including a file that already is included the client won't try to load it again and end in an endless loop but stops parsing and logs and warning. Reviewed-by: bjorn. Conflicts: src/resources/spritedef.cpp --- src/resources/spritedef.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/resources') diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index 6b856c5d3..082e528e7 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -39,6 +39,7 @@ #include "debug.h" +std::set processedFiles; SpriteReference *SpriteReference::Empty = nullptr; Action *SpriteDef::getAction(std::string action, unsigned num) const @@ -85,6 +86,9 @@ SpriteDef *SpriteDef::load(const std::string &animationFile, int variant) if (pos != std::string::npos) palettes = animationFile.substr(pos + 1); + processedFiles.clear(); + processedFiles.insert(animationFile); + XML::Document doc(animationFile.substr(0, pos)); xmlNodePtr rootNode = doc.rootNode(); @@ -350,14 +354,21 @@ void SpriteDef::loadAnimation(xmlNodePtr animationNode, void SpriteDef::includeSprite(xmlNodePtr includeNode) { - // TODO: Perform circular dependency check, since it's easy to crash the - // client this way. - const std::string filename = XML::getProperty(includeNode, "file", ""); + std::string filename = XML::getProperty(includeNode, "file", ""); if (filename.empty()) return; + filename = paths.getStringValue("sprites") + filename; + + if (processedFiles.find(filename) != processedFiles.end()) + { + logger->log("Error, Tried to include %s which already is included.", + filename.c_str()); + return; + } + processedFiles.insert(filename); - XML::Document doc(paths.getStringValue("sprites") + filename); + XML::Document doc(filename); xmlNodePtr rootNode = doc.rootNode(); if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) -- cgit v1.2.3-70-g09d2