diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2021-05-15 21:22:50 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-05-15 21:22:50 +0300 |
commit | f6af7c442df3e569492ced730e70d1bc72356c5e (patch) | |
tree | bd18c18acbc131bd43de99c9f03f698eff479a82 /src | |
parent | workflows/ci: build using VS 2015 also (diff) | |
download | winapi-debug-f6af7c442df3e569492ced730e70d1bc72356c5e.tar.gz winapi-debug-f6af7c442df3e569492ced730e70d1bc72356c5e.zip |
get rid of SafeInt
Diffstat (limited to '')
-rw-r--r-- | src/call_stack.cpp | 5 | ||||
-rw-r--r-- | src/dbghelp.cpp | 16 | ||||
-rw-r--r-- | src/module.cpp | 12 | ||||
-rw-r--r-- | src/process.cpp | 7 | ||||
-rw-r--r-- | src/symbol.cpp | 14 | ||||
-rw-r--r-- | src/utils/file.cpp | 9 |
6 files changed, 35 insertions, 28 deletions
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 <pdb/all.hpp> -#include <SafeInt.hpp> - #include <windows.h> #include <algorithm> @@ -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 <pdb/all.hpp> -#include <SafeInt.hpp> #include <boost/nowide/convert.hpp> #include <dbghelp.h> @@ -13,6 +12,7 @@ #include <cstddef> #include <cstring> +#include <limits> #include <stdexcept> #include <string> #include <vector> @@ -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<decltype(next_offline_base)>::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<decltype(size)>::max()) + throw std::range_error{"PDB file is too large"}; + size = static_cast<decltype(size)>(raw_size); + } // MinGW-w64 (as of version 7.0) requires PSTR as the third argument. std::vector<char> _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 <pdb/all.hpp> -#include <SafeInt.hpp> #include <boost/nowide/convert.hpp> #include <cstring> +#include <limits> #include <sstream> #include <stdexcept> #include <string> @@ -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<decltype(online)>::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<decltype(offline)>::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 <pdb/all.hpp> -#include <SafeInt.hpp> #include <boost/nowide/convert.hpp> #include <windows.h> +#include <limits> #include <stdexcept> #include <string> #include <utility> @@ -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<decltype(size)>::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 <pdb/all.hpp> -#include <SafeInt.hpp> #include <boost/nowide/convert.hpp> #include <dbghelp.h> @@ -13,6 +12,7 @@ #include <cstddef> #include <cstring> +#include <limits> #include <stdexcept> #include <string> #include <type_traits> @@ -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<decltype(impl.Name)>::type); - return SafeInt<std::size_t>{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<decltype(impl.Name)>::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<decltype(dest)>::max()) throw std::runtime_error{"invalid line number"}; - return dest; + return static_cast<decltype(dest)>(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 <pdb/all.hpp> -#include <SafeInt.hpp> #include <boost/nowide/convert.hpp> #include <windows.h> #include <cstddef> +#include <cstdint> #include <stdexcept> #include <string> @@ -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<std::size_t>(size.QuadPart); } ID query_id(const std::string& path) { |