From 7b5eced67a35bf5bb5bfa131e8926db6081e5734 Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Tue, 30 Jul 2024 15:48:49 +0200 Subject: [PATCH] Replaced deprecated sprintf with snprintf --- lib/cdm/cdm/media/cdm/cdm_adapter.cc | 2 +- src/common/SegTemplate.cpp | 4 ++-- src/test/TestUtils.cpp | 21 +++++++++++++++++++++ src/utils/DigestMD5Utils.cpp | 4 +++- src/utils/StringUtils.cpp | 4 ++-- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/cdm/cdm/media/cdm/cdm_adapter.cc b/lib/cdm/cdm/media/cdm/cdm_adapter.cc index 0d5e961fc..22f872257 100644 --- a/lib/cdm/cdm/media/cdm/cdm_adapter.cc +++ b/lib/cdm/cdm/media/cdm/cdm_adapter.cc @@ -623,7 +623,7 @@ void CdmAdapter::OnSessionKeysChange(const char* session_id, char buffer[128]; char* bufferPtr{buffer}; for (uint32_t j{0}; j < keys_info[i].key_id_size; ++j) - bufferPtr += sprintf(bufferPtr, "%02X", (int)keys_info[i].key_id[j]); + bufferPtr += std::snprintf(bufferPtr, 3, "%02X", (int)keys_info[i].key_id[j]); LOG::Log(LOGDEBUG, "%s: Sessionkey %s status: %d syscode: %u", __func__, buffer, keys_info[i].status, keys_info[i].system_code); diff --git a/src/common/SegTemplate.cpp b/src/common/SegTemplate.cpp index 5cc65e375..dea1b6ec0 100644 --- a/src/common/SegTemplate.cpp +++ b/src/common/SegTemplate.cpp @@ -13,7 +13,7 @@ #include "kodi/tools/StringUtils.h" -#include // sprintf +#include // snprintf using namespace PLAYLIST; using namespace UTILS; @@ -198,7 +198,7 @@ std::string PLAYLIST::CSegmentTemplate::FormatIdentifier(std::string_view identi } char substitution[128]; - if (std::sprintf(substitution, formatTag.c_str(), value) > 0) + if (std::snprintf(substitution, 128, formatTag.c_str(), value) > 0) return substitution; else LOG::LogF(LOGERROR, "Cannot convert value \"%llu\" with \"%s\" format tag", value, diff --git a/src/test/TestUtils.cpp b/src/test/TestUtils.cpp index 3607898e9..f536fd392 100644 --- a/src/test/TestUtils.cpp +++ b/src/test/TestUtils.cpp @@ -10,6 +10,8 @@ #include "../common/AdaptiveTreeFactory.h" #include "../common/SegTemplate.h" +#include "../utils/DigestMD5Utils.h" +#include "../utils/StringUtils.h" #include "../utils/UrlUtils.h" #include "../utils/XMLUtils.h" @@ -266,3 +268,22 @@ TEST_F(UtilsTest, XMLDateTimeConversions) EXPECT_EQ(XML::ParseDate("2024-05-07T17:00:21.989+0200"), 1715101221.989); } + +TEST_F(UtilsTest, MD5HashTest) +{ + std::string strTest = "Test"; + DIGEST::MD5 md5; + md5.Update(strTest.c_str(), static_cast(strTest.size())); + md5.Finalize(); + + EXPECT_EQ(md5.HexDigest(), "0cbc6611f5540bd0809a388dc95a615b"); +} + +TEST_F(UtilsTest, UrlEncodeDecode) +{ + const std::string strTest = "abc123-._!()~&%\xC3\xA8\xC3\xB9"; // abc123-._!()~&%ищ + std::string encoded = STRING::URLEncode(strTest); + + EXPECT_EQ(encoded, "abc123-._!()~%26%25%C3%A8%C3%B9"); + EXPECT_EQ(STRING::URLDecode(encoded), strTest); +} diff --git a/src/utils/DigestMD5Utils.cpp b/src/utils/DigestMD5Utils.cpp index 1ead6ee97..f410832ad 100644 --- a/src/utils/DigestMD5Utils.cpp +++ b/src/utils/DigestMD5Utils.cpp @@ -255,7 +255,9 @@ std::string UTILS::DIGEST::MD5::HexDigest() const char buf[33]; for (int i = 0; i < 16; i++) - sprintf(buf + i * 2, "%02x", m_digest[i]); + { + std::snprintf(buf + i * 2, 3, "%02x", m_digest[i]); + } buf[32] = 0; return std::string(buf); diff --git a/src/utils/StringUtils.cpp b/src/utils/StringUtils.cpp index 20494a921..c051dc63f 100644 --- a/src/utils/StringUtils.cpp +++ b/src/utils/StringUtils.cpp @@ -150,8 +150,8 @@ std::string UTILS::STRING::URLEncode(std::string_view strURLData) else { result.append("%"); - char buf[3]; - sprintf(buf, "%.2X", c); + char buf[4]; // 3 chars + null + std::snprintf(buf, 4, "%.2X", static_cast(c)); result.append(buf); } }