From 0468612ac0bbd3095b160b8a23ba5346d8705095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Thu, 22 Apr 2021 00:48:35 +0200 Subject: [PATCH] Fix conversion warnings for the Network module --- src/SFML/Network/Ftp.cpp | 12 ++++++------ src/SFML/Network/Http.cpp | 4 ++-- src/SFML/Network/IpAddress.cpp | 2 +- src/SFML/Network/Packet.cpp | 8 ++++---- src/SFML/Network/TcpSocket.cpp | 23 ++++++++++++++++------- src/SFML/Network/UdpSocket.cpp | 10 ++++++++-- src/SFML/Network/Unix/SocketImpl.hpp | 1 + src/SFML/Network/Win32/SocketImpl.hpp | 1 + 8 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp index 78df4ebb9..598891db9 100644 --- a/src/SFML/Network/Ftp.cpp +++ b/src/SFML/Network/Ftp.cpp @@ -549,7 +549,7 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode) // Extract the current number while (isdigit(str[index])) { - data[i] = data[i] * 10 + (str[index] - '0'); + data[i] = static_cast(data[i] * 10) + static_cast(str[index] - '0'); index++; } @@ -558,11 +558,11 @@ Ftp::Response Ftp::DataChannel::open(Ftp::TransferMode mode) } // Reconstruct connection port and address - unsigned short port = data[4] * 256 + data[5]; - IpAddress address(static_cast(data[0]), - static_cast(data[1]), - static_cast(data[2]), - static_cast(data[3])); + unsigned short port = static_cast(data[4] * 256) + data[5]; + IpAddress address(data[0], + data[1], + data[2], + data[3]); // Connect the data channel to the server if (m_dataSocket.connect(address, port) == Socket::Done) diff --git a/src/SFML/Network/Http.cpp b/src/SFML/Network/Http.cpp index aa7e58f8a..9e9d0aea8 100644 --- a/src/SFML/Network/Http.cpp +++ b/src/SFML/Network/Http.cpp @@ -208,8 +208,8 @@ void Http::Response::parse(const std::string& data) (toLower(version.substr(0, 5)) == "http/") && isdigit(version[5]) && isdigit(version[7])) { - m_majorVersion = version[5] - '0'; - m_minorVersion = version[7] - '0'; + m_majorVersion = static_cast(version[5] - '0'); + m_minorVersion = static_cast(version[7] - '0'); } else { diff --git a/src/SFML/Network/IpAddress.cpp b/src/SFML/Network/IpAddress.cpp index 0f0e9fb81..2c6bc6b2d 100644 --- a/src/SFML/Network/IpAddress.cpp +++ b/src/SFML/Network/IpAddress.cpp @@ -69,7 +69,7 @@ m_valid (false) //////////////////////////////////////////////////////////// IpAddress::IpAddress(Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3) : -m_address(htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3)), +m_address(htonl(static_cast((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))), m_valid (true) { } diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index 3b638d43f..22315d8b7 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -150,7 +150,7 @@ Packet& Packet::operator >>(Int16& data) if (checkSize(sizeof(data))) { std::memcpy(&data, &m_data[m_readPos], sizeof(data)); - data = ntohs(data); + data = static_cast(ntohs(static_cast(data))); m_readPos += sizeof(data); } @@ -178,7 +178,7 @@ Packet& Packet::operator >>(Int32& data) if (checkSize(sizeof(data))) { std::memcpy(&data, &m_data[m_readPos], sizeof(data)); - data = ntohl(data); + data = static_cast(ntohl(static_cast(data))); m_readPos += sizeof(data); } @@ -412,7 +412,7 @@ Packet& Packet::operator <<(Uint8 data) //////////////////////////////////////////////////////////// Packet& Packet::operator <<(Int16 data) { - Int16 toWrite = htons(data); + Int16 toWrite = static_cast(htons(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } @@ -430,7 +430,7 @@ Packet& Packet::operator <<(Uint16 data) //////////////////////////////////////////////////////////// Packet& Packet::operator <<(Int32 data) { - Int32 toWrite = htonl(data); + Int32 toWrite = static_cast(htonl(static_cast(data))); append(&toWrite, sizeof(toWrite)); return *this; } diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index 3205f80bf..7181df0eb 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -178,7 +178,7 @@ Socket::Status TcpSocket::connect(const IpAddress& remoteAddress, unsigned short time.tv_usec = static_cast(timeout.asMicroseconds() % 1000000); // Wait for something to write on our socket (which means that the connection request has returned) - if (select(static_cast(getHandle() + 1), NULL, &selector, NULL, &time) > 0) + if (select(getHandle() + 1, NULL, &selector, NULL, &time) > 0) { // At this point the connection may have been either accepted or refused. // To know whether it's a success or a failure, we must check the address of the connected peer @@ -242,11 +242,14 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size, std::size_t& } // Loop until every byte has been sent - int result = 0; - for (sent = 0; sent < size; sent += result) + ssize_t result = 0; + for (sent = 0; sent < size; sent += static_cast(result)) { + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wuseless-cast" // Send a chunk of data - result = ::send(getHandle(), static_cast(data) + sent, static_cast(size - sent), flags); + result = ::send(getHandle(), static_cast(data) + sent, static_cast(size - sent), flags); + #pragma GCC diagnostic pop // Check for errors if (result < 0) @@ -277,8 +280,11 @@ Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& rec return Error; } + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wuseless-cast" // Receive a chunk of bytes - int sizeReceived = recv(getHandle(), static_cast(data), static_cast(size), flags); + int sizeReceived = static_cast(recv(getHandle(), static_cast(data), static_cast(size), flags)); + #pragma GCC diagnostic pop // Check the number of bytes received if (sizeReceived > 0) @@ -324,9 +330,12 @@ Socket::Status TcpSocket::send(Packet& packet) if (size > 0) std::memcpy(&blockToSend[0] + sizeof(packetSize), data, size); + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wuseless-cast" // Send the data block std::size_t sent; - Status status = send(&blockToSend[0] + packet.m_sendPos, blockToSend.size() - packet.m_sendPos, sent); + Status status = send(&blockToSend[0] + packet.m_sendPos, static_cast(blockToSend.size() - packet.m_sendPos), sent); + #pragma GCC diagnostic pop // In the case of a partial send, record the location to resume from if (status == Partial) @@ -379,7 +388,7 @@ Socket::Status TcpSocket::receive(Packet& packet) while (m_pendingPacket.Data.size() < packetSize) { // Receive a chunk of data - std::size_t sizeToGet = std::min(static_cast(packetSize - m_pendingPacket.Data.size()), sizeof(buffer)); + std::size_t sizeToGet = std::min(packetSize - m_pendingPacket.Data.size(), sizeof(buffer)); Status status = receive(buffer, sizeToGet, received); if (status != Done) return status; diff --git a/src/SFML/Network/UdpSocket.cpp b/src/SFML/Network/UdpSocket.cpp index a16182374..c90462ccf 100644 --- a/src/SFML/Network/UdpSocket.cpp +++ b/src/SFML/Network/UdpSocket.cpp @@ -113,8 +113,11 @@ Socket::Status UdpSocket::send(const void* data, std::size_t size, const IpAddre // Build the target address sockaddr_in address = priv::SocketImpl::createAddress(remoteAddress.toInteger(), remotePort); + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wuseless-cast" // Send the data (unlike TCP, all the data is always sent in one call) - int sent = sendto(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), sizeof(address)); + int sent = static_cast(sendto(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), sizeof(address))); + #pragma GCC diagnostic pop // Check for errors if (sent < 0) @@ -142,9 +145,12 @@ Socket::Status UdpSocket::receive(void* data, std::size_t size, std::size_t& rec // Data that will be filled with the other computer's address sockaddr_in address = priv::SocketImpl::createAddress(INADDR_ANY, 0); + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wuseless-cast" // Receive a chunk of bytes priv::SocketImpl::AddrLength addressSize = sizeof(address); - int sizeReceived = recvfrom(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), &addressSize); + int sizeReceived = static_cast(recvfrom(getHandle(), static_cast(data), static_cast(size), 0, reinterpret_cast(&address), &addressSize)); + #pragma GCC diagnostic pop // Check for errors if (sizeReceived < 0) diff --git a/src/SFML/Network/Unix/SocketImpl.hpp b/src/SFML/Network/Unix/SocketImpl.hpp index 5e3742a0c..c43fa4016 100644 --- a/src/SFML/Network/Unix/SocketImpl.hpp +++ b/src/SFML/Network/Unix/SocketImpl.hpp @@ -55,6 +55,7 @@ public: // Types //////////////////////////////////////////////////////////// typedef socklen_t AddrLength; + typedef size_t Size; //////////////////////////////////////////////////////////// /// \brief Create an internal sockaddr_in address diff --git a/src/SFML/Network/Win32/SocketImpl.hpp b/src/SFML/Network/Win32/SocketImpl.hpp index 78121110a..3663719e7 100644 --- a/src/SFML/Network/Win32/SocketImpl.hpp +++ b/src/SFML/Network/Win32/SocketImpl.hpp @@ -58,6 +58,7 @@ public: // Types //////////////////////////////////////////////////////////// typedef int AddrLength; + typedef int Size; //////////////////////////////////////////////////////////// /// \brief Create an internal sockaddr_in address