fix(config_parser): UB in logger

Because we passed a temporary as the logger, it gets destroyed after the
config_parser constructor returns and when the logger is called in
config_parser it operates on a dangling reference.

Ref: https://stackoverflow.com/q/35770357/5363071
This commit is contained in:
patrick96 2021-01-26 16:08:34 +01:00 committed by Patrick Ziegler
parent 04344aa0e7
commit eaa50691fc
2 changed files with 7 additions and 2 deletions

View File

@ -88,6 +88,11 @@ struct line_t {
class config_parser {
public:
config_parser(const logger& logger, string&& file, string&& bar);
/**
* This prevents passing a temporary logger to the constructor because that would be UB, as the temporary would be
* destroyed once the constructor returns.
*/
config_parser(logger&& logger, string&& file, string&& bar) = delete;
/**
* \brief Performs the parsing of the main config file m_file

View File

@ -33,8 +33,8 @@ class TestableConfigParser : public config_parser {
*/
class ConfigParser : public ::testing::Test {
protected:
unique_ptr<TestableConfigParser> parser =
make_unique<TestableConfigParser>(logger(loglevel::NONE), "/dev/zero", "TEST");
const logger l = logger(loglevel::NONE);
unique_ptr<TestableConfigParser> parser = make_unique<TestableConfigParser>(l, "/dev/zero", "TEST");
};
// ParseLineTest {{{