Skip to content

Commit

Permalink
Fix undefined behavior in scan_result_base::read
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroMemes committed Oct 9, 2024
1 parent 31a9c99 commit 16be213
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion include/libhat/Scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ namespace hat {
/// Reads an integer of the specified type located at an offset from the signature result
template<std::integral Int>
[[nodiscard]] constexpr Int read(size_t offset) const {
return *reinterpret_cast<const Int*>(this->result + offset);
if LIBHAT_IF_CONSTEVAL {
static constexpr size_t sz = sizeof(Int);
return std::bit_cast<Int>([=]<size_t... Index>(std::index_sequence<Index...>) {
return std::array<T, sz>{this->result + offset + Index...};
}(std::make_index_sequence<sz>{}));
} else {
Int value;
std::memcpy(&value, this->result + offset, sizeof(Int));
return value;
}
}

/// Reads an integer of the specified type which represents an index into an array with the given element type
Expand Down

0 comments on commit 16be213

Please sign in to comment.