Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C++ style casts #80

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/safetyhook/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uintptr_t>(m_address); }

/// @brief Returns the size of the allocation.
/// @return The size of the allocation.
Expand Down
16 changes: 12 additions & 4 deletions include/safetyhook/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ template <typename T> constexpr void store(uint8_t* address, const T& value) {
std::copy_n(reinterpret_cast<const uint8_t*>(&value), sizeof(T), address);
}

template <typename T> constexpr T address_cast(auto address) {
if constexpr (std::is_integral_v<T> && std::is_integral_v<decltype(address)>) {
return static_cast<T>(address);
} else {
return reinterpret_cast<T>(address);
}
}

template <typename T>
concept FnPtr = requires(T f) { std::is_pointer_v<T>&& std::is_function_v<std::remove_pointer_t<T>>; };

Expand Down Expand Up @@ -42,14 +50,14 @@ class UnprotectMemory {
[[nodiscard]] std::optional<UnprotectMemory> unprotect(uint8_t* address, size_t size);

template <typename T> constexpr T align_up(T address, size_t align) {
const auto unaligned_address = (uintptr_t)address;
const auto unaligned_address = address_cast<uintptr_t>(address);
const auto aligned_address = (unaligned_address + align - 1) & ~(align - 1);
return (T)aligned_address;
return address_cast<T>(aligned_address);
}

template <typename T> constexpr T align_down(T address, size_t align) {
const auto unaligned_address = (uintptr_t)address;
const auto unaligned_address = address_cast<uintptr_t>(address);
const auto aligned_address = unaligned_address & ~(align - 1);
return (T)aligned_address;
return address_cast<T>(aligned_address);
}
} // namespace safetyhook
Loading