aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2021-05-15 21:22:50 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2021-05-15 21:22:50 +0300
commitf6af7c442df3e569492ced730e70d1bc72356c5e (patch)
treebd18c18acbc131bd43de99c9f03f698eff479a82
parentworkflows/ci: build using VS 2015 also (diff)
downloadwinapi-debug-f6af7c442df3e569492ced730e70d1bc72356c5e.tar.gz
winapi-debug-f6af7c442df3e569492ced730e70d1bc72356c5e.zip
get rid of SafeInt
-rw-r--r--.clang-format2
-rw-r--r--.gitmodules3
m---------3rdparty/microsoft/SafeInt0
-rw-r--r--include/pdb/call_stack.hpp2
-rw-r--r--src/call_stack.cpp5
-rw-r--r--src/dbghelp.cpp16
-rw-r--r--src/module.cpp12
-rw-r--r--src/process.cpp7
-rw-r--r--src/symbol.cpp14
-rw-r--r--src/utils/file.cpp9
10 files changed, 36 insertions, 34 deletions
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: '^<test_lib\.|^<pdb\/'
Priority: 2
- - Regex: '^<boost\/|^<SafeInt\.'
+ - Regex: '^<boost\/'
Priority: 3
- Regex: '^<.*\.h>$'
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
-Subproject a77fa86b5c1e1486ac90944568174c804a599e7
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 <SafeInt.hpp>
-
#include <windows.h>
#include <array>
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) {