Skip to content

Commit

Permalink
vm_ptr: return nullptr and add some nullptr deref checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Jun 11, 2024
1 parent cc19685 commit 11a8610
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions rpcs3/Emu/Memory/vm_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,39 @@ namespace vm
return vm::cast(m_addr);
}

template <bool Strict = false>
T* get_ptr() const
{
return static_cast<T*>(vm::base(vm::cast(m_addr)));
if (operator bool()) [[likely]]
{
return static_cast<T*>(vm::base(vm::cast(m_addr)));
}

if constexpr (Strict)
{
fmt::throw_exception("Invalid pointer");
}

return nullptr;
}

T* operator ->() const requires (!std::is_void_v<T>)
{
return get_ptr();
return get_ptr<true>();
}

std::add_lvalue_reference_t<T> operator *() const requires (!std::is_void_v<T>)
{
return *static_cast<T*>(vm::base(vm::cast(m_addr)));
return *get_ptr<true>();
}

std::add_lvalue_reference_t<T> operator [](u32 index) const requires (!std::is_void_v<T>)
{
if (operator bool() == false) [[unlikely]]
{
fmt::throw_exception("Invalid pointer");
}

return *static_cast<T*>(vm::base(vm::cast(m_addr) + u32{sizeof(T)} * index));
}

Expand Down

0 comments on commit 11a8610

Please sign in to comment.