From 551f3ce03e50615ecba1da96e8cb91e9ef50f06a Mon Sep 17 00:00:00 2001 From: Tristan Matthews <tristan.matthews@savoirfairelinux.com> Date: Fri, 17 Aug 2012 12:06:47 -0400 Subject: [PATCH] * #14569: yamlparser: guarantee that the file exists for the duration of yamlparser's lifetime Before it was possible that it was deleted in between the time it was checked and the time the YamlParser was created. --- daemon/src/config/yamlparser.cpp | 8 ++------ daemon/src/config/yamlparser.h | 10 ++-------- daemon/src/managerimpl.cpp | 18 ++++++------------ daemon/test/configurationtest.cpp | 5 ++++- 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/daemon/src/config/yamlparser.cpp b/daemon/src/config/yamlparser.cpp index 10a34bb0b5..b83e5a4f40 100644 --- a/daemon/src/config/yamlparser.cpp +++ b/daemon/src/config/yamlparser.cpp @@ -40,8 +40,7 @@ namespace Conf { -YamlParser::YamlParser(const char *file) : filename_(file) - , fd_(0) +YamlParser::YamlParser(FILE *fd) : fd_(fd) , parser_() , events_() , eventNumber_(0) @@ -58,7 +57,6 @@ YamlParser::YamlParser(const char *file) : filename_(file) , voiplinkNode_(NULL) , shortcutNode_(NULL) { - fd_ = fopen(filename_.c_str(), "rb"); if (!fd_) throw YamlParserException("Could not open file descriptor"); @@ -129,10 +127,8 @@ YamlParser::getShortcutNode() YamlParser::~YamlParser() { - if (fd_) { - fclose(fd_); + if (fd_) yaml_parser_delete(&parser_); - } for (int i = 0; i < eventNumber_; ++i) yaml_event_delete(&events_[i]); diff --git a/daemon/src/config/yamlparser.h b/daemon/src/config/yamlparser.h index 09d2959f8d..a94bcf601e 100644 --- a/daemon/src/config/yamlparser.h +++ b/daemon/src/config/yamlparser.h @@ -51,8 +51,7 @@ typedef std::vector<yaml_event_t> YamlEventVector; class YamlParserException : public std::runtime_error { public: - YamlParserException(const std::string& str="") : - std::runtime_error("YamlParserException occured: " + str) {} + YamlParserException(const char *err) : std::runtime_error(err) {} }; @@ -60,7 +59,7 @@ class YamlParser { public: - YamlParser(const char *file); + YamlParser(FILE *fd); ~YamlParser(); @@ -107,11 +106,6 @@ class YamlParser { void mainNativeDataMapping(MappingNode *map); - /** - * Configuration file name - */ - std::string filename_; - /** * Configuration file descriptor */ diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp index 41d68417d3..dd7c970978 100644 --- a/daemon/src/managerimpl.cpp +++ b/daemon/src/managerimpl.cpp @@ -103,26 +103,20 @@ void ManagerImpl::init(const std::string &config_file) DEBUG("Configuration file path: %s", path_.c_str()); try { - std::fstream testFileExistence(path_.c_str(), std::fstream::in); - bool fileExist = testFileExistence.good(); - testFileExistence.close(); + FILE *file = fopen(path_.c_str(), "rb"); - if(fileExist) { - Conf::YamlParser parser(path_.c_str()); + if (file) { + Conf::YamlParser parser(file); parser.serializeEvents(); parser.composeEvents(); parser.constructNativeData(); loadAccountMap(parser); - } - else { + fclose(file); + } else { WARN("Config file not found: creating default account map"); loadDefaultAccountMap(); } - } - catch (const Conf::YamlParserException &e) { - ERROR("%s", e.what()); - } - catch(std::fstream::failure &e) { + } catch (const Conf::YamlParserException &e) { ERROR("%s", e.what()); } diff --git a/daemon/test/configurationtest.cpp b/daemon/test/configurationtest.cpp index 59136e9117..fc05dcd4b1 100644 --- a/daemon/test/configurationtest.cpp +++ b/daemon/test/configurationtest.cpp @@ -91,10 +91,13 @@ void ConfigurationTest::testInitAudioDriver() void ConfigurationTest::testYamlParser() { try { - Conf::YamlParser parser("ymlParser.yml"); + FILE *file = fopen("ymlParser.yml", "rb"); + Conf::YamlParser parser(file); parser.serializeEvents(); parser.composeEvents(); parser.constructNativeData(); + if (file) + fclose(file); } catch (const Conf::YamlParserException &e) { ERROR("ConfigTree: %s", e.what()); } -- GitLab