0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-19 19:52:15 +02:00

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 <charlie.vigue@openvpn.com>
This commit is contained in:
Charlie Vigue 2024-03-29 03:13:00 +00:00
parent e150bb3985
commit dd115d2e83
3 changed files with 16 additions and 1 deletions

View File

@ -150,7 +150,7 @@ class TunBuilderCapture : public TunBuilderBase, public RC<thread_unsafe_refcoun
std::string to_string() const
{
std::ostringstream os;
os << address << '/' << prefix_length;
os << address << '/' << static_cast<uint16_t>(prefix_length);
if (!gateway.empty())
os << " -> " << gateway;
if (metric >= 0)

View File

@ -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

View File

@ -0,0 +1,14 @@
#include "test_common.h"
#include <openvpn/tun/builder/capture.hpp>
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");
}