* #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.
This commit is contained in:
Tristan Matthews
2012-08-17 12:06:47 -04:00
parent 94cc6fdb16
commit 551f3ce03e
4 changed files with 14 additions and 27 deletions

View File

@ -40,8 +40,7 @@
namespace Conf { namespace Conf {
YamlParser::YamlParser(const char *file) : filename_(file) YamlParser::YamlParser(FILE *fd) : fd_(fd)
, fd_(0)
, parser_() , parser_()
, events_() , events_()
, eventNumber_(0) , eventNumber_(0)
@ -58,7 +57,6 @@ YamlParser::YamlParser(const char *file) : filename_(file)
, voiplinkNode_(NULL) , voiplinkNode_(NULL)
, shortcutNode_(NULL) , shortcutNode_(NULL)
{ {
fd_ = fopen(filename_.c_str(), "rb");
if (!fd_) if (!fd_)
throw YamlParserException("Could not open file descriptor"); throw YamlParserException("Could not open file descriptor");
@ -129,10 +127,8 @@ YamlParser::getShortcutNode()
YamlParser::~YamlParser() YamlParser::~YamlParser()
{ {
if (fd_) { if (fd_)
fclose(fd_);
yaml_parser_delete(&parser_); yaml_parser_delete(&parser_);
}
for (int i = 0; i < eventNumber_; ++i) for (int i = 0; i < eventNumber_; ++i)
yaml_event_delete(&events_[i]); yaml_event_delete(&events_[i]);

View File

@ -51,8 +51,7 @@ typedef std::vector<yaml_event_t> YamlEventVector;
class YamlParserException : public std::runtime_error { class YamlParserException : public std::runtime_error {
public: public:
YamlParserException(const std::string& str="") : YamlParserException(const char *err) : std::runtime_error(err) {}
std::runtime_error("YamlParserException occured: " + str) {}
}; };
@ -60,7 +59,7 @@ class YamlParser {
public: public:
YamlParser(const char *file); YamlParser(FILE *fd);
~YamlParser(); ~YamlParser();
@ -107,11 +106,6 @@ class YamlParser {
void mainNativeDataMapping(MappingNode *map); void mainNativeDataMapping(MappingNode *map);
/**
* Configuration file name
*/
std::string filename_;
/** /**
* Configuration file descriptor * Configuration file descriptor
*/ */

View File

@ -103,26 +103,20 @@ void ManagerImpl::init(const std::string &config_file)
DEBUG("Configuration file path: %s", path_.c_str()); DEBUG("Configuration file path: %s", path_.c_str());
try { try {
std::fstream testFileExistence(path_.c_str(), std::fstream::in); FILE *file = fopen(path_.c_str(), "rb");
bool fileExist = testFileExistence.good();
testFileExistence.close();
if(fileExist) { if (file) {
Conf::YamlParser parser(path_.c_str()); Conf::YamlParser parser(file);
parser.serializeEvents(); parser.serializeEvents();
parser.composeEvents(); parser.composeEvents();
parser.constructNativeData(); parser.constructNativeData();
loadAccountMap(parser); loadAccountMap(parser);
} fclose(file);
else { } else {
WARN("Config file not found: creating default account map"); WARN("Config file not found: creating default account map");
loadDefaultAccountMap(); loadDefaultAccountMap();
} }
} } catch (const Conf::YamlParserException &e) {
catch (const Conf::YamlParserException &e) {
ERROR("%s", e.what());
}
catch(std::fstream::failure &e) {
ERROR("%s", e.what()); ERROR("%s", e.what());
} }

View File

@ -91,10 +91,13 @@ void ConfigurationTest::testInitAudioDriver()
void ConfigurationTest::testYamlParser() void ConfigurationTest::testYamlParser()
{ {
try { try {
Conf::YamlParser parser("ymlParser.yml"); FILE *file = fopen("ymlParser.yml", "rb");
Conf::YamlParser parser(file);
parser.serializeEvents(); parser.serializeEvents();
parser.composeEvents(); parser.composeEvents();
parser.constructNativeData(); parser.constructNativeData();
if (file)
fclose(file);
} catch (const Conf::YamlParserException &e) { } catch (const Conf::YamlParserException &e) {
ERROR("ConfigTree: %s", e.what()); ERROR("ConfigTree: %s", e.what());
} }