From 104dcb745de20ba6f4b4ab73a53841c6d52c715f Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 28 Jul 2016 22:45:37 -0400 Subject: Get rid of undefined behavior in configuration blocks by creating them as necessary, add templated set function --- src/config.cpp | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/config.cpp b/src/config.cpp index ad73f8e59..b3de2864d 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -38,47 +38,37 @@ const Anope::string &Block::GetName() const int Block::CountBlock(const Anope::string &bname) { - if (!this) - return 0; - return blocks.count(bname); } -Block* Block::GetBlock(const Anope::string &bname, int num) +Block* Block::GetBlock(const Anope::string &bname) { - if (!this) - return NULL; + auto it = blocks.find(bname); - std::pair it = blocks.equal_range(bname); + if (it != blocks.end()) + return &it->second; - for (int i = 0; it.first != it.second; ++it.first, ++i) - if (i == num) - return &it.first->second; - return NULL; + blocks.emplace(bname, bname); + return GetBlock(bname); } -bool Block::Set(const Anope::string &tag, const Anope::string &value) +Block* Block::GetBlock(const Anope::string &bname, int num) { - if (!this) - return false; + std::pair it = blocks.equal_range(bname); - items[tag] = value; - return true; + for (int i = 0; it.first != it.second; ++it.first, ++i) + if (i == num) + return &it.first->second; + return nullptr; } const Block::item_map* Block::GetItems() const { - if (this) - return &items; - else - return NULL; + return &items; } template<> Anope::string Block::Get(const Anope::string &tag, const Anope::string& def) const { - if (!this) - return def; - Anope::map::const_iterator it = items.find(tag); if (it != items.end()) return it->second; @@ -118,6 +108,11 @@ template<> unsigned int Block::Get(const Anope::string &tag, const Anope::string return l; } +template<> void Block::Set(const Anope::string &tag, const Anope::string &value) +{ + items[tag] = value; +} + static void ValidateNotEmpty(const Anope::string &block, const Anope::string &name, const Anope::string &value) { if (value.empty()) -- cgit