From f8040f480308b5dacc0a5ff81912282e45cf6de1 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 25 Oct 2019 06:00:36 +0200 Subject: [PATCH] Make type conversions explicit. --- .../native-wrapper/remwharead_wrapper.cpp | 2 +- src/lib/uri.cpp | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp index 060e7ea..172f2b1 100644 --- a/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp +++ b/browser-plugins/webextension/native-wrapper/remwharead_wrapper.cpp @@ -55,7 +55,7 @@ const string read_input() void send_message(const string &message) { - const uint32_t length = message.length() + 2; + const uint32_t length = static_cast(message.length() + 2); cout.write(reinterpret_cast(&length), sizeof(uint32_t)); cout << '"' << message << '"'; } diff --git a/src/lib/uri.cpp b/src/lib/uri.cpp index bb2cf18..a976718 100644 --- a/src/lib/uri.cpp +++ b/src/lib/uri.cpp @@ -45,6 +45,7 @@ using std::vector; using std::cerr; using std::endl; using std::move; +using std::uint32_t; using Poco::Net::HTTPClientSession; using Poco::Net::HTTPSClientSession; using Poco::Net::HTTPRequest; @@ -92,12 +93,13 @@ void URI::set_proxy() proxy.host = matches[3]; if (!matches[4].empty()) { - const std::uint32_t &port = std::stoul(matches[4]); + // NOLINTNEXTLINE(google-runtime-lint) - Need to use same as stoul. + const unsigned long port = std::stoul(matches[4]); if (port > 65535) { throw std::invalid_argument("Proxy port number out of range"); } - proxy.port = port; + proxy.port = static_cast(port); } HTTPClientSession::setGlobalProxyConfig(proxy); } @@ -331,11 +333,11 @@ string URI::unescape_html(string html) // 'x' in front of the number means it's hexadecimal, else decimal. if (matches[1].length != 0) { - codepoint = std::stoi(number, nullptr, 16); + codepoint = static_cast(std::stoul(number, nullptr, 16)); } else { - codepoint = std::stoi(number, nullptr, 10); + codepoint = static_cast(std::stoi(number, nullptr, 10)); } const string unicode = u8c.to_bytes(codepoint); html.replace(matches[0].offset, matches[0].length, unicode); @@ -664,8 +666,14 @@ string URI::cut_text(const string &text, const uint16_t n_chars) const { constexpr char suffix[] = " […]"; constexpr auto suffix_len = std::end(suffix) - std::begin(suffix) - 1; + if (n_chars <= suffix_len) + { + throw std::invalid_argument("n_chars has to be greater than " + + std::to_string(suffix_len)); + } - const size_t pos = text.rfind(' ', n_chars - suffix_len); + const size_t pos = + text.rfind(' ', static_cast(n_chars - suffix_len)); return text.substr(0, pos) + suffix; }