From f6af7c442df3e569492ced730e70d1bc72356c5e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 15 May 2021 21:22:50 +0300 Subject: get rid of SafeInt --- src/call_stack.cpp | 5 ++--- src/dbghelp.cpp | 16 ++++++++++++---- src/module.cpp | 12 +++++++++--- src/process.cpp | 7 ++++--- src/symbol.cpp | 14 +++++--------- src/utils/file.cpp | 9 +++------ 6 files changed, 35 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/call_stack.cpp b/src/call_stack.cpp index aec50de..183f14a 100644 --- a/src/call_stack.cpp +++ b/src/call_stack.cpp @@ -5,8 +5,6 @@ #include -#include - #include #include @@ -41,8 +39,9 @@ std::string offset_from(const std::string& thing, Address offset) { std::string offset_from_module(const ModuleInfo& module, Address addr) { Address offset = 0; - if (!SafeSubtract(addr, module.get_offline_base(), offset)) + if (addr < module.get_offline_base()) throw std::range_error{"invalid address in module"}; + offset = addr - module.get_offline_base(); return offset_from(module.get_name(), offset); } diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index c9db499..78b8e88 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -5,7 +5,6 @@ #include -#include #include #include @@ -13,6 +12,7 @@ #include #include +#include #include #include #include @@ -40,9 +40,13 @@ Address next_offline_base = 0x10000000; Address gen_next_offline_base(std::size_t pdb_size) { const auto base = next_offline_base; - if (!SafeAdd(next_offline_base, pdb_size, next_offline_base)) + + const auto max_addr = std::numeric_limits::max(); + if (max_addr - next_offline_base < pdb_size) throw std::runtime_error{ "no more PDB files can be added, the internal address space is exhausted"}; + next_offline_base += pdb_size; + return base; } @@ -121,8 +125,12 @@ void DbgHelp::close() { ModuleInfo DbgHelp::load_pdb(const std::string& path) const { DWORD size = 0; - if (!SafeCast(file::get_size(path), size)) - throw std::range_error{"PDB file is too large"}; + { + const auto raw_size = file::get_size(path); + if (raw_size > std::numeric_limits::max()) + throw std::range_error{"PDB file is too large"}; + size = static_cast(raw_size); + } // MinGW-w64 (as of version 7.0) requires PSTR as the third argument. std::vector _path; diff --git a/src/module.cpp b/src/module.cpp index 3f4764c..f05ef03 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -5,10 +5,10 @@ #include -#include #include #include +#include #include #include #include @@ -38,8 +38,11 @@ Address Module::translate_offline_address(Address offline) const { throw std::range_error{invalid_offline_address(offline)}; const auto offset = offline - get_offline_base(); auto online = offset; - if (!SafeAdd(online, get_online_base(), online)) + // Check that it fits the address space. + const auto max_addr = std::numeric_limits::max(); + if (online > max_addr - get_online_base()) throw std::range_error{invalid_offline_address(offline)}; + online += get_online_base(); return online; } @@ -48,8 +51,11 @@ Address Module::translate_online_address(Address online) const { throw std::range_error{invalid_online_address(online)}; const auto offset = online - get_online_base(); auto offline = offset; - if (!SafeAdd(offline, get_offline_base(), offline)) + // Check that it fits the address space. + const auto max_addr = std::numeric_limits::max(); + if (offline > max_addr - get_offline_base()) throw std::range_error{invalid_online_address(offline)}; + offline += get_offline_base(); return offline; } diff --git a/src/process.cpp b/src/process.cpp index 683312c..adb4b96 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -5,11 +5,11 @@ #include -#include #include #include +#include #include #include #include @@ -41,9 +41,10 @@ public: if (size < min_size) { size = min_size; } else { - if (!SafeMultiply(size, 2, size)) { + // Check if we can still multiply by two. + if (std::numeric_limits::max() - size < size) throw std::range_error{"couldn't allocate buffer sufficient for a file path"}; - } + size *= 2; } data.resize(size); } diff --git a/src/symbol.cpp b/src/symbol.cpp index ad7bd3f..9f5e11d 100644 --- a/src/symbol.cpp +++ b/src/symbol.cpp @@ -5,7 +5,6 @@ #include -#include #include #include @@ -13,6 +12,7 @@ #include #include +#include #include #include #include @@ -21,21 +21,17 @@ namespace pdb { namespace { std::size_t calc_size(const SymbolInfo::Impl& impl) { - try { - static constexpr auto char_size = sizeof(std::remove_extent::type); - return SafeInt{impl.SizeOfStruct} + (impl.NameLen - 1) * char_size; - } catch (const SafeIntException&) { - throw std::runtime_error{"invalid SYMBOL_INFO size"}; - } + static constexpr auto char_size = sizeof(std::remove_extent::type); + return impl.SizeOfStruct + (impl.NameLen - 1) * char_size; } unsigned long cast_line_number(DWORD impl) { unsigned long dest = 0; - if (!SafeCast(impl, dest)) + if (impl > std::numeric_limits::max()) throw std::runtime_error{"invalid line number"}; - return dest; + return static_cast(dest); } } // namespace diff --git a/src/utils/file.cpp b/src/utils/file.cpp index fb106ec..3f37cfc 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -5,12 +5,12 @@ #include -#include #include #include #include +#include #include #include @@ -34,12 +34,9 @@ std::size_t get_size(const std::string& path) { if (!GetFileSizeEx(handle.get(), &size)) throw error::windows(GetLastError(), "GetFileSizeEx"); - std::size_t result = 0; - - if (!SafeCast(size.QuadPart, result)) + if (size.QuadPart < 0 || size.QuadPart > SIZE_MAX) throw std::runtime_error{"invalid file size"}; - - return result; + return static_cast(size.QuadPart); } ID query_id(const std::string& path) { -- cgit v1.2.3