From ea0333cd82d4849311ec5e3702e7c8fd96f07f45 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Thu, 4 Sep 2025 00:23:35 +0300 Subject: [PATCH] Use snprintf with buffer length --- sha1.cpp | 5 +++-- sha256.cpp | 3 ++- sha512.cpp | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sha1.cpp b/sha1.cpp index 4e68083..14fec7a 100644 --- a/sha1.cpp +++ b/sha1.cpp @@ -18,7 +18,8 @@ /* Based on: http://www.zedwood.com/article/cpp-sha1-function * Modified by NewYaroslav, 2025-04-15 */ - + +#include #include "sha1.hpp" /* Help macros */ @@ -246,7 +247,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA1::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA1::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA1::DIGEST_SIZE; ++i) { - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA1::DIGEST_SIZE)); } diff --git a/sha256.cpp b/sha256.cpp index d8aae6e..c189fe1 100644 --- a/sha256.cpp +++ b/sha256.cpp @@ -40,6 +40,7 @@ */ #include +#include //#include #include "sha256.hpp" @@ -205,7 +206,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA256::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA256::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA256::DIGEST_SIZE; ++i) { - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA256::DIGEST_SIZE)); } diff --git a/sha512.cpp b/sha512.cpp index 83d784b..a82fa34 100644 --- a/sha512.cpp +++ b/sha512.cpp @@ -40,6 +40,7 @@ */ #include +#include #include "sha512.hpp" #define SHA2_SHFR(x, n) (x >> n) @@ -240,7 +241,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA512::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA512::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA512::DIGEST_SIZE; ++i){ - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA512::DIGEST_SIZE)); }