From dd115d2e83d609f5a6b2ad3a786f835e1ca3569a Mon Sep 17 00:00:00 2001 From: Charlie Vigue Date: Fri, 29 Mar 2024 03:13:00 +0000 Subject: [PATCH] Fix RouteBase string formatting bug When correcting conversion issues in RouteBase a to_string bug was introduced which caused some characters to be escaped when inserted to the string, for example a prefix_len of 0 would render as "\0" rather than inserting '0'. The std::ios::binary flag does not seem to prevent this for std::ostringstream so I have cast the data member up to uint16_t which should be safe, and solves the issue. Added a unit test to demonstrate the issue. Old code output was "0.0.0.0/\0", now outputs "0.0.0.0/0" as expected. Signed-off-by: Charlie Vigue --- openvpn/tun/builder/capture.hpp | 2 +- test/unittests/CMakeLists.txt | 1 + test/unittests/test_tun_builder.cpp | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/unittests/test_tun_builder.cpp diff --git a/openvpn/tun/builder/capture.hpp b/openvpn/tun/builder/capture.hpp index 0b7a0d74..f88d4b97 100644 --- a/openvpn/tun/builder/capture.hpp +++ b/openvpn/tun/builder/capture.hpp @@ -150,7 +150,7 @@ class TunBuilderCapture : public TunBuilderBase, public RC(prefix_length); if (!gateway.empty()) os << " -> " << gateway; if (metric >= 0) diff --git a/test/unittests/CMakeLists.txt b/test/unittests/CMakeLists.txt index 70cb8182..2ea39541 100644 --- a/test/unittests/CMakeLists.txt +++ b/test/unittests/CMakeLists.txt @@ -67,6 +67,7 @@ add_executable(coreUnitTests test_streq.cpp test_time.cpp test_typeindex.cpp + test_tun_builder.cpp test_userpass.cpp test_validatecreds.cpp test_weak.cpp diff --git a/test/unittests/test_tun_builder.cpp b/test/unittests/test_tun_builder.cpp new file mode 100644 index 00000000..4ec6d7a6 --- /dev/null +++ b/test/unittests/test_tun_builder.cpp @@ -0,0 +1,14 @@ + +#include "test_common.h" + +#include + +TEST(tun_builder, to_string_zero) +{ + auto rb = openvpn::TunBuilderCapture::RouteBase(); + + rb.address = "0.0.0.0"; + rb.prefix_length = 0; + + EXPECT_EQ(rb.to_string(), "0.0.0.0/0"); +} \ No newline at end of file