Skip to content

Commit

Permalink
Merge pull request #1612 from CastagnaIT/sprintf
Browse files Browse the repository at this point in the history
Replaced deprecated sprintf with snprintf
  • Loading branch information
CastagnaIT authored Jul 30, 2024
2 parents 9cbda82 + 1700aca commit a7f9338
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/cdm/cdm/media/cdm/cdm_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/common/SegTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "kodi/tools/StringUtils.h"

#include <cstdio> // sprintf
#include <cstdio> // snprintf

using namespace PLAYLIST;
using namespace UTILS;
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/test/TestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<uint32_t>(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);
}
4 changes: 3 additions & 1 deletion src/utils/DigestMD5Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>(c));
result.append(buf);
}
}
Expand Down

0 comments on commit a7f9338

Please sign in to comment.