From 992c714451676cee33d3dff49f36329423270c1c Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sun, 21 Apr 2024 21:48:05 +0200 Subject: [PATCH] common: Don't terminate on null character in UrlDecode The previous behavior was the result of casting the result returned from the libevent function evhttp_uridecode to std:string but this was probably not intended. --- src/common/url.cpp | 5 ----- src/test/common_url_tests.cpp | 6 ++++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/common/url.cpp b/src/common/url.cpp index f27bb2b73f..ecf88d07ea 100644 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -25,11 +25,6 @@ std::string UrlDecode(std::string_view url_encoded) // Only if there is no error and the pointer is set to the end of // the string, we can be sure both characters were valid hex if (ec == std::errc{} && p == url_encoded.data() + i + 3) { - // A null character terminates the string - if (decoded_value == 0) { - return res; - } - res += static_cast(decoded_value); // Next two characters are part of the percent encoding i += 2; diff --git a/src/test/common_url_tests.cpp b/src/test/common_url_tests.cpp index eb92c2ceee..cc893cbed7 100644 --- a/src/test/common_url_tests.cpp +++ b/src/test/common_url_tests.cpp @@ -63,8 +63,10 @@ BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) { } BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) { - BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), ""); - BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), "abc"); + std::string result1{"\0\0x\0\0", 5}; + BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), result1); + std::string result2{"abc\0\0", 5}; + BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), result2); } BOOST_AUTO_TEST_SUITE_END()