From c8774d464c0bff4e2dc9ff7ea35a51e5ea8416a4 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Sun, 6 Oct 2024 22:39:04 +0000 Subject: [PATCH] refactor: nuke a whole load of C style casts from orbit --- include/dpp/utility.h | 2 +- src/dpp/discordvoiceclient.cpp | 1 - src/dpp/utility.cpp | 4 ++-- src/dpp/voice/enabled/discover_ip.cpp | 8 ++++---- src/dpp/voice/enabled/handle_frame.cpp | 8 ++++---- src/dpp/voice/enabled/opus.cpp | 12 ++++++------ src/dpp/voice/enabled/read_ready.cpp | 4 ++-- src/dpp/voice/enabled/read_write.cpp | 15 +++++++++++---- src/dpp/voice/enabled/thread.cpp | 3 --- src/dpp/voice/enabled/voice_payload.cpp | 20 -------------------- src/dpp/voice/enabled/write_ready.cpp | 2 +- 11 files changed, 31 insertions(+), 48 deletions(-) diff --git a/include/dpp/utility.h b/include/dpp/utility.h index f78d3b5b9e..0dcb974972 100644 --- a/include/dpp/utility.h +++ b/include/dpp/utility.h @@ -736,7 +736,7 @@ uint32_t DPP_EXPORT hsl(int h, int s, int l); * @param data The start of the data to display * @param length The length of data to display */ -std::string DPP_EXPORT debug_dump(uint8_t* data, size_t length); +std::string DPP_EXPORT debug_dump(const uint8_t* data, size_t length); /** * @brief Returns the length of a UTF-8 string in codepoints. diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 16f7e05f3f..0f171112b5 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -115,7 +115,6 @@ void discord_voice_client::get_user_privacy_code(const dpp::snowflake user, priv return; } mls_state->dave_session->get_pairwise_fingerprint(0x0000, user.str(), [callback](const std::vector &data) { - std::cout << dpp::utility::debug_dump((uint8_t *) data.data(), data.size()); callback(data.size() == 64 ? generate_displayable_code(data, 45) : ""); }); #else diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index e9c811535c..b41be262fa 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -355,7 +355,7 @@ image_data&& icon::as_image_data() && { return std::move(std::get(hash_or_data)); } -std::string debug_dump(uint8_t* data, size_t length) { +std::string debug_dump(const uint8_t* data, size_t length) { std::ostringstream out; size_t addr = (size_t)data; size_t extra = addr % 16; @@ -367,7 +367,7 @@ std::string debug_dump(uint8_t* data, size_t length) { out << "-- "; } std::string ascii; - for (uint8_t* ptr = data; ptr < data + length; ++ptr) { + for (const uint8_t* ptr = data; ptr < data + length; ++ptr) { if (((size_t)ptr % 16) == 0) { out << ascii << "\n[" << to_hex((size_t)ptr) << "] : "; ascii.clear(); diff --git a/src/dpp/voice/enabled/discover_ip.cpp b/src/dpp/voice/enabled/discover_ip.cpp index 0f923078ef..29339a4ec4 100644 --- a/src/dpp/voice/enabled/discover_ip.cpp +++ b/src/dpp/voice/enabled/discover_ip.cpp @@ -91,7 +91,7 @@ std::string discord_voice_client::discover_ip() { servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(0); - if (bind(socket.fd, (const sockaddr*)(&servaddr), sizeof(servaddr)) < 0) { + if (bind(socket.fd, reinterpret_cast(&servaddr), sizeof(servaddr)) < 0) { log(ll_warning, "Could not bind socket for IP discovery"); return ""; } @@ -99,11 +99,11 @@ std::string discord_voice_client::discover_ip() { servaddr.sin_family = AF_INET; servaddr.sin_port = htons(this->port); servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str()); - if (::connect(socket.fd, (const sockaddr*)(&servaddr), sizeof(sockaddr_in)) < 0) { + if (::connect(socket.fd, reinterpret_cast(&servaddr), sizeof(sockaddr_in)) < 0) { log(ll_warning, "Could not connect socket for IP discovery"); return ""; } - if (::send(socket.fd, (const char*)&discovery, sizeof(discovery), 0) == -1) { + if (::send(socket.fd, reinterpret_cast(&discovery), sizeof(discovery), 0) == -1) { log(ll_warning, "Could not send packet for IP discovery"); return ""; } @@ -120,7 +120,7 @@ std::string discord_voice_client::discover_ip() { log(ll_warning, "Timed out in IP discovery"); return ""; default: - if (recv(socket.fd, (char*)&discovery, sizeof(discovery), 0) == -1) { + if (recv(socket.fd, reinterpret_cast(&discovery), sizeof(discovery), 0) == -1) { log(ll_warning, "Could not receive packet for IP discovery"); return ""; } diff --git a/src/dpp/voice/enabled/handle_frame.cpp b/src/dpp/voice/enabled/handle_frame.cpp index 86ae682cc5..e3e61f777f 100644 --- a/src/dpp/voice/enabled/handle_frame.cpp +++ b/src/dpp/voice/enabled/handle_frame.cpp @@ -121,7 +121,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod break; default: log(ll_debug, "Unexpected DAVE frame opcode"); - log(dpp::ll_trace, "R: " + dpp::utility::debug_dump((uint8_t*)(data.data()), data.length())); + log(dpp::ll_trace, "R: " + dpp::utility::debug_dump(reinterpret_cast(data.data()), data.length())); break; } @@ -390,7 +390,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod } log(ll_debug, "Voice websocket established; UDP endpoint: " + ip + ":" + std::to_string(port) + " [ssrc=" + std::to_string(ssrc) + "] with " + std::to_string(modes.size()) + " modes"); - dpp::socket newfd; + dpp::socket newfd = 0; if ((newfd = ::socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { sockaddr_in servaddr{}; @@ -399,7 +399,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(0); - if (bind(newfd, (sockaddr*)&servaddr, sizeof(servaddr)) < 0) { + if (bind(newfd, reinterpret_cast(&servaddr), sizeof(servaddr)) < 0) { throw dpp::connection_exception(err_bind_failure, "Can't bind() client UDP socket"); } @@ -417,7 +417,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod int bound_port = 0; sockaddr_in sin{}; socklen_t len = sizeof(sin); - if (getsockname(this->fd, (sockaddr *)&sin, &len) > -1) { + if (getsockname(this->fd, reinterpret_cast(&sin), &len) > -1) { bound_port = ntohs(sin.sin_port); } diff --git a/src/dpp/voice/enabled/opus.cpp b/src/dpp/voice/enabled/opus.cpp index a24de61ef8..066cacdb53 100644 --- a/src/dpp/voice/enabled/opus.cpp +++ b/src/dpp/voice/enabled/opus.cpp @@ -43,7 +43,7 @@ discord_voice_client& discord_voice_client::send_audio_raw(uint16_t* audio_data, } if (length > send_audio_raw_max_length) { - std::string s_audio_data((const char*)audio_data, length); + std::string s_audio_data(reinterpret_cast(audio_data), length); while (s_audio_data.length() > send_audio_raw_max_length) { std::string packet(s_audio_data.substr(0, send_audio_raw_max_length)); @@ -51,23 +51,23 @@ discord_voice_client& discord_voice_client::send_audio_raw(uint16_t* audio_data, s_audio_data.erase(s_audio_data.begin(), s_audio_data.begin() + packet_size); - send_audio_raw((uint16_t*)packet.data(), packet_size); + send_audio_raw(reinterpret_cast(packet.data()), packet_size); } return *this; } if (length < send_audio_raw_max_length) { - std::string packet((const char*)audio_data, length); + std::string packet(reinterpret_cast(audio_data), length); packet.resize(send_audio_raw_max_length, 0); - return send_audio_raw((uint16_t*)packet.data(), packet.size()); + return send_audio_raw(reinterpret_cast(packet.data()), packet.size()); } opus_int32 encoded_audio_max_length = (opus_int32)length; std::vector encoded_audio(encoded_audio_max_length); size_t encoded_audio_length = encoded_audio_max_length; - encoded_audio_length = this->encode((uint8_t*)audio_data, length, encoded_audio.data(), encoded_audio_length); + encoded_audio_length = this->encode(reinterpret_cast(audio_data), length, encoded_audio.data(), encoded_audio_length); send_audio_opus(encoded_audio.data(), encoded_audio_length); return *this; } @@ -172,7 +172,7 @@ size_t discord_voice_client::encode(uint8_t *input, size_t inDataSize, uint8_t * return outDataSize; } for (size_t i = 0; i < (inDataSize / mEncFrameBytes); ++ i) { - const opus_int16* pcm = (opus_int16*)(input + i * mEncFrameBytes); + const opus_int16* pcm = reinterpret_cast(input + i * mEncFrameBytes); int ret = opus_encode(encoder, pcm, mEncFrameSize, out, 65536); if (ret > 0) { int retval = opus_repacketizer_cat(repacketizer, out, ret); diff --git a/src/dpp/voice/enabled/read_ready.cpp b/src/dpp/voice/enabled/read_ready.cpp index 8645ab7a73..51a817fbed 100644 --- a/src/dpp/voice/enabled/read_ready.cpp +++ b/src/dpp/voice/enabled/read_ready.cpp @@ -35,7 +35,7 @@ namespace dpp { void discord_voice_client::read_ready() { uint8_t buffer[65535]; - int packet_size = this->udp_recv((char*)buffer, sizeof(buffer)); + int packet_size = this->udp_recv(reinterpret_cast(buffer), sizeof(buffer)); bool receive_handler_is_empty = creator->on_voice_receive.empty() && creator->on_voice_receive_combined.empty(); if (packet_size <= 0 || receive_handler_is_empty) { @@ -67,7 +67,7 @@ void discord_voice_client::read_ready() voice_payload vp{0, // seq, populate later 0, // timestamp, populate later - std::make_unique(nullptr, std::string((char*)buffer, packet_size))}; + std::make_unique(nullptr, std::string(reinterpret_cast(buffer), packet_size))}; vp.vr->voice_client = this; diff --git a/src/dpp/voice/enabled/read_write.cpp b/src/dpp/voice/enabled/read_write.cpp index 97ea0efaa8..1702e6cf13 100644 --- a/src/dpp/voice/enabled/read_write.cpp +++ b/src/dpp/voice/enabled/read_write.cpp @@ -32,9 +32,9 @@ dpp::socket discord_voice_client::want_write() { std::lock_guard lock(this->stream_mutex); if (!this->paused && !outbuf.empty()) { return fd; - } else { - return INVALID_SOCKET; } + return INVALID_SOCKET; + } dpp::socket discord_voice_client::want_read() { @@ -56,12 +56,19 @@ int discord_voice_client::udp_send(const char* data, size_t length) { servaddr.sin_family = AF_INET; servaddr.sin_port = htons(this->port); servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str()); - return (int) sendto(this->fd, data, (int)length, 0, (const sockaddr*)&servaddr, (int)sizeof(sockaddr_in)); + return static_cast(sendto( + this->fd, + data, + static_cast(length), + 0, + reinterpret_cast(&servaddr), + static_cast(sizeof(sockaddr_in)) + )); } int discord_voice_client::udp_recv(char* data, size_t max_length) { - return (int) recv(this->fd, data, (int)max_length, 0); + return static_cast(recv(this->fd, data, static_cast(max_length), 0)); } } \ No newline at end of file diff --git a/src/dpp/voice/enabled/thread.cpp b/src/dpp/voice/enabled/thread.cpp index be07d795c8..0082eb0a72 100644 --- a/src/dpp/voice/enabled/thread.cpp +++ b/src/dpp/voice/enabled/thread.cpp @@ -24,10 +24,7 @@ #include #include #include - -#include #include "../../dave/encryptor.h" - #include "enabled.h" namespace dpp { diff --git a/src/dpp/voice/enabled/voice_payload.cpp b/src/dpp/voice/enabled/voice_payload.cpp index 05f3b5686f..afc871a3d7 100644 --- a/src/dpp/voice/enabled/voice_payload.cpp +++ b/src/dpp/voice/enabled/voice_payload.cpp @@ -20,30 +20,10 @@ * ************************************************************************************/ -#ifdef _WIN32 -#include - #include - #include -#else - #include - #include - #include -#endif -#include -#include #include -#include -#include #include #include #include -#include - -#include -#include -#include "../../dave/session.h" -#include "../../dave/decryptor.h" -#include "../../dave/encryptor.h" namespace dpp { diff --git a/src/dpp/voice/enabled/write_ready.cpp b/src/dpp/voice/enabled/write_ready.cpp index f5a94deb5e..46c0307055 100644 --- a/src/dpp/voice/enabled/write_ready.cpp +++ b/src/dpp/voice/enabled/write_ready.cpp @@ -39,7 +39,7 @@ void discord_voice_client::write_ready() { std::lock_guard lock(this->stream_mutex); if (!this->paused && outbuf.size()) { type = send_audio_type; - if (outbuf[0].packet.size() == sizeof(uint16_t) && (*((uint16_t*)(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) { + if (outbuf[0].packet.size() == sizeof(uint16_t) && (*(reinterpret_cast(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) { outbuf.erase(outbuf.begin()); track_marker_found = true; if (tracks > 0) {