Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed -Wconversion warnings. #180

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ matrix:
include:
- os: osx
compiler: clang
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
- os: osx
compiler: clang
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
- os: osx
compiler: clang
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
- os: linux
compiler: clang
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
addons:
apt:
sources:
Expand All @@ -47,7 +47,7 @@ matrix:
- os: linux
sudo: required
compiler: clang
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CFLAGS="-Werror -g -Wall -Wextra -Wno-ignored-qualifiers -fsanitize=address -fno-omit-frame-pointer" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -fsanitize=address -fno-omit-frame-pointer" LDFLAGS="-Wno-ignored-qualifiers -fsanitize=address" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CFLAGS="-Werror -g -Wall -Wextra -Wno-ignored-qualifiers -fsanitize=address -fno-omit-frame-pointer" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion -fsanitize=address -fno-omit-frame-pointer" LDFLAGS="-Wno-ignored-qualifiers -fsanitize=address" MAKEFLAGS="-j2"
addons:
apt:
sources:
Expand All @@ -67,7 +67,7 @@ matrix:
- boost1.67
- os: linux
compiler: gcc
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=ON -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=ON" CXXFLAGS="-std=c++14 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
addons:
apt:
sources:
Expand All @@ -84,7 +84,7 @@ matrix:
- os: linux
sudo: required
compiler: gcc
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=OFF" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers" MAKEFLAGS="-j2"
env: FLAGS="-DMQTT_NO_TLS=OFF -DMQTT_USE_WS=OFF -DMQTT_USE_STR_CHECK=OFF" CXXFLAGS="-std=c++17 -Werror -g -Wall -Wextra -Wno-ignored-qualifiers -Wconversion" MAKEFLAGS="-j2"
addons:
apt:
sources:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MQTT client/server for C++14 based on Boost.Asio

Version 3.1.0 [![Build Status](https://travis-ci.org/redboltz/mqtt_cpp.svg?branch=master)](https://travis-ci.org/redboltz/mqtt_cpp) [![Build status](https://ci.appveyor.com/api/projects/status/21a267hd34s0kqu5/branch/master?svg=true)](https://ci.appveyor.com/project/redboltz/mqtt-client-cpp/branch/master)
Version 4.0.0 [![Build Status](https://travis-ci.org/redboltz/mqtt_cpp.svg?branch=master)](https://travis-ci.org/redboltz/mqtt_cpp) [![Build status](https://ci.appveyor.com/api/projects/status/21a267hd34s0kqu5/branch/master?svg=true)](https://ci.appveyor.com/project/redboltz/mqtt-client-cpp/branch/master)

## Overview

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 3.1.0.{build}
version: 4.0.0.{build}

image: Visual Studio 2017

Expand Down
4 changes: 2 additions & 2 deletions include/mqtt/connect_flags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ inline constexpr bool has_user_name_flag(char v) {
}

inline void set_will_qos(char& v, std::size_t qos) {
v |= (qos & 0b00000011) << 3;
v |= static_cast<char>((qos & 0b00000011) << 3);
}

inline constexpr char will_qos(char v) {
inline constexpr std::uint8_t will_qos(char v) {
return (v & 0b00011000) >> 3;
}

Expand Down
4 changes: 2 additions & 2 deletions include/mqtt/control_packet_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ constexpr std::uint8_t const disconnect = 14;
} // namespace control_packet_type

inline
constexpr std::uint8_t get_control_packet_type(char v) {
return static_cast<std::uint8_t>(v) >> 4;
constexpr std::uint8_t get_control_packet_type(std::uint8_t v) {
return static_cast<std::uint8_t>(v >> 4);
}

} // namespace mqtt
Expand Down
22 changes: 12 additions & 10 deletions include/mqtt/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4079,7 +4079,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, Loc
LockGuard<Mutex> lck (store_mtx_);
if (packet_id_.size() == std::numeric_limits<packet_id_t>::max()) throw packet_id_exhausted_error();
if (packet_id_master_ == std::numeric_limits<packet_id_t>::max()) {
packet_id_master_ = 1;
packet_id_master_ = 1U;
}
else {
++packet_id_master_;
Expand All @@ -4092,24 +4092,24 @@ class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, Loc
--last;

if (*last != std::numeric_limits<packet_id_t>::max()) {
packet_id_master_ = *last + 1;
packet_id_master_ = static_cast<packet_id_t>(*last + 1U);
packet_id_.insert(e, packet_id_master_);
return packet_id_master_;
}

auto b = packet_id_.begin();
auto prev = *b;
if (prev != 1) {
packet_id_master_ = 1;
if (prev != 1U) {
packet_id_master_ = 1U;
packet_id_.insert(b, packet_id_master_);
return packet_id_master_;
}
++b;
while (*b - 1 == prev && b != e) {
while (*b - 1U == prev && b != e) {
prev = *b;
++b;
}
packet_id_master_ = prev + 1;
packet_id_master_ = static_cast<packet_id_t>(prev + 1U);
packet_id_.insert(b, packet_id_master_);
return packet_id_master_;
}
Expand Down Expand Up @@ -4151,7 +4151,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, Loc
restore_serialized_message(packet_id_t /*packet_id*/, Iterator b, Iterator e) {
if (b == e) return;

auto fixed_header = *b;
auto fixed_header = static_cast<std::uint8_t>(*b);
switch (get_control_packet_type(fixed_header)) {
case control_packet_type::publish: {
auto sp = std::make_shared<basic_publish_message<PacketIdBytes>>(b, e);
Expand Down Expand Up @@ -5386,7 +5386,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, Loc
results.push_back(boost::none);
}
else {
results.push_back(*it);
results.push_back(static_cast<std::uint8_t>(*it));
}
}
if (h_suback_) return h_suback_(packet_id, std::move(results));
Expand Down Expand Up @@ -5992,8 +5992,10 @@ class endpoint : public std::enable_shared_from_this<endpoint<Socket, Mutex, Loc

static std::uint16_t make_uint16_t(char b1, char b2) {
return
((static_cast<std::uint16_t>(b1) & 0xff)) << 8 |
(static_cast<std::uint16_t>(b2) & 0xff);
static_cast<std::uint16_t>(
((static_cast<std::uint16_t>(b1) & 0xff)) << 8 |
(static_cast<std::uint16_t>(b2) & 0xff)
);
}

struct write_completion_handler {
Expand Down
6 changes: 3 additions & 3 deletions include/mqtt/fixed_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
namespace mqtt {

inline constexpr
char make_fixed_header(std::uint8_t type, std::uint8_t flags) {
std::uint8_t make_fixed_header(std::uint8_t type, std::uint8_t flags) {
return
static_cast<char>(
(static_cast<std::uint8_t>(type) << 4) |
static_cast<std::uint8_t>(
(type << 4) |
(flags & 0x0f)
);
}
Expand Down
10 changes: 6 additions & 4 deletions include/mqtt/four_byte_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ std::uint32_t make_uint32_t(It b, It e) {
auto b3 = b++;
auto b4 = b++;
return
(static_cast<std::uint16_t>(*b1) & 0xff) << 24 |
(static_cast<std::uint16_t>(*b2) & 0xff) << 16 |
(static_cast<std::uint16_t>(*b3) & 0xff) << 8 |
(static_cast<std::uint16_t>(*b4) & 0xff);
static_cast<std::uint32_t>(
(static_cast<std::uint16_t>(*b1) & 0xff) << 24 |
(static_cast<std::uint16_t>(*b2) & 0xff) << 16 |
(static_cast<std::uint16_t>(*b3) & 0xff) << 8 |
(static_cast<std::uint16_t>(*b4) & 0xff)
);
}

} // namespace mqtt
Expand Down
34 changes: 17 additions & 17 deletions include/mqtt/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class header_only_message {
* @brief Create empty header_packet_id_message.
*/
header_only_message(std::uint8_t type, std::uint8_t flags)
: message_ { make_fixed_header(type, flags), 0 }
: message_ { static_cast<char>(make_fixed_header(type, flags)), 0 }
{}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ class basic_header_packet_id_message {
* @brief Create empty header_packet_id_message.
*/
basic_header_packet_id_message(std::uint8_t type, std::uint8_t flags, typename packet_id_type<PacketIdBytes>::type packet_id)
: message_ { make_fixed_header(type, flags), PacketIdBytes }
: message_ { static_cast<char>(make_fixed_header(type, flags)), PacketIdBytes }
{
add_packet_id_to_buf<PacketIdBytes>::apply(message_, packet_id);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ class connack_message {
public:
connack_message(bool session_present, std::uint8_t return_code)
: message_ {
make_fixed_header(control_packet_type::connack, 0b0000),
static_cast<char>(make_fixed_header(control_packet_type::connack, 0b0000)),
0b0010,
static_cast<char>(session_present ? 1 : 0),
static_cast<char>(return_code)
Expand Down Expand Up @@ -283,7 +283,7 @@ class connect_message {
boost::optional<std::string> const& user_name,
boost::optional<std::string> const& password
)
: fixed_header_(make_fixed_header(control_packet_type::connect, 0b0000)),
: fixed_header_(static_cast<char>(make_fixed_header(control_packet_type::connect, 0b0000))),
connect_flags_(0),
// protocol name length, protocol name, protocol level, connect flag, client id length, client id, keep alive
remaining_length_(
Expand Down Expand Up @@ -413,7 +413,7 @@ class connect_message {

ret.reserve(size());

ret.push_back(fixed_header_);
ret.push_back(static_cast<char>(fixed_header_));
ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
ret.append(protocol_name_and_level_.data(), protocol_name_and_level_.size());
ret.push_back(connect_flags_);
Expand Down Expand Up @@ -477,7 +477,7 @@ class basic_publish_message {
typename packet_id_type<PacketIdBytes>::type packet_id,
as::const_buffer const& payload
)
: fixed_header_(make_fixed_header(control_packet_type::publish, 0b0000)),
: fixed_header_(static_cast<char>(make_fixed_header(control_packet_type::publish, 0b0000))),
topic_name_(topic_name),
topic_name_length_buf_ { MQTT_16BITNUM_TO_BYTE_SEQ(get_size(topic_name)) },
payload_(payload),
Expand All @@ -502,14 +502,14 @@ class basic_publish_message {
template <typename Iterator>
basic_publish_message(Iterator b, Iterator e) {
if (b >= e) throw remaining_length_error();
fixed_header_ = *b;
fixed_header_ = static_cast<std::uint8_t>(*b);
auto qos = publish::get_qos(fixed_header_);
++b;

if (b + 4 >= e) throw remaining_length_error();
auto len_consumed = remaining_length(b, b + 4);
remaining_length_ = std::get<0>(len_consumed);
auto consumed = std::get<1>(len_consumed);
auto consumed = static_cast<std::string::difference_type>(std::get<1>(len_consumed));

std::copy(b, b + consumed, std::back_inserter(remaining_length_buf_));
b += consumed;
Expand Down Expand Up @@ -538,7 +538,7 @@ class basic_publish_message {
break;
};

payload_ = as::buffer(&*b, std::distance(b, e));
payload_ = as::buffer(&*b, static_cast<std::size_t>(std::distance(b, e)));
}

/**
Expand Down Expand Up @@ -589,7 +589,7 @@ class basic_publish_message {

ret.reserve(size());

ret.push_back(fixed_header_);
ret.push_back(static_cast<char>(fixed_header_));
ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());

ret.append(topic_name_length_buf_.data(), topic_name_length_buf_.size());
Expand Down Expand Up @@ -678,7 +678,7 @@ class basic_publish_message {
}

private:
char fixed_header_;
std::uint8_t fixed_header_;
as::const_buffer topic_name_;
boost::container::static_vector<char, 2> topic_name_length_buf_;
boost::container::static_vector<char, PacketIdBytes> packet_id_;
Expand Down Expand Up @@ -710,7 +710,7 @@ class basic_subscribe_message {
std::vector<std::tuple<as::const_buffer, std::uint8_t>> const& params,
typename packet_id_type<PacketIdBytes>::type packet_id
)
: fixed_header_(static_cast<char>(make_fixed_header(control_packet_type::subscribe, 0b0010))),
: fixed_header_(static_cast<char>(static_cast<char>(make_fixed_header(control_packet_type::subscribe, 0b0010)))),
remaining_length_(PacketIdBytes)
{
add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
Expand Down Expand Up @@ -780,15 +780,15 @@ class basic_subscribe_message {

ret.reserve(size());

ret.push_back(fixed_header_);
ret.push_back(static_cast<char>(fixed_header_));
ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());

ret.append(packet_id_.data(), packet_id_.size());

for (auto const& e : entries_) {
ret.append(e.topic_name_length_buf.data(), e.topic_name_length_buf.size());
ret.append(get_pointer(e.topic_name), get_size(e.topic_name));
ret.push_back(e.qos);
ret.push_back(static_cast<char>(e.qos));
}


Expand All @@ -812,7 +812,7 @@ class basic_suback_message {
std::vector<std::uint8_t> const& params,
typename packet_id_type<PacketIdBytes>::type packet_id
)
: fixed_header_(static_cast<char>(make_fixed_header(control_packet_type::suback, 0b0000))),
: fixed_header_(static_cast<char>(static_cast<char>(make_fixed_header(control_packet_type::suback, 0b0000)))),
remaining_length_(params.size() + PacketIdBytes)
{
add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
Expand Down Expand Up @@ -862,7 +862,7 @@ class basic_suback_message {

ret.reserve(size());

ret.push_back(fixed_header_);
ret.push_back(static_cast<char>(fixed_header_));
ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());

ret.append(packet_id_.data(), packet_id_.size());
Expand Down Expand Up @@ -963,7 +963,7 @@ class basic_unsubscribe_message {
std::string ret;
ret.reserve(size());

ret.push_back(fixed_header_);
ret.push_back(static_cast<char>(fixed_header_));

ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());

Expand Down
22 changes: 12 additions & 10 deletions include/mqtt/publish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,35 @@ namespace mqtt {
namespace publish {

inline
constexpr bool is_dup(char v) {
constexpr bool is_dup(std::uint8_t v) {
return (v & 0b00001000) != 0;
}

inline
constexpr std::uint8_t get_qos(char v) {
return static_cast<std::uint8_t>(v & 0b00000110) >> 1;
constexpr std::uint8_t get_qos(std::uint8_t v) {
return (v & 0b00000110) >> 1;
}

constexpr bool is_retain(char v) {
inline
constexpr bool is_retain(std::uint8_t v) {
return (v & 0b00000001) != 0;
}

inline
constexpr void set_dup(char& fixed_header, bool dup) {
constexpr void set_dup(std::uint8_t& fixed_header, bool dup) {
if (dup) fixed_header |= 0b00001000;
else fixed_header &= ~0b00001000;
else fixed_header &= static_cast<std::uint8_t>(~0b00001000);
}

inline
constexpr void set_qos(char& fixed_header, std::uint8_t qos) {
fixed_header |= qos << 1;
constexpr void set_qos(std::uint8_t& fixed_header, std::uint8_t qos) {
fixed_header |= static_cast<std::uint8_t>(qos << 1);
}

constexpr void set_retain(char& fixed_header, bool retain) {
inline
constexpr void set_retain(std::uint8_t& fixed_header, bool retain) {
if (retain) fixed_header |= 0b00000001;
else fixed_header &= ~0b00000001;
else fixed_header &= static_cast<std::uint8_t>(~0b00000001);
}

} // namespace publish
Expand Down
6 changes: 3 additions & 3 deletions include/mqtt/remaining_length.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace mqtt {

inline std::string
remaining_bytes(std::size_t size) {
if (size > 0xfffffff) throw remaining_length_error();
if (size > 0xfffffffU) throw remaining_length_error();
std::string bytes;
while (size > 127) {
bytes.push_back((size & 0b01111111) | 0b10000000);
while (size > 127U) {
bytes.push_back(static_cast<char>((size & 0b01111111) | 0b10000000));
size >>= 7;
}
bytes.push_back(size & 0b01111111);
Expand Down