Skip to content

Commit 832ce62

Browse files
Fix raw memory leak in XML Error copy-assignment
1 parent 00902aa commit 832ce62

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/aws-cpp-sdk-core/include/aws/core/utils/xml/XmlSerializer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ namespace Aws
142142
//we do not own these.... I just had to change it from ref because the compiler was
143143
//confused about which assignment operator to call. Do not... I repeat... do not delete
144144
//these pointers in your destructor.
145-
Aws::External::tinyxml2::XMLNode* m_node;
146-
const XmlDocument* m_doc;
145+
Aws::External::tinyxml2::XMLNode* m_node = nullptr;
146+
const XmlDocument* m_doc = nullptr;
147147

148148
friend class XmlDocument;
149149
};
@@ -200,7 +200,7 @@ namespace Aws
200200
private:
201201
void InitDoc();
202202

203-
Aws::External::tinyxml2::XMLDocument* m_doc;
203+
Aws::External::tinyxml2::XMLDocument* m_doc = nullptr;
204204

205205
friend class XmlNode;
206206

src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,22 @@ void XmlNode::SetText(const Aws::String& textValue)
134134
{
135135
if (m_node != nullptr)
136136
{
137+
assert(m_doc && m_doc->m_doc == m_node->GetDocument());
137138
Aws::External::tinyxml2::XMLText* text = m_doc->m_doc->NewText(textValue.c_str());
138139
m_node->InsertEndChild(text);
139140
}
140141
}
141142

142143
XmlNode XmlNode::CreateChildElement(const Aws::String& name)
143144
{
145+
assert(m_doc && m_doc->m_doc == m_node->GetDocument());
144146
Aws::External::tinyxml2::XMLElement* element = m_doc->m_doc->NewElement(name.c_str());
145147
return XmlNode(m_node->InsertEndChild(element), *m_doc);
146148
}
147149

148150
XmlNode XmlNode::CreateSiblingElement(const Aws::String& name)
149151
{
152+
assert(m_doc && m_doc->m_doc == m_node->GetDocument());
150153
Aws::External::tinyxml2::XMLElement* element = m_doc->m_doc->NewElement(name.c_str());
151154
return XmlNode(m_node->Parent()->InsertEndChild(element), *m_doc);
152155
}
@@ -193,6 +196,7 @@ XmlDocument& XmlDocument::operator=(const XmlDocument& other)
193196
if (m_doc != nullptr)
194197
{
195198
m_doc->Clear();
199+
Aws::Delete(m_doc);
196200
m_doc = nullptr;
197201
}
198202
}
@@ -228,11 +232,13 @@ XmlDocument::~XmlDocument()
228232
if (m_doc)
229233
{
230234
Aws::Delete(m_doc);
235+
m_doc = nullptr;
231236
}
232237
}
233238

234239
void XmlDocument::InitDoc()
235240
{
241+
assert(!m_doc);
236242
m_doc = Aws::New<Aws::External::tinyxml2::XMLDocument>(XML_SERIALIZER_ALLOCATION_TAG, true, Aws::External::tinyxml2::Whitespace::PRESERVE_WHITESPACE);
237243
}
238244

tests/aws-cpp-sdk-core-tests/aws/client/AWSErrorMashallerTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ TEST_F(AWSErrorMarshallerTest, TestXmlErrorPayload)
183183
ASSERT_EQ("", error.GetMessage());
184184
ASSERT_EQ("", error.GetRequestId());
185185
ASSERT_TRUE(error.ShouldRetry());
186+
187+
AWSError<CoreErrors> emptyError;
188+
error = emptyError; // ASAN must not complain.
186189
}
187190

188191
TEST_F(AWSErrorMarshallerTest, TestCombinationsOfJsonErrorPayload)

0 commit comments

Comments
 (0)