From 26110d5c8e11ed5b62f6e8e69e61972d203b5518 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 29 Dec 2021 21:48:50 -0600 Subject: [PATCH] fix: out-of-bound memory acess sonarcloud warning (#2364) --- libtransmission/clients.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libtransmission/clients.cc b/libtransmission/clients.cc index ac25d65aa..fb8af1ab1 100644 --- a/libtransmission/clients.cc +++ b/libtransmission/clients.cc @@ -129,6 +129,8 @@ void two_major_two_minor_formatter(char* buf, size_t buflen, std::string_view na bool decodeShad0wClient(char* buf, size_t buflen, std::string_view in) { + auto const* const buf_in = buf; + // Shad0w with his experimental BitTorrent implementation and BitTornado // introduced peer ids that begin with a character which is``T`` in the // case of BitTornado followed by up to five ascii characters for version @@ -199,7 +201,10 @@ bool decodeShad0wClient(char* buf, size_t buflen, std::string_view in) std::rbegin(vals), std::rend(vals), [&buf, &buflen](int num) { std::tie(buf, buflen) = buf_append(buf, buflen, num, '.'); }); - buf[-1] = '\0'; // remove trailing '.' + if (buf > buf_in) + { + buf[-1] = '\0'; // remove trailing '.' + } return true; }