From eaa50691fc39add8f5188820ae79477858c27c06 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Tue, 26 Jan 2021 16:08:34 +0100
Subject: [PATCH] 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
---
include/components/config_parser.hpp | 5 +++++
tests/unit_tests/components/config_parser.cpp | 4 ++--
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/include/components/config_parser.hpp b/include/components/config_parser.hpp
index 68f343a8..2fc38a19 100644
--- a/include/components/config_parser.hpp
+++ b/include/components/config_parser.hpp
@@ -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
diff --git a/tests/unit_tests/components/config_parser.cpp b/tests/unit_tests/components/config_parser.cpp
index 5aa7c43e..921815bb 100644
--- a/tests/unit_tests/components/config_parser.cpp
+++ b/tests/unit_tests/components/config_parser.cpp
@@ -33,8 +33,8 @@ class TestableConfigParser : public config_parser {
*/
class ConfigParser : public ::testing::Test {
protected:
- unique_ptr parser =
- make_unique(logger(loglevel::NONE), "/dev/zero", "TEST");
+ const logger l = logger(loglevel::NONE);
+ unique_ptr parser = make_unique(l, "/dev/zero", "TEST");
};
// ParseLineTest {{{