From a95eedffffc1f4d5cc3ce9afb8defdbaae35918b Mon Sep 17 00:00:00 2001 From: "Md. Anisul Haque" Date: Wed, 24 May 2023 15:13:04 +0530 Subject: [PATCH] fix: suggested changes --- hashing/sha256.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hashing/sha256.cpp b/hashing/sha256.cpp index 3a4aeb3da..2431d89aa 100644 --- a/hashing/sha256.cpp +++ b/hashing/sha256.cpp @@ -49,7 +49,10 @@ std::size_t compute_padded_size(const std::size_t input_size) { * @param byte_num Position of byte to be returned * @return uint8_t Byte at position byte_num */ -uint8_t extract_byte(const std::size_t in_value, const std::size_t byte_num) { +uint8_t extract_byte(const auto in_value, const std::size_t byte_num) { + if (sizeof(in_value) <= byte_num) { + throw std::out_of_range("Byte at index byte_num does not exist"); + } return ((in_value >> (byte_num * 8)) & 0xFF); } @@ -71,6 +74,9 @@ char get_char(const std::string &input, std::size_t pos) { if (pos < padded_input_size - 8) { return '\x00'; } + if (padded_input_size <= pos) { + throw std::out_of_range("pos is out of range"); + } return static_cast( extract_byte(input_size * 8, padded_input_size - pos - 1)); }