diff --git a/include/safetyhook/allocator.hpp b/include/safetyhook/allocator.hpp index 83f0326..7a48144 100644 --- a/include/safetyhook/allocator.hpp +++ b/include/safetyhook/allocator.hpp @@ -36,7 +36,7 @@ class Allocation final { /// @brief Returns the address of the allocation. /// @return The address of the allocation. - [[nodiscard]] uintptr_t address() const noexcept { return (uintptr_t)m_address; } + [[nodiscard]] uintptr_t address() const noexcept { return reinterpret_cast(m_address); } /// @brief Returns the size of the allocation. /// @return The size of the allocation. diff --git a/include/safetyhook/utility.hpp b/include/safetyhook/utility.hpp index 8c982b4..9b24dcb 100644 --- a/include/safetyhook/utility.hpp +++ b/include/safetyhook/utility.hpp @@ -14,6 +14,14 @@ template constexpr void store(uint8_t* address, const T& value) { std::copy_n(reinterpret_cast(&value), sizeof(T), address); } +template constexpr T address_cast(auto address) { + if constexpr (std::is_integral_v && std::is_integral_v) { + return static_cast(address); + } else { + return reinterpret_cast(address); + } +} + template concept FnPtr = requires(T f) { std::is_pointer_v&& std::is_function_v>; }; @@ -42,14 +50,14 @@ class UnprotectMemory { [[nodiscard]] std::optional unprotect(uint8_t* address, size_t size); template constexpr T align_up(T address, size_t align) { - const auto unaligned_address = (uintptr_t)address; + const auto unaligned_address = address_cast(address); const auto aligned_address = (unaligned_address + align - 1) & ~(align - 1); - return (T)aligned_address; + return address_cast(aligned_address); } template constexpr T align_down(T address, size_t align) { - const auto unaligned_address = (uintptr_t)address; + const auto unaligned_address = address_cast(address); const auto aligned_address = unaligned_address & ~(align - 1); - return (T)aligned_address; + return address_cast(aligned_address); } } // namespace safetyhook