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 --- .clang-format | 2 +- .gitmodules | 3 --- 3rdparty/microsoft/SafeInt | 1 - include/pdb/call_stack.hpp | 2 -- 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 +++------ 10 files changed, 36 insertions(+), 35 deletions(-) delete mode 160000 3rdparty/microsoft/SafeInt diff --git a/.clang-format b/.clang-format index 08d4d1d..21ec554 100644 --- a/.clang-format +++ b/.clang-format @@ -14,7 +14,7 @@ IncludeCategories: Priority: 1 - Regex: '^$' Priority: 4 diff --git a/.gitmodules b/.gitmodules index 495304d..7a1b1cb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,6 @@ [submodule "cmake"] path = cmake url = https://github.com/egor-tensin/cmake-common.git -[submodule "3rdparty/microsoft/SafeInt"] - path = 3rdparty/microsoft/SafeInt - url = https://github.com/dcleblanc/SafeInt.git [submodule "3rdparty/boost/nowide"] path = 3rdparty/boost/nowide url = https://github.com/boostorg/nowide.git diff --git a/3rdparty/microsoft/SafeInt b/3rdparty/microsoft/SafeInt deleted file mode 160000 index a77fa86..0000000 --- a/3rdparty/microsoft/SafeInt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a77fa86b5c1e1486ac90944568174c804a599e77 diff --git a/include/pdb/call_stack.hpp b/include/pdb/call_stack.hpp index ef4c119..e86569a 100644 --- a/include/pdb/call_stack.hpp +++ b/include/pdb/call_stack.hpp @@ -8,8 +8,6 @@ #include "address.hpp" #include "dbghelp.hpp" -#include - #include #include 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