From 2ef2cdd874374c36f336f6d90defe7bc2a79c236 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 17 May 2025 20:29:54 +0200 Subject: [PATCH] fix: Make sure PerProvider don't get accessed using a nullptr provider --- lib/libimhex/include/hex/providers/provider_data.hpp | 12 ++++++++++++ .../builtin/source/content/views/view_hex_editor.cpp | 8 ++++++-- .../source/content/views/view_highlight_rules.cpp | 1 - .../source/content/views/view_pattern_editor.cpp | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/libimhex/include/hex/providers/provider_data.hpp b/lib/libimhex/include/hex/providers/provider_data.hpp index e2f9710f0..41d6c9120 100644 --- a/lib/libimhex/include/hex/providers/provider_data.hpp +++ b/lib/libimhex/include/hex/providers/provider_data.hpp @@ -38,18 +38,30 @@ namespace hex { } T& get(const prv::Provider *provider = ImHexApi::Provider::get()) { + if (provider == nullptr) [[unlikely]] + throw std::invalid_argument("PerProvider::get called with nullptr"); + return m_data[provider]; } const T& get(const prv::Provider *provider = ImHexApi::Provider::get()) const { + if (provider == nullptr) [[unlikely]] + throw std::invalid_argument("PerProvider::get called with nullptr"); + return m_data.at(provider); } void set(const T &data, const prv::Provider *provider = ImHexApi::Provider::get()) { + if (provider == nullptr) [[unlikely]] + throw std::invalid_argument("PerProvider::set called with nullptr"); + m_data[provider] = data; } void set(T &&data, const prv::Provider *provider = ImHexApi::Provider::get()) { + if (provider == nullptr) [[unlikely]] + throw std::invalid_argument("PerProvider::set called with nullptr"); + m_data[provider] = std::move(data); } diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 18cef4dd5..73232a0f8 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1090,8 +1090,12 @@ namespace hex::plugin::builtin { }); EventHighlightingChanged::subscribe(this, [this]{ - m_foregroundHighlights->clear(); - m_backgroundHighlights->clear(); + auto provider = ImHexApi::Provider::get(); + if (provider == nullptr) + return; + + m_foregroundHighlights.get(provider).clear(); + m_backgroundHighlights.get(provider).clear(); }); ProjectFile::registerPerProviderHandler({ diff --git a/plugins/builtin/source/content/views/view_highlight_rules.cpp b/plugins/builtin/source/content/views/view_highlight_rules.cpp index 2b61a9b58..4cb04062f 100644 --- a/plugins/builtin/source/content/views/view_highlight_rules.cpp +++ b/plugins/builtin/source/content/views/view_highlight_rules.cpp @@ -168,7 +168,6 @@ namespace hex::plugin::builtin { }); // Initialize the selected rule iterators to point to the end of the rules lists - m_selectedRule = m_rules->end(); EventProviderOpened::subscribe([this](prv::Provider *provider) { m_selectedRule.get(provider) = m_rules.get(provider).end(); }); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index a7e6f9c37..cbf169587 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1462,6 +1462,8 @@ namespace hex::plugin::builtin { void ViewPatternEditor::drawAlwaysVisibleContent() { auto provider = ImHexApi::Provider::get(); + if (provider == nullptr) + return; auto open = m_sectionWindowDrawer.contains(provider); if (open) {