From f8aceb6c37fcfa0c76a048a6354dff7d2a25b074 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Tue, 6 May 2025 22:26:12 +0200 Subject: [PATCH] Codechange: use value initialisation over memset --- src/cheat.cpp | 2 +- src/cheat_type.h | 22 +++++++++++----------- src/network/core/address.cpp | 3 +-- src/network/core/address.h | 11 ++++------- src/network/core/tcp_connect.cpp | 3 +-- src/network/core/tcp_listen.h | 3 +-- src/network/core/udp.cpp | 3 +-- src/os/macosx/crashlog_osx.cpp | 3 +-- src/os/unix/crashlog_unix.cpp | 3 +-- src/os/windows/crashlog_win.cpp | 3 +-- src/saveload/saveload.cpp | 6 ++---- src/screenshot_pcx.cpp | 4 +--- src/screenshot_png.cpp | 3 +-- src/textfile_gui.cpp | 3 +-- src/video/win32_v.cpp | 4 +--- 15 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/cheat.cpp b/src/cheat.cpp index 30adc36fbe..71546382de 100644 --- a/src/cheat.cpp +++ b/src/cheat.cpp @@ -18,5 +18,5 @@ Cheats _cheats; /** Reinitialise all the cheats. */ void InitializeCheats() { - memset(&_cheats, 0, sizeof(Cheats)); + _cheats = {}; } diff --git a/src/cheat_type.h b/src/cheat_type.h index 8c5dc981b1..a13cab7eb2 100644 --- a/src/cheat_type.h +++ b/src/cheat_type.h @@ -14,8 +14,8 @@ * Info about each of the cheats. */ struct Cheat { - bool been_used; ///< has this cheat been used before? - bool value; ///< tells if the bool cheat is active or not + bool been_used = false; ///< has this cheat been used before? + bool value = false; ///< tells if the bool cheat is active or not }; /** @@ -24,15 +24,15 @@ struct Cheat { * Only add new entries at the end of the struct! */ struct Cheats { - Cheat magic_bulldozer; ///< dynamite industries, objects - Cheat switch_company; ///< change to another company - Cheat money; ///< get rich or poor - Cheat crossing_tunnels; ///< allow tunnels that cross each other - Cheat no_jetcrash; ///< no jet will crash on small airports anymore - Cheat change_date; ///< changes date ingame - Cheat setup_prod; ///< setup raw-material production in game - Cheat edit_max_hl; ///< edit the maximum heightlevel; this is a cheat because of the fact that it needs to reset NewGRF game state and doing so as a simple configuration breaks the expectation of many - Cheat station_rating; ///< Fix station ratings at 100% + Cheat magic_bulldozer{}; ///< dynamite industries, objects + Cheat switch_company{}; ///< change to another company + Cheat money{}; ///< get rich or poor + Cheat crossing_tunnels{}; ///< allow tunnels that cross each other + Cheat no_jetcrash{}; ///< no jet will crash on small airports anymore + Cheat change_date{}; ///< changes date ingame + Cheat setup_prod{}; ///< setup raw-material production in game + Cheat edit_max_hl{}; ///< edit the maximum heightlevel; this is a cheat because of the fact that it needs to reset NewGRF game state and doing so as a simple configuration breaks the expectation of many + Cheat station_rating{}; ///< Fix station ratings at 100% }; extern Cheats _cheats; diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index ea898bed71..03936edcb2 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -202,8 +202,7 @@ bool NetworkAddress::IsInNetmask(std::string_view netmask) SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func) { struct addrinfo *ai; - struct addrinfo hints; - memset(&hints, 0, sizeof (hints)); + struct addrinfo hints{}; hints.ai_family = family; hints.ai_flags = flags; hints.ai_socktype = socktype; diff --git a/src/network/core/address.h b/src/network/core/address.h index 8d73084495..5263fdacdb 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -27,10 +27,10 @@ using SocketList = std::map; ///< Type for a mapping */ class NetworkAddress { private: - std::string hostname; ///< The hostname - int address_length; ///< The length of the resolved address - sockaddr_storage address; ///< The resolved address - bool resolved; ///< Whether the address has been (tried to be) resolved + std::string hostname{}; ///< The hostname + int address_length{}; ///< The length of the resolved address + sockaddr_storage address{}; ///< The resolved address + bool resolved = false; ///< Whether the address has been (tried to be) resolved /** * Helper function to resolve something to a socket. @@ -62,7 +62,6 @@ public: address_length(address_length), resolved(address_length != 0) { - memset(&this->address, 0, sizeof(this->address)); memcpy(&this->address, address, address_length); } @@ -81,8 +80,6 @@ public: hostname.remove_suffix(1); } this->hostname = hostname; - - memset(&this->address, 0, sizeof(this->address)); this->address.ss_family = family; this->SetPort(port); } diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index 44a4f550d1..8b0bd647c9 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -222,8 +222,7 @@ void TCPConnecter::Resolve() /* Port is already guaranteed part of the connection_string. */ NetworkAddress address = ParseConnectionString(this->connection_string, 0); - addrinfo hints; - memset(&hints, 0, sizeof(hints)); + addrinfo hints{}; hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h index b2a7b85ee4..7e38734e6d 100644 --- a/src/network/core/tcp_listen.h +++ b/src/network/core/tcp_listen.h @@ -76,8 +76,7 @@ public: static void AcceptClient(SOCKET ls) { for (;;) { - struct sockaddr_storage sin; - memset(&sin, 0, sizeof(sin)); + struct sockaddr_storage sin{}; socklen_t sin_len = sizeof(sin); SOCKET s = accept(ls, (struct sockaddr*)&sin, &sin_len); if (s == INVALID_SOCKET) return; diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index 45900d1118..000e650411 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -113,8 +113,7 @@ void NetworkUDPSocketHandler::ReceivePackets() { for (auto &s : this->sockets) { for (int i = 0; i < 1000; i++) { // Do not infinitely loop when DoSing with UDP - struct sockaddr_storage client_addr; - memset(&client_addr, 0, sizeof(client_addr)); + struct sockaddr_storage client_addr{}; /* The limit is UDP_MTU, but also allocate that much as we need to read the whole packet in one go. */ Packet p(this, UDP_MTU, UDP_MTU); diff --git a/src/os/macosx/crashlog_osx.cpp b/src/os/macosx/crashlog_osx.cpp index 50dd97f7b1..c22f602286 100644 --- a/src/os/macosx/crashlog_osx.cpp +++ b/src/os/macosx/crashlog_osx.cpp @@ -159,8 +159,7 @@ static sigset_t SetSignals(void(*handler)(int)) sigaddset(&sigs, signum); } - struct sigaction sa; - memset(&sa, 0, sizeof(sa)); + struct sigaction sa{}; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); diff --git a/src/os/unix/crashlog_unix.cpp b/src/os/unix/crashlog_unix.cpp index d60ddb79f8..9ec7d70a5e 100644 --- a/src/os/unix/crashlog_unix.cpp +++ b/src/os/unix/crashlog_unix.cpp @@ -146,8 +146,7 @@ static sigset_t SetSignals(void(*handler)(int)) sigaddset(&sigs, signum); } - struct sigaction sa; - memset(&sa, 0, sizeof(sa)); + struct sigaction sa{}; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index 0f609d89e2..4ffd2442a9 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -212,8 +212,7 @@ static const uint MAX_FRAMES = 64; proc.pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_UNDNAME); /* Initialize starting stack frame from context record. */ - STACKFRAME64 frame; - memset(&frame, 0, sizeof(frame)); + STACKFRAME64 frame{}; #ifdef _M_AMD64 frame.AddrPC.Offset = ep->ContextRecord->Rip; frame.AddrFrame.Offset = ep->ContextRecord->Rbp; diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 2bf80a0a02..761f2c4db4 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2557,7 +2557,7 @@ struct NoCompSaveFilter : SaveFilter { /** Filter using Zlib compression. */ struct ZlibLoadFilter : LoadFilter { - z_stream z; ///< Stream state we are reading from. + z_stream z{}; ///< Stream state we are reading from. uint8_t fread_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for reading from the file. /** @@ -2566,7 +2566,6 @@ struct ZlibLoadFilter : LoadFilter { */ ZlibLoadFilter(std::shared_ptr chain) : LoadFilter(std::move(chain)) { - memset(&this->z, 0, sizeof(this->z)); if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor"); } @@ -2601,7 +2600,7 @@ struct ZlibLoadFilter : LoadFilter { /** Filter using Zlib compression. */ struct ZlibSaveFilter : SaveFilter { - z_stream z; ///< Stream state we are writing to. + z_stream z{}; ///< Stream state we are writing to. uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for writing to the file. /** @@ -2611,7 +2610,6 @@ struct ZlibSaveFilter : SaveFilter { */ ZlibSaveFilter(std::shared_ptr chain, uint8_t compression_level) : SaveFilter(std::move(chain)) { - memset(&this->z, 0, sizeof(this->z)); if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor"); } diff --git a/src/screenshot_pcx.cpp b/src/screenshot_pcx.cpp index 20eb22ec32..7e9b1f89d3 100644 --- a/src/screenshot_pcx.cpp +++ b/src/screenshot_pcx.cpp @@ -45,7 +45,6 @@ public: { uint maxlines; uint y; - PcxHeader pcx; bool success; if (pixelformat == 32) { @@ -58,9 +57,8 @@ public: if (!of.has_value()) return false; auto &f = *of; - memset(&pcx, 0, sizeof(pcx)); - /* setup pcx header */ + PcxHeader pcx{}; pcx.manufacturer = 10; pcx.version = 5; pcx.rle = 1; diff --git a/src/screenshot_png.cpp b/src/screenshot_png.cpp index 2756869043..9c526de1e4 100644 --- a/src/screenshot_png.cpp +++ b/src/screenshot_png.cpp @@ -75,8 +75,7 @@ public: /* Try to add some game metadata to the PNG screenshot so * it's more useful for debugging and archival purposes. */ - png_text_struct text[2]; - memset(text, 0, sizeof(text)); + png_text_struct text[2]{}; text[0].key = const_cast("Software"); text[0].text = const_cast(_openttd_revision.c_str()); text[0].text_length = _openttd_revision.size(); diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index 59490fece4..94900220f7 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -680,8 +680,7 @@ static std::vector Gunzip(std::span input) static const int BLOCKSIZE = 8192; std::vector output; - z_stream z; - memset(&z, 0, sizeof(z)); + z_stream z{}; z.next_in = reinterpret_cast(input.data()); z.avail_in = static_cast(input.size()); diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 1928074d6f..798a2dc475 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -152,9 +152,7 @@ bool VideoDriver_Win32Base::MakeWindow(bool full_screen, bool resize) } if (full_screen) { - DEVMODE settings; - - memset(&settings, 0, sizeof(settings)); + DEVMODE settings{}; settings.dmSize = sizeof(settings); settings.dmFields = DM_BITSPERPEL |