From 721ca6fc423e85f8c1ecc93740b3ed044fc65c21 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 4 May 2021 14:38:09 +0300 Subject: get rid of SafeInt --- .gitmodules | 3 --- 3rdparty/microsoft/SafeInt | 1 - CMakeLists.txt | 5 ----- src/convert.cpp | 42 ++++++++++++++++++++++++++++-------------- 4 files changed, 28 insertions(+), 23 deletions(-) delete mode 160000 3rdparty/microsoft/SafeInt 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 index 66b9633..0000000 --- a/3rdparty/microsoft/SafeInt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 66b9633cf4d4102f85e7de3db50f21e25228e9ab 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 -#include - #include #include +#include #include #include #include @@ -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(INT32_MAX)) + return false; + dest = static_cast(src); + return true; +} + +bool int_to_size_t(int32_t src, std::size_t& dest) { + if (src < 0 || static_cast(src) > SIZE_MAX) + return false; + dest = static_cast(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 -std::vector output_buffer(int size) { +std::vector 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 output_buffer(int size) { } template -void verify_output(const std::vector& expected, int actual_size) { - if (!SafeEquals(expected.size(), actual_size)) { +void verify_output(const std::vector& 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()}; } } -- cgit v1.2.3