diff options
author | Jack Lloyd <[email protected]> | 2017-08-31 07:13:58 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-08-31 07:13:58 -0400 |
commit | df4287c3c763de14b262ed39d54b3de552adbefb (patch) | |
tree | 10b839bab8d2e36a806951bb2e5e5f0212f7a0f9 /src/lib/utils | |
parent | 17ef09021892afc4c0e9fe7ba97423bf832e6dc5 (diff) |
Fix various MSVC warnings
Based on VC2017 output
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/ct_utils.h | 2 | ||||
-rw-r--r-- | src/lib/utils/data_src.cpp | 8 | ||||
-rw-r--r-- | src/lib/utils/os_utils.cpp | 18 | ||||
-rw-r--r-- | src/lib/utils/parsing.cpp | 45 | ||||
-rw-r--r-- | src/lib/utils/parsing.h | 7 |
5 files changed, 46 insertions, 34 deletions
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<typename T> inline T is_equal(T x, T y) { - return is_zero(x ^ y); + return is_zero<T>(x ^ y); } template<typename T> 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<size_t>(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<size_t>(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<char*>(buf.data()), buf.size()); if(m_source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); - got = m_source.gcount(); + got = static_cast<size_t>(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<char*>(out), length); if(m_source.bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); - got = m_source.gcount(); + got = static_cast<size_t>(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<uint64_t>(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<uint64_t>(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<uint16_t>(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<uint32_t>::max()) + if(sizeof(unsigned long int) > 4) + { + // x might be uint64 + if (x > std::numeric_limits<uint32_t>::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<uint32_t>(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 @@ -105,6 +105,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 * @return number of seconds represented by timespec |