From df4287c3c763de14b262ed39d54b3de552adbefb Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 31 Aug 2017 07:13:58 -0400 Subject: Fix various MSVC warnings Based on VC2017 output --- src/lib/utils/ct_utils.h | 2 +- src/lib/utils/data_src.cpp | 8 ++++---- src/lib/utils/os_utils.cpp | 18 ++++++++++-------- src/lib/utils/parsing.cpp | 45 ++++++++++++++++++++++++--------------------- src/lib/utils/parsing.h | 7 +++++++ 5 files changed, 46 insertions(+), 34 deletions(-) (limited to 'src/lib/utils') diff --git a/src/lib/utils/ct_utils.h b/src/lib/utils/ct_utils.h index 68bd01c94..709b6d9e5 100644 --- a/src/lib/utils/ct_utils.h +++ b/src/lib/utils/ct_utils.h @@ -115,7 +115,7 @@ inline T is_zero(T x) template inline T is_equal(T x, T y) { - return is_zero(x ^ y); + return is_zero(x ^ y); } template diff --git a/src/lib/utils/data_src.cpp b/src/lib/utils/data_src.cpp index 0e9fd0e33..078d3f2ea 100644 --- a/src/lib/utils/data_src.cpp +++ b/src/lib/utils/data_src.cpp @@ -110,7 +110,7 @@ size_t DataSource_Stream::read(uint8_t out[], size_t length) if(m_source.bad()) throw Stream_IO_Error("DataSource_Stream::read: Source failure"); - size_t got = m_source.gcount(); + const size_t got = static_cast(m_source.gcount()); m_total_read += got; return got; } @@ -119,7 +119,7 @@ bool DataSource_Stream::check_available(size_t n) { const std::streampos orig_pos = m_source.tellg(); m_source.seekg(0, std::ios::end); - const size_t avail = m_source.tellg() - orig_pos; + const size_t avail = static_cast(m_source.tellg() - orig_pos); m_source.seekg(orig_pos); return (avail >= n); } @@ -140,7 +140,7 @@ size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) cons m_source.read(reinterpret_cast(buf.data()), buf.size()); if(m_source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); - got = m_source.gcount(); + got = static_cast(m_source.gcount()); } if(got == offset) @@ -148,7 +148,7 @@ size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) cons m_source.read(reinterpret_cast(out), length); if(m_source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); - got = m_source.gcount(); + got = static_cast(m_source.gcount()); } if(m_source.eof()) diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index 7bd9b842d..e887d6e76 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -51,12 +51,13 @@ uint64_t OS::get_processor_timestamp() #elif defined(BOTAN_USE_GCC_INLINE_ASM) #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) - if(CPUID::has_rdtsc()) // not available on all x86 CPUs - { - uint32_t rtc_low = 0, rtc_high = 0; - asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); - return (static_cast(rtc_high) << 32) | rtc_low; - } + + if(CPUID::has_rdtsc() == false) + return 0; + + uint32_t rtc_low = 0, rtc_high = 0; + asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); + return (static_cast(rtc_high) << 32) | rtc_low; #elif defined(BOTAN_TARGET_ARCH_IS_PPC64) uint32_t rtc_low = 0, rtc_high = 0; @@ -99,11 +100,12 @@ uint64_t OS::get_processor_timestamp() #else //#warning "OS::get_processor_timestamp not implemented" + return 0; #endif -#endif - +#else return 0; +#endif } uint64_t OS::get_high_resolution_clock() diff --git a/src/lib/utils/parsing.cpp b/src/lib/utils/parsing.cpp index 7583767f0..e0173443f 100644 --- a/src/lib/utils/parsing.cpp +++ b/src/lib/utils/parsing.cpp @@ -17,37 +17,40 @@ namespace Botan { +uint16_t to_uint16(const std::string& str) + { + const uint32_t x = to_u32bit(str); + + if(x >> 16) + throw Invalid_Argument("Integer value exceeds 16 bit range"); + + return static_cast(x); + } + uint32_t to_u32bit(const std::string& str) { - try + // std::stoul is not strict enough. Ensure that str is digit only [0-9]* + for(const char chr : str) { - // std::stoul is not strict enough. Ensure that str is digit only [0-9]* - for (const char chr : str) + if(chr < '0' || chr > '9') { - if (chr < '0' || chr > '9') - { - auto chrAsString = std::string(1, chr); - throw Invalid_Argument("String contains non-digit char: " + chrAsString); - } + std::string chrAsString(1, chr); + throw Invalid_Argument("String contains non-digit char: " + chrAsString); } + } - const auto integerValue = std::stoul(str); + const unsigned long int x = std::stoul(str); - // integerValue might be uint64 - if (integerValue > std::numeric_limits::max()) + if(sizeof(unsigned long int) > 4) + { + // x might be uint64 + if (x > std::numeric_limits::max()) { - throw Invalid_Argument("Integer value exceeds 32 bit range: " + std::to_string(integerValue)); + throw Invalid_Argument("Integer value of " + str + " exceeds 32 bit range"); } - - return integerValue; - } - catch(std::exception& e) - { - auto message = std::string("Could not read '" + str + "' as decimal string"); - auto exceptionMessage = std::string(e.what()); - if (!exceptionMessage.empty()) message += ": " + exceptionMessage; - throw Exception(message); } + + return static_cast(x); } /* diff --git a/src/lib/utils/parsing.h b/src/lib/utils/parsing.h index 71f349126..f4936bd68 100644 --- a/src/lib/utils/parsing.h +++ b/src/lib/utils/parsing.h @@ -104,6 +104,13 @@ BOTAN_DLL bool x500_name_cmp(const std::string& name1, */ BOTAN_DLL uint32_t to_u32bit(const std::string& str); +/** +* Convert a string to a number +* @param str the string to convert +* @return number value of the string +*/ +BOTAN_DLL uint16_t to_uint16(const std::string& str); + /** * Convert a time specification to a number * @param timespec the time specification -- cgit v1.2.3