From 55b877d5e0a5cf149a86dd7643bee8fb9151c3ad Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 17 Aug 2022 16:28:44 +0200 Subject: [PATCH] fix: Region validity check not working correctly with non-zero base address Fixes #683 --- lib/libimhex/source/providers/provider.cpp | 2 +- .../content/providers/intel_hex_provider.hpp | 2 ++ .../source/content/providers/disk_provider.cpp | 4 +++- .../source/content/providers/file_provider.cpp | 4 +++- .../source/content/providers/gdb_provider.cpp | 4 +++- .../content/providers/intel_hex_provider.cpp | 15 +++++++++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 07b9dbc46..7169bd210 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -266,7 +266,7 @@ namespace hex::prv { } std::pair Provider::getRegionValidity(u64 address) const { - if (address > this->getActualSize()) + if ((address - this->getBaseAddress()) > this->getActualSize()) return { Region::Invalid(), false }; bool insideValidRegion = false; diff --git a/plugins/builtin/include/content/providers/intel_hex_provider.hpp b/plugins/builtin/include/content/providers/intel_hex_provider.hpp index 07fcec49a..4e101115f 100644 --- a/plugins/builtin/include/content/providers/intel_hex_provider.hpp +++ b/plugins/builtin/include/content/providers/intel_hex_provider.hpp @@ -17,6 +17,8 @@ namespace hex::plugin::builtin::prv { [[nodiscard]] bool isResizable() const override { return false; } [[nodiscard]] bool isSavable() const override { return false; } + void setBaseAddress(u64 address) override; + void readRaw(u64 offset, void *buffer, size_t size) override; void writeRaw(u64 offset, const void *buffer, size_t size) override; [[nodiscard]] size_t getActualSize() const override; diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 801564a5f..f2d7b2d39 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -358,8 +358,10 @@ namespace hex::plugin::builtin::prv { } std::pair DiskProvider::getRegionValidity(u64 address) const { + address -= this->getBaseAddress(); + if (address < this->getActualSize()) - return { Region { address, this->getActualSize() - address }, true }; + return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true }; else return { Region::Invalid(), false }; } diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 1b4cb088c..6dacdf033 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -336,8 +336,10 @@ namespace hex::plugin::builtin::prv { } std::pair FileProvider::getRegionValidity(u64 address) const { + address -= this->getBaseAddress(); + if (address < this->getActualSize()) - return { Region { address, this->getActualSize() - address }, true }; + return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true }; else return { Region::Invalid(), false }; } diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index f41626b5c..c677a6da8 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -325,8 +325,10 @@ namespace hex::plugin::builtin::prv { } std::pair GDBProvider::getRegionValidity(u64 address) const { + address -= this->getBaseAddress(); + if (address < this->getActualSize()) - return { Region { address, this->getActualSize() - address }, true }; + return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true }; else return { Region::Invalid(), false }; } diff --git a/plugins/builtin/source/content/providers/intel_hex_provider.cpp b/plugins/builtin/source/content/providers/intel_hex_provider.cpp index a69abf0ea..a088114f1 100644 --- a/plugins/builtin/source/content/providers/intel_hex_provider.cpp +++ b/plugins/builtin/source/content/providers/intel_hex_provider.cpp @@ -153,6 +153,21 @@ namespace hex::plugin::builtin::prv { } + void IntelHexProvider::setBaseAddress(u64 address) { + auto oldBase = this->getBaseAddress(); + + auto intervals = this->m_data.findOverlapping(oldBase, oldBase + this->getActualSize()); + + for (auto &interval : intervals) { + interval.start = (interval.start - oldBase) + address; + interval.stop = (interval.stop - oldBase) + address; + } + + this->m_data = std::move(intervals); + + Provider::setBaseAddress(address); + } + void IntelHexProvider::readRaw(u64 offset, void *buffer, size_t size) { auto intervals = this->m_data.findOverlapping(offset, (offset + size) - 1);