From d48f6281af49b17d1f6d6802b8921c1aed38a713 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 19 May 2017 06:48:22 +0300 Subject: hardening & refactoring My latest obsession is integer overflows. --- include/pdb/module.hpp | 56 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 49 deletions(-) (limited to 'include/pdb') diff --git a/include/pdb/module.hpp b/include/pdb/module.hpp index 3e785c4..e539fde 100644 --- a/include/pdb/module.hpp +++ b/include/pdb/module.hpp @@ -10,10 +10,6 @@ #include #include -#include - -#include -#include #include namespace pdb @@ -23,16 +19,10 @@ namespace pdb public: typedef IMAGEHLP_MODULE64 Raw; - ModuleInfo() - : raw{prepare_buffer()} - { } - - explicit ModuleInfo(const Raw& raw) - : raw{raw} - { } + ModuleInfo(); + explicit ModuleInfo(const Raw& raw); explicit operator Raw&() { return raw; } - explicit operator const Raw&() const { return raw; } Address get_offline_base() const { return raw.BaseOfImage; } @@ -40,13 +30,7 @@ namespace pdb std::string get_name() const { return raw.ModuleName; } private: - static Raw prepare_buffer() - { - Raw raw; - std::memset(&raw, 0, sizeof(raw)); - raw.SizeOfStruct = sizeof(raw); - return raw; - } + static Raw create_raw(); Raw raw; }; @@ -61,38 +45,12 @@ namespace pdb Address get_online_base() const { return online_base; } - Address translate_offline_address(Address offline) const - { - if (offline < get_offline_base()) - throw std::range_error{invalid_offline_address(offline)}; - return offline - get_offline_base() + get_online_base(); - } - - Address translate_online_address(Address online) const - { - if (online < get_online_base()) - throw std::range_error{invalid_online_address(online)}; - return online - get_online_base() + get_offline_base(); - } + Address translate_offline_address(Address offline) const; + Address translate_online_address(Address online) const; private: - std::string invalid_offline_address(Address offline) const - { - std::ostringstream oss; - oss << "offline address " << format_address(offline) - << " doesn't belong to module " << get_name() - << " (base offline address " << format_address(get_offline_base()) << ')'; - return oss.str(); - } - - std::string invalid_online_address(Address online) const - { - std::ostringstream oss; - oss << "online address " << format_address(online) - << " doesn't belong to module " << get_name() - << " (base online address " << format_address(get_online_base()) << ')'; - return oss.str(); - } + std::string invalid_offline_address(Address offline) const; + std::string invalid_online_address(Address online) const; const Address online_base; }; -- cgit v1.2.3