1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-22 07:42:37 +00:00

fix: handle nullptr in json serde (#7258)

* fix: handle nullptr in json serde

* test: fuzz json serde
This commit is contained in:
Yat Ho 2024-12-10 01:59:10 +08:00 committed by GitHub
parent affb03a8d2
commit 60e5d98dc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View file

@ -203,11 +203,11 @@ private:
std::optional<tr_variant> tr_variant_serde::parse_json(std::string_view input)
{
auto* const begin = std::data(input);
TR_ASSERT(begin != nullptr); // RapidJSON will dereference a nullptr if this is false
auto* begin = std::data(input);
if (begin == nullptr)
{
return {};
// RapidJSON will dereference a nullptr otherwise
begin = "";
}
auto const size = std::size(input);

View file

@ -11,6 +11,7 @@
#include <string>
#include <string_view>
#include <libtransmission/crypto-utils.h>
#include <libtransmission/quark.h>
#include <libtransmission/utils.h>
#include <libtransmission/variant.h>
@ -239,6 +240,23 @@ TEST_P(JSONTest, unescape)
EXPECT_EQ("/usr/lib"sv, sv);
}
TEST_P(JSONTest, parseJsonFuzz)
{
auto serde = tr_variant_serde::json().inplace();
auto var = serde.parse({ nullptr, 0U });
EXPECT_FALSE(var);
auto buf = std::vector<char>{};
for (size_t i = 0; i < 100000U; ++i)
{
buf.resize(tr_rand_int(1024U));
tr_rand_buffer(std::data(buf), std::size(buf));
(void)serde.parse({ std::data(buf), std::size(buf) });
}
}
INSTANTIATE_TEST_SUITE_P( //
JSON,
JSONTest,