diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2021-05-04 14:38:09 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-05-04 15:01:05 +0300 |
commit | 721ca6fc423e85f8c1ecc93740b3ed044fc65c21 (patch) | |
tree | 457577a9701b2cf64a591f9ca6ef4d6c4daf5e50 | |
parent | workflows/ci: linting (diff) | |
download | winapi-utf8-721ca6fc423e85f8c1ecc93740b3ed044fc65c21.tar.gz winapi-utf8-721ca6fc423e85f8c1ecc93740b3ed044fc65c21.zip |
get rid of SafeInt
-rw-r--r-- | .gitmodules | 3 | ||||
m--------- | 3rdparty/microsoft/SafeInt | 0 | ||||
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/convert.cpp | 42 |
4 files changed, 28 insertions, 22 deletions
diff --git a/.gitmodules b/.gitmodules index 486e870..d65ecb9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [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/egor-tensin/SafeInt.git diff --git a/3rdparty/microsoft/SafeInt b/3rdparty/microsoft/SafeInt deleted file mode 160000 -Subproject 66b9633cf4d4102f85e7de3db50f21e25228e9a diff --git a/CMakeLists.txt b/CMakeLists.txt index d2e111a..b4b277b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,15 +5,10 @@ enable_testing() include(cmake/common.cmake) -if(NOT TARGET SafeInt) - add_subdirectory(3rdparty/microsoft/SafeInt EXCLUDE_FROM_ALL) -endif() - file(GLOB_RECURSE winapi_utf8_include "include/*.hpp") file(GLOB winapi_utf8_src "src/*.cpp") add_library(winapi_utf8 ${winapi_utf8_include} ${winapi_utf8_src}) target_include_directories(winapi_utf8 PUBLIC include) -target_link_libraries(winapi_utf8 PRIVATE SafeInt) # Vista is the lower bound (due to WC_ERR_INVALID_CHARS): if(MINGW) target_compile_definitions(winapi_utf8 PRIVATE diff --git a/src/convert.cpp b/src/convert.cpp index cb0bc1c..bacea60 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -5,11 +5,10 @@ #include <winapi/utf8.hpp> -#include <SafeInt.hpp> - #include <windows.h> #include <cstddef> +#include <cstdint> #include <sstream> #include <stdexcept> #include <string> @@ -24,10 +23,24 @@ std::runtime_error error(const char* function, DWORD code) { return std::runtime_error{oss.str()}; } -int convert_input_bytes_to_bytes(std::size_t nb) { - int real_nb = 0; +bool size_t_to_int(std::size_t src, int32_t& dest) { + if (src > static_cast<uint32_t>(INT32_MAX)) + return false; + dest = static_cast<int32_t>(src); + return true; +} + +bool int_to_size_t(int32_t src, std::size_t& dest) { + if (src < 0 || static_cast<uint32_t>(src) > SIZE_MAX) + return false; + dest = static_cast<std::size_t>(src); + return true; +} - if (!SafeCast(nb, real_nb)) { +int32_t convert_input_bytes_to_bytes(std::size_t nb) { + int32_t real_nb = 0; + + if (!size_t_to_int(nb, real_nb)) { std::ostringstream oss; oss << "Input buffer is too large at " << nb << " bytes"; throw std::runtime_error{oss.str()}; @@ -36,7 +49,7 @@ int convert_input_bytes_to_bytes(std::size_t nb) { return real_nb; } -int convert_input_bytes_to_chars(std::size_t nb) { +int32_t convert_input_bytes_to_chars(std::size_t nb) { if (nb % sizeof(WCHAR) != 0) { std::ostringstream oss; oss << "Buffer size invalid at " << nb << " bytes"; @@ -44,10 +57,9 @@ int convert_input_bytes_to_chars(std::size_t nb) { } const std::size_t nch = nb / sizeof(WCHAR); + int32_t real_nch = 0; - int real_nch = 0; - - if (!SafeCast(nch, real_nch)) { + if (!size_t_to_int(nch, real_nch)) { std::ostringstream oss; oss << "Input buffer is too large at " << nch << " characters"; throw std::runtime_error{oss.str()}; @@ -57,10 +69,10 @@ int convert_input_bytes_to_chars(std::size_t nb) { } template <typename CharT> -std::vector<CharT> output_buffer(int size) { +std::vector<CharT> output_buffer(int32_t size) { std::size_t real_size = 0; - if (!SafeCast(size, real_size)) { + if (!int_to_size_t(size, real_size)) { std::ostringstream oss; oss << "Buffer size invalid at " << size << " bytes"; throw std::runtime_error{oss.str()}; @@ -72,10 +84,12 @@ std::vector<CharT> output_buffer(int size) { } template <typename CharT> -void verify_output(const std::vector<CharT>& expected, int actual_size) { - if (!SafeEquals(expected.size(), actual_size)) { +void verify_output(const std::vector<CharT>& expected, int32_t _actual_size) { + std::size_t actual_size = 0; + + if (!int_to_size_t(_actual_size, actual_size) || expected.size() != actual_size) { std::ostringstream oss; - oss << "Expected output length " << expected.size() << ", got " << actual_size; + oss << "Expected output length " << expected.size() << ", got " << _actual_size; throw std::runtime_error{oss.str()}; } } |