Skip to content

Commit 3b2ed1c

Browse files
committed
Fix node iteration & return errors on invalid XML string
1 parent fc46a28 commit 3b2ed1c

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

Shared/XML/CXMLImpl.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,52 @@ void CXMLImpl::DeleteXML(CXMLFile* pFile)
4646

4747
CXMLNode* CXMLImpl::ParseString(const char* strXmlContent)
4848
{
49-
TiXmlElement *xmlRoot;
50-
TiXmlNode *xmlChild;
51-
CXMLNodeImpl *xmlRootNode;
52-
CXMLNode *xmlChildNode;
53-
49+
TiXmlElement *xmlDocumentRoot;
50+
CXMLNodeImpl *xmlBaseNode;
51+
CXMLNode *xmlRootNode;
52+
5453
TiXmlDocument* xmlDoc = new TiXmlDocument();
5554

5655
if (xmlDoc)
57-
{
58-
xmlDoc->Parse(strXmlContent, 0, TIXML_ENCODING_UTF8);
56+
{
57+
if (xmlDoc->Parse(strXmlContent, 0, TIXML_ENCODING_UTF8))
58+
{
59+
xmlDocumentRoot = xmlDoc->RootElement();
5960

60-
xmlRoot = xmlDoc->RootElement();
61+
xmlBaseNode = new CXMLNodeImpl(NULL, NULL, *xmlDocumentRoot);
6162

62-
xmlRootNode = new CXMLNodeImpl(NULL, NULL, *xmlRoot);
63+
xmlRootNode = CXMLImpl::BuildNode(xmlBaseNode, xmlDocumentRoot);
6364

64-
xmlChild = 0;
65-
while (xmlChild = xmlRoot->IterateChildren(xmlChild))
66-
{
67-
xmlChildNode = new CXMLNodeImpl(NULL, xmlRootNode, *xmlChild->ToElement());
65+
return xmlRootNode;
6866
}
69-
70-
return xmlRootNode;
67+
else
68+
return NULL;
7169
}
7270
else
7371
{
7472
return NULL;
7573
}
7674
}
7775

76+
CXMLNode* CXMLImpl::BuildNode(CXMLNodeImpl* xmlParent, TiXmlNode* xmlNode)
77+
{
78+
TiXmlNode *xmlChild;
79+
TiXmlElement *xmlChildElement;
80+
CXMLNodeImpl *xmlChildNode;
81+
82+
xmlChild = 0;
83+
while (xmlChild = xmlNode->IterateChildren(xmlChild))
84+
{
85+
xmlChildElement = xmlChild->ToElement();
86+
87+
xmlChildNode = new CXMLNodeImpl(NULL, xmlParent, *xmlChildElement);
88+
89+
CXMLImpl::BuildNode(xmlChildNode, xmlChildElement);
90+
}
91+
92+
return xmlParent;
93+
}
94+
7895
CXMLNode* CXMLImpl::CreateDummyNode()
7996
{
8097
CXMLNode* xmlNode = new CXMLNodeImpl(NULL, NULL, *new TiXmlElement("dummy_storage"));

Shared/XML/CXMLImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CXMLImpl : public CXML
2121

2222
CXMLFile* CreateXML(const char* szFilename, bool bUseIDs, bool bReadOnly);
2323
CXMLNode* ParseString(const char* strXmlContent);
24+
CXMLNode* BuildNode(CXMLNodeImpl* xmlParent, TiXmlNode* xmlNode);
2425
void DeleteXML(CXMLFile* pFile);
2526

2627
CXMLNode* CreateDummyNode();

Shared/mods/deathmatch/logic/luadefs/CLuaXMLDefs.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ int CLuaXMLDefs::xmlLoadString(lua_State* luaVM)
230230
if (pLuaMain)
231231
{
232232
CXMLNode* rootNode = pLuaMain->ParseString(strXmlContent);
233-
if (rootNode->IsValid())
233+
234+
if (rootNode && rootNode->IsValid())
234235
{
235236
lua_pushxmlnode(luaVM, rootNode);
236237
return 1;
237238
}
239+
else
240+
m_pScriptDebugging->LogCustom(luaVM, "Unable to load XML string");
238241
}
239242
}
240243
else

0 commit comments

Comments
 (0)