From db923a9a2123a14ad52266700edfde1d172abf0f Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 8 Feb 2022 14:09:34 +0100 Subject: [PATCH 1/5] IO/config: declare some variables as constant --- src/IO/configuration_JSON.cpp | 2 +- src/IO/configuration_XML.cpp | 2 +- src/IO/configuration_config.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/IO/configuration_JSON.cpp b/src/IO/configuration_JSON.cpp index 7c798b1b11..707a231ee1 100644 --- a/src/IO/configuration_JSON.cpp +++ b/src/IO/configuration_JSON.cpp @@ -263,7 +263,7 @@ void writeConfiguration(const std::string &filename, const Config *rootConfig, b void writeNode(const Config *config, rapidjson::Value &rootJSONData, rapidjson::Document::AllocatorType &allocator) { // Write the options - for (auto &entry : config->getOptions()) { + for (const auto &entry : config->getOptions()) { const std::string &configKey = entry.first; const std::string &configValue = entry.second; diff --git a/src/IO/configuration_XML.cpp b/src/IO/configuration_XML.cpp index 189bd81172..fc22c1e124 100644 --- a/src/IO/configuration_XML.cpp +++ b/src/IO/configuration_XML.cpp @@ -84,7 +84,7 @@ void readNode(xmlNodePtr root, Config *config) void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string &encoding) { // Write the options - for (auto &entry : config->getOptions()) { + for (const auto &entry : config->getOptions()) { const std::string &key = entry.first; const std::string &value = entry.second; diff --git a/src/IO/configuration_config.cpp b/src/IO/configuration_config.cpp index 3533e3e8c1..4290913884 100644 --- a/src/IO/configuration_config.cpp +++ b/src/IO/configuration_config.cpp @@ -408,7 +408,7 @@ void Config::dump(std::ostream &out, int indentLevel) const out << std::endl; out << indent << "Options..." << std::endl; if (getOptionCount() > 0) { - for (auto &entry : getOptions()) { + for (const auto &entry : getOptions()) { out << indent << padding << entry.first << " = " << entry.second << std::endl; } } else { @@ -416,7 +416,7 @@ void Config::dump(std::ostream &out, int indentLevel) const } ++indentLevel; - for (auto &entry : getSections()) { + for (const auto &entry : getSections()) { out << std::endl; out << indent << padding << "::: Section " << entry.first << " :::" << std::endl; entry.second->dump(out, indentLevel); From 8e53942008b6d4631778d476dc073306afeb3063 Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 8 Feb 2022 14:10:33 +0100 Subject: [PATCH 2/5] IO/config: remove unneeded destructor definition --- src/IO/configuration_config.cpp | 7 ------- src/IO/configuration_config.hpp | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/IO/configuration_config.cpp b/src/IO/configuration_config.cpp index 4290913884..b9afe19257 100644 --- a/src/IO/configuration_config.cpp +++ b/src/IO/configuration_config.cpp @@ -78,13 +78,6 @@ Config & Config::operator=(Config other) return *this; } -/*! - Destructor. -*/ -Config::~Config() -{ -} - /*! Swap operator. diff --git a/src/IO/configuration_config.hpp b/src/IO/configuration_config.hpp index 18f5b0d72a..a6ef2ed60c 100644 --- a/src/IO/configuration_config.hpp +++ b/src/IO/configuration_config.hpp @@ -48,7 +48,7 @@ class Config Config & operator=(Config other); Config & operator=(Config &&other) = default; - virtual ~Config(); + virtual ~Config() = default; void swap(Config &other); From 890834f3b4cca8a08d4fbf6bb6f7234a709a6c8c Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 8 Feb 2022 16:29:55 +0100 Subject: [PATCH 3/5] IO/config: fix leaks when writing an XML configuration --- src/IO/configuration_XML.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/IO/configuration_XML.cpp b/src/IO/configuration_XML.cpp index fc22c1e124..55a6c97798 100644 --- a/src/IO/configuration_XML.cpp +++ b/src/IO/configuration_XML.cpp @@ -83,6 +83,8 @@ void readNode(xmlNodePtr root, Config *config) */ void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string &encoding) { + int status; + // Write the options for (const auto &entry : config->getOptions()) { const std::string &key = entry.first; @@ -91,6 +93,12 @@ void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string xmlChar *elementName = encodeString(key, encoding); xmlChar *elementText = encodeString(value, encoding); int status = xmlTextWriterWriteFormatElement(writer, BAD_CAST elementName, "%s", elementText); + if (elementText) { + xmlFree(elementText); + } + if (elementName) { + xmlFree(elementName); + } if (status < 0) { throw std::runtime_error("Error at xmlTextWriterWriteFormatElement"); } @@ -103,7 +111,10 @@ void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string // Start the section xmlChar *elementName = encodeString(key, encoding); - int status = xmlTextWriterStartElement(writer, BAD_CAST elementName); + status = xmlTextWriterStartElement(writer, BAD_CAST elementName); + if (elementName) { + xmlFree(elementName); + } if (status < 0) { throw std::runtime_error("Error at xmlTextWriterStartElement"); } @@ -250,6 +261,9 @@ void writeConfiguration(const std::string &filename, const std::string &rootname // Start the root element xmlChar *elementName = encodeString(rootname, DEFAULT_ENCODING); status = xmlTextWriterStartElement(writer, BAD_CAST elementName); + if (elementName) { + xmlFree(elementName); + } if (status < 0) { throw std::runtime_error("Error at xmlTextWriterStartElement"); } @@ -260,6 +274,9 @@ void writeConfiguration(const std::string &filename, const std::string &rootname xmlChar *versionAttr = encodeString(versionStream.str(), DEFAULT_ENCODING); status = xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST versionAttr); + if (versionAttr) { + xmlFree(versionAttr); + } if (status < 0) { throw std::runtime_error("Error at xmlTextWriterWriteAttribute"); } From 55f2519294e758a631747fc7331194e08e68d72b Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Wed, 9 Feb 2022 21:58:59 +0100 Subject: [PATCH 4/5] IO/config: cosmetic change --- src/IO/configuration_XML.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/IO/configuration_XML.cpp b/src/IO/configuration_XML.cpp index 55a6c97798..f5d461fd4b 100644 --- a/src/IO/configuration_XML.cpp +++ b/src/IO/configuration_XML.cpp @@ -205,8 +205,7 @@ void readConfiguration(const std::string &filename, const std::string &rootname, } // Check if the version is supported - const xmlChar *versionAttributeName = - reinterpret_cast("version"); + const xmlChar *versionAttributeName = reinterpret_cast("version"); if (checkVersion && xmlHasProp(rootElement, versionAttributeName)) { xmlChar *versionValue = xmlGetProp(rootElement, versionAttributeName); std::string versionString((char *) versionValue); From 74304ccb812261feccefedd0542f6be38c8e35db Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 23 Sep 2022 09:27:43 +0200 Subject: [PATCH 5/5] IO/config: return constant references whenever possible --- src/IO/configuration_config.cpp | 2 +- src/IO/configuration_config.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IO/configuration_config.cpp b/src/IO/configuration_config.cpp index b9afe19257..44a6cf3683 100644 --- a/src/IO/configuration_config.cpp +++ b/src/IO/configuration_config.cpp @@ -151,7 +151,7 @@ bool Config::hasOption(const std::string &key) const \param key is the name of the option \result The specified option. */ -std::string Config::get(const std::string &key) const +const std::string & Config::get(const std::string &key) const { return m_options->at(key); } diff --git a/src/IO/configuration_config.hpp b/src/IO/configuration_config.hpp index a6ef2ed60c..ea923f6b5c 100644 --- a/src/IO/configuration_config.hpp +++ b/src/IO/configuration_config.hpp @@ -58,7 +58,7 @@ class Config Options & getOptions(); const Options & getOptions() const; bool hasOption(const std::string &key) const; - std::string get(const std::string &key) const; + const std::string & get(const std::string &key) const; std::string get(const std::string &key, const std::string &fallback) const; void set(const std::string &key, const std::string &value); bool removeOption(const std::string &key);