summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Tardon <dtardon@redhat.com>2017-04-02 15:57:25 +0200
committerDavid Tardon <dtardon@redhat.com>2017-04-02 16:11:25 +0200
commit27ab61071e7f88e8e1c840d4b8d1c97f19293d3f (patch)
tree41df175243fa57e81309ebb8ab933ae4a564169c
parent22356b9b04d0fe552d9d956698fe00c03a28b3c5 (diff)
avoid memory leaks on exception
Change-Id: I17075162b6e8919d28e80709b8e50833bb2601bc
-rw-r--r--src/lib/ABWParser.cpp189
-rw-r--r--src/lib/ABWXMLString.cpp34
-rw-r--r--src/lib/ABWXMLString.h38
-rw-r--r--src/lib/Makefile.am2
4 files changed, 124 insertions, 139 deletions
diff --git a/src/lib/ABWParser.cpp b/src/lib/ABWParser.cpp
index 4736b8a..5691eca 100644
--- a/src/lib/ABWParser.cpp
+++ b/src/lib/ABWParser.cpp
@@ -21,6 +21,7 @@
#include "ABWStylesCollector.h"
#include "libabw_internal.h"
#include "ABWXMLHelper.h"
+#include "ABWXMLString.h"
#include "ABWXMLTokenMap.h"
@@ -378,21 +379,16 @@ int libabw::ABWParser::getElementToken(xmlTextReaderPtr reader)
void libabw::ABWParser::readAbiword(xmlTextReaderPtr reader)
{
- xmlChar *const props = xmlTextReaderGetAttribute(reader, BAD_CAST("props"));
+ const ABWXMLString props = xmlTextReaderGetAttribute(reader, BAD_CAST("props"));
if (m_collector)
- m_collector->collectDocumentProperties(reinterpret_cast<const char *>(props));
- if (props)
- xmlFree(props);
+ m_collector->collectDocumentProperties(static_cast<const char *>(props));
}
void libabw::ABWParser::readM(xmlTextReaderPtr reader)
{
- xmlChar *const key = xmlTextReaderGetAttribute(reader, BAD_CAST("key"));
+ const ABWXMLString key = xmlTextReaderGetAttribute(reader, BAD_CAST("key"));
if (key)
- {
- m_state->m_currentMetadataKey = reinterpret_cast<const char *>(key);
- xmlFree(key);
- }
+ m_state->m_currentMetadataKey = static_cast<const char *>(key);
}
void libabw::ABWParser::readHistory(xmlTextReaderPtr reader)
@@ -467,37 +463,29 @@ void libabw::ABWParser::readIgnoredWords(xmlTextReaderPtr reader)
void libabw::ABWParser::readPageSize(xmlTextReaderPtr reader)
{
- xmlChar *width = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("width"));
- xmlChar *height = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("height"));
- xmlChar *units = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("units"));
- xmlChar *pageScale = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("page-scale"));
+ ABWXMLString width = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("width"));
+ ABWXMLString height = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("height"));
+ ABWXMLString units = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("units"));
+ ABWXMLString pageScale = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("page-scale"));
if (m_collector)
m_collector->collectPageSize((const char *)width, (const char *)height, (const char *)units, (const char *)pageScale);
- if (width)
- xmlFree(width);
- if (height)
- xmlFree(height);
- if (units)
- xmlFree(units);
- if (pageScale)
- xmlFree(pageScale);
}
void libabw::ABWParser::readSection(xmlTextReaderPtr reader)
{
- xmlChar *id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("id"));
- xmlChar *type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
- xmlChar *footer = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer"));
- xmlChar *footerLeft = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-even"));
- xmlChar *footerFirst = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-first"));
- xmlChar *footerLast = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-last"));
- xmlChar *header = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header"));
- xmlChar *headerLeft = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-even"));
- xmlChar *headerFirst = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-first"));
- xmlChar *headerLast = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-last"));
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
-
- if (!type || (xmlStrncmp(type, call_BAD_CAST_OnConst("header"), 6) && xmlStrncmp(type, call_BAD_CAST_OnConst("footer"), 6)))
+ ABWXMLString id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("id"));
+ ABWXMLString type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
+ ABWXMLString footer = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer"));
+ ABWXMLString footerLeft = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-even"));
+ ABWXMLString footerFirst = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-first"));
+ ABWXMLString footerLast = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footer-last"));
+ ABWXMLString header = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header"));
+ ABWXMLString headerLeft = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-even"));
+ ABWXMLString headerFirst = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-first"));
+ ABWXMLString headerLast = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("header-last"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+
+ if (!type || (xmlStrncmp(type.get(), call_BAD_CAST_OnConst("header"), 6) && xmlStrncmp(type.get(), call_BAD_CAST_OnConst("footer"), 6)))
{
if (m_collector)
m_collector->collectSectionProperties((const char *)footer, (const char *)footerLeft,
@@ -511,42 +499,18 @@ void libabw::ABWParser::readSection(xmlTextReaderPtr reader)
if (m_collector)
m_collector->collectHeaderFooter((const char *)id, (const char *)type);
}
-
- if (id)
- xmlFree(id);
- if (type)
- xmlFree(type);
- if (footer)
- xmlFree(footer);
- if (footerLeft)
- xmlFree(footerLeft);
- if (footerFirst)
- xmlFree(footerFirst);
- if (footerLast)
- xmlFree(footerLast);
- if (header)
- xmlFree(header);
- if (headerLeft)
- xmlFree(headerLeft);
- if (headerFirst)
- xmlFree(headerFirst);
- if (headerLast)
- xmlFree(headerLast);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readD(xmlTextReaderPtr reader)
{
- xmlChar *name = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("name"));
- xmlChar *mimeType = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("mime-type"));
+ ABWXMLString name = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("name"));
+ ABWXMLString mimeType = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("mime-type"));
- xmlChar *tmpBase64 = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("base64"));
+ ABWXMLString tmpBase64 = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("base64"));
bool base64(false);
if (tmpBase64)
{
findBool((const char *)tmpBase64, base64);
- xmlFree(tmpBase64);
}
int ret = 1;
@@ -584,19 +548,15 @@ void libabw::ABWParser::readD(xmlTextReaderPtr reader)
}
}
while ((XML_D != tokenId || XML_READER_TYPE_END_ELEMENT != tokenType) && 1 == ret);
- if (name)
- xmlFree(name);
- if (mimeType)
- xmlFree(mimeType);
}
void libabw::ABWParser::readS(xmlTextReaderPtr reader)
{
- xmlChar *type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
- xmlChar *name = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("name"));
- xmlChar *basedon = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("basedon"));
- xmlChar *followedby = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("followedby"));
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
+ ABWXMLString name = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("name"));
+ ABWXMLString basedon = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("basedon"));
+ ABWXMLString followedby = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("followedby"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
if (type)
{
if (m_collector)
@@ -611,135 +571,86 @@ void libabw::ABWParser::readS(xmlTextReaderPtr reader)
break;
}
}
- xmlFree(type);
}
- if (name)
- xmlFree(name);
- if (basedon)
- xmlFree(basedon);
- if (followedby)
- xmlFree(followedby);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readA(xmlTextReaderPtr reader)
{
- xmlChar *href = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("xlink:href"));
+ ABWXMLString href = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("xlink:href"));
if (m_collector)
m_collector->openLink((const char *)href);
- if (href)
- xmlFree(href);
}
void libabw::ABWParser::readP(xmlTextReaderPtr reader)
{
- xmlChar *level = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("level"));
- xmlChar *listid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("listid"));
- xmlChar *parentid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("listid"));
- xmlChar *style = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("style"));
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString level = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("level"));
+ ABWXMLString listid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("listid"));
+ ABWXMLString parentid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("listid"));
+ ABWXMLString style = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("style"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
if (m_collector)
m_collector->collectParagraphProperties((const char *)level, (const char *)listid, (const char *)parentid,
(const char *)style, (const char *)props);
- if (level)
- xmlFree(level);
- if (listid)
- xmlFree(listid);
- if (parentid)
- xmlFree(parentid);
- if (style)
- xmlFree(style);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readC(xmlTextReaderPtr reader)
{
- xmlChar *style = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("style"));
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString style = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("style"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
if (m_collector)
m_collector->collectCharacterProperties((const char *)style, (const char *)props);
- if (style)
- xmlFree(style);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readEndnote(xmlTextReaderPtr reader)
{
- xmlChar *id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("endnote-id"));
+ ABWXMLString id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("endnote-id"));
if (m_collector)
m_collector->openEndnote((const char *)id);
- if (id)
- xmlFree(id);
}
void libabw::ABWParser::readFoot(xmlTextReaderPtr reader)
{
- xmlChar *id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footnote-id"));
+ ABWXMLString id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("footnote-id"));
if (m_collector)
m_collector->openFoot((const char *)id);
- if (id)
- xmlFree(id);
}
void libabw::ABWParser::readTable(xmlTextReaderPtr reader)
{
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
if (m_collector)
m_collector->openTable((const char *)props);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readCell(xmlTextReaderPtr reader)
{
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
if (m_collector)
m_collector->openCell((const char *)props);
- if (props)
- xmlFree(props);
}
void libabw::ABWParser::readImage(xmlTextReaderPtr reader)
{
- xmlChar *props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
- xmlChar *dataid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("dataid"));
+ ABWXMLString props = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("props"));
+ ABWXMLString dataid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("dataid"));
if (m_collector)
m_collector->insertImage((const char *)dataid, (const char *)props);
- if (props)
- xmlFree(props);
- if (dataid)
- xmlFree(dataid);
}
void libabw::ABWParser::readL(xmlTextReaderPtr reader)
{
- xmlChar *id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("id"));
- xmlChar *listDecimal = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("list-decimal"));
+ ABWXMLString id = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("id"));
+ ABWXMLString listDecimal = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("list-decimal"));
if (!listDecimal)
listDecimal = xmlCharStrdup("NULL");
- xmlChar *listDelim = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("list-delim"));
- xmlChar *parentid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("parentid"));
- xmlChar *startValue = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("start-value"));
- xmlChar *type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
+ ABWXMLString listDelim = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("list-delim"));
+ ABWXMLString parentid = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("parentid"));
+ ABWXMLString startValue = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("start-value"));
+ ABWXMLString type = xmlTextReaderGetAttribute(reader, call_BAD_CAST_OnConst("type"));
if (m_collector)
m_collector->collectList((const char *)id, (const char *)listDecimal, (const char *)listDelim,
(const char *)parentid, (const char *)startValue, (const char *)type);
- if (id)
- xmlFree(id);
- if (listDecimal)
- xmlFree(listDecimal);
- if (listDelim)
- xmlFree(listDelim);
- if (parentid)
- xmlFree(parentid);
- if (startValue)
- xmlFree(startValue);
- if (type)
- xmlFree(type);
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
diff --git a/src/lib/ABWXMLString.cpp b/src/lib/ABWXMLString.cpp
new file mode 100644
index 0000000..00f9a43
--- /dev/null
+++ b/src/lib/ABWXMLString.cpp
@@ -0,0 +1,34 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * This file is part of the libabw project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "ABWXMLString.h"
+
+#include <libxml/xmlmemory.h>
+
+namespace libabw
+{
+
+ABWXMLString::ABWXMLString(xmlChar *xml)
+ : m_xml(xml, xmlFree)
+{
+}
+
+const xmlChar *ABWXMLString::get() const
+{
+ return m_xml.get();
+}
+
+ABWXMLString::operator const char *() const
+{
+ return reinterpret_cast<const char *>(m_xml.get());
+}
+
+}
+
+/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
diff --git a/src/lib/ABWXMLString.h b/src/lib/ABWXMLString.h
new file mode 100644
index 0000000..893e9a3
--- /dev/null
+++ b/src/lib/ABWXMLString.h
@@ -0,0 +1,38 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * This file is part of the libabw project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef __ABWXMLSTRING_H__
+#define __ABWXMLSTRING_H__
+
+#include <boost/shared_ptr.hpp>
+
+#include <libxml/xmlstring.h>
+
+namespace libabw
+{
+
+// An exception-safe wrapper around xmlChar *
+class ABWXMLString
+{
+public:
+ ABWXMLString(xmlChar *xml);
+
+ const xmlChar *get() const;
+
+ operator const char *() const;
+
+private:
+ boost::shared_ptr<xmlChar> m_xml;
+};
+
+}
+
+#endif /* __ABWXMLSTRING_H__ */
+
+/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 1fe3d54..daeb7d9 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -37,6 +37,7 @@ libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_SOURCES = \
ABWParser.cpp \
ABWStylesCollector.cpp \
ABWXMLHelper.cpp \
+ ABWXMLString.cpp \
ABWXMLTokenMap.cpp \
ABWZlibStream.cpp \
AbiDocument.cpp \
@@ -48,6 +49,7 @@ libabw_@ABW_MAJOR_VERSION@_@ABW_MINOR_VERSION@_la_SOURCES = \
ABWParser.h \
ABWStylesCollector.h \
ABWXMLHelper.h \
+ ABWXMLString.h \
ABWXMLTokenMap.h \
ABWZlibStream.h \
libabw_internal.h