From d6e1961112c29eee7f60128c29ed699795505167 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 11 Apr 2024 12:40:15 -0600 Subject: [PATCH] Ensure struct data members are given an initial value While I don't suspect there are any uninitialize variable bugs present, it's still good to err on the side of safety and provide an initial value nonetheless. --- examples/island/Island.cpp | 8 +++--- examples/shader/Effect.hpp | 2 +- examples/shader/Shader.cpp | 2 -- include/SFML/Audio/SoundStream.hpp | 4 +-- include/SFML/Graphics/RenderTarget.hpp | 24 +++++++++--------- include/SFML/Graphics/StencilMode.hpp | 2 +- include/SFML/Network/Packet.hpp | 4 +-- src/SFML/Network/SocketSelector.cpp | 8 +++--- src/SFML/Window/DRM/DRMContext.cpp | 8 +++--- src/SFML/Window/DRM/DRMContext.hpp | 14 +++++------ src/SFML/Window/GlContext.cpp | 2 +- src/SFML/Window/Unix/WindowImplX11.cpp | 17 +++++-------- src/SFML/Window/Win32/JoystickImpl.cpp | 10 ++++---- src/SFML/Window/Win32/WglContext.cpp | 12 ++++----- src/SFML/Window/WindowImpl.cpp | 3 ++- .../Window/macOS/SFKeyboardModifiersHelper.mm | 18 ++++++------- test/Audio/SoundStream.test.cpp | 25 +++++++++++++++---- 17 files changed, 86 insertions(+), 77 deletions(-) diff --git a/examples/island/Island.cpp b/examples/island/Island.cpp index fe83f99d5..3edc39ff7 100644 --- a/examples/island/Island.cpp +++ b/examples/island/Island.cpp @@ -37,8 +37,8 @@ const unsigned int blockCount = 32; struct WorkItem { - sf::Vertex* targetBuffer; - unsigned int index; + sf::Vertex* targetBuffer{}; + unsigned int index{}; }; std::deque workQueue; @@ -50,8 +50,8 @@ std::recursive_mutex workQueueMutex; struct Setting { - const char* name; - float* value; + const char* name{}; + float* value{}; }; // Terrain noise parameters diff --git a/examples/shader/Effect.hpp b/examples/shader/Effect.hpp index e3b6b1730..ed2e8f965 100644 --- a/examples/shader/Effect.hpp +++ b/examples/shader/Effect.hpp @@ -74,5 +74,5 @@ private: bool m_isLoaded{}; // NOLINTNEXTLINE(readability-identifier-naming) - static const sf::Font* s_font; + static inline const sf::Font* s_font{}; }; diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index df3f24287..66aba8d34 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -21,8 +21,6 @@ std::random_device rd; std::mt19937 rng(rd()); } // namespace -const sf::Font* Effect::s_font = nullptr; - //////////////////////////////////////////////////////////// // "Pixelate" fragment shader //////////////////////////////////////////////////////////// diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index e39c73cfb..7049d6c65 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -55,8 +55,8 @@ public: //////////////////////////////////////////////////////////// struct Chunk { - const std::int16_t* samples; //!< Pointer to the audio samples - std::size_t sampleCount; //!< Number of samples pointed by Samples + const std::int16_t* samples{}; //!< Pointer to the audio samples + std::size_t sampleCount{}; //!< Number of samples pointed by Samples }; //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/RenderTarget.hpp b/include/SFML/Graphics/RenderTarget.hpp index 3c04c8d1f..bad236a51 100644 --- a/include/SFML/Graphics/RenderTarget.hpp +++ b/include/SFML/Graphics/RenderTarget.hpp @@ -547,18 +547,18 @@ private: //////////////////////////////////////////////////////////// struct StatesCache { - bool enable; //!< Is the cache enabled? - bool glStatesSet{}; //!< Are our internal GL states set yet? - bool viewChanged; //!< Has the current view changed since last draw? - bool scissorEnabled; //!< Is scissor testing enabled? - bool stencilEnabled; //!< Is stencil testing enabled? - BlendMode lastBlendMode; //!< Cached blending mode - StencilMode lastStencilMode; //!< Cached stencil - std::uint64_t lastTextureId; //!< Cached texture - CoordinateType lastCoordinateType; //!< Texture coordinate type - bool texCoordsArrayEnabled; //!< Is GL_TEXTURE_COORD_ARRAY client state enabled? - bool useVertexCache; //!< Did we previously use the vertex cache? - std::array vertexCache; //!< Pre-transformed vertices cache + bool enable{}; //!< Is the cache enabled? + bool glStatesSet{}; //!< Are our internal GL states set yet? + bool viewChanged{}; //!< Has the current view changed since last draw? + bool scissorEnabled{}; //!< Is scissor testing enabled? + bool stencilEnabled{}; //!< Is stencil testing enabled? + BlendMode lastBlendMode; //!< Cached blending mode + StencilMode lastStencilMode; //!< Cached stencil + std::uint64_t lastTextureId{}; //!< Cached texture + CoordinateType lastCoordinateType{}; //!< Texture coordinate type + bool texCoordsArrayEnabled{}; //!< Is GL_TEXTURE_COORD_ARRAY client state enabled? + bool useVertexCache{}; //!< Did we previously use the vertex cache? + std::array vertexCache{}; //!< Pre-transformed vertices cache }; //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/StencilMode.hpp b/include/SFML/Graphics/StencilMode.hpp index 1c31b264b..d40e010db 100644 --- a/include/SFML/Graphics/StencilMode.hpp +++ b/include/SFML/Graphics/StencilMode.hpp @@ -96,7 +96,7 @@ struct SFML_GRAPHICS_API StencilValue template StencilValue(T) = delete; - unsigned int value = 0u; //!< The stored stencil value + unsigned int value{}; //!< The stored stencil value }; //////////////////////////////////////////////////////////// diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp index 2c0715f74..6cf2226fa 100644 --- a/include/SFML/Network/Packet.hpp +++ b/include/SFML/Network/Packet.hpp @@ -497,8 +497,8 @@ private: /// \code /// struct MyStruct /// { -/// float number; -/// std::int8_t integer; +/// float number{}; +/// std::int8_t integer{}; /// std::string str; /// }; /// diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index 69d4d354b..86c750fcf 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -45,10 +45,10 @@ namespace sf //////////////////////////////////////////////////////////// struct SocketSelector::SocketSelectorImpl { - fd_set allSockets; //!< Set containing all the sockets handles - fd_set socketsReady; //!< Set containing handles of the sockets that are ready - int maxSocket; //!< Maximum socket handle - int socketCount; //!< Number of socket handles + fd_set allSockets{}; //!< Set containing all the sockets handles + fd_set socketsReady{}; //!< Set containing handles of the sockets that are ready + int maxSocket{}; //!< Maximum socket handle + int socketCount{}; //!< Number of socket handles }; diff --git a/src/SFML/Window/DRM/DRMContext.cpp b/src/SFML/Window/DRM/DRMContext.cpp index 084dba2bf..4b8b8863c 100644 --- a/src/SFML/Window/DRM/DRMContext.cpp +++ b/src/SFML/Window/DRM/DRMContext.cpp @@ -53,14 +53,14 @@ namespace { struct DrmFb { - gbm_bo* bo; - std::uint32_t fbId; + gbm_bo* bo{}; + std::uint32_t fbId{}; }; bool initialized = false; sf::priv::Drm drmNode; -drmEventContext drmEventCtx; -pollfd pollFD; +drmEventContext drmEventCtx{}; +pollfd pollFD{}; gbm_device* gbmDevice = nullptr; int contextCount = 0; EGLDisplay display = EGL_NO_DISPLAY; diff --git a/src/SFML/Window/DRM/DRMContext.hpp b/src/SFML/Window/DRM/DRMContext.hpp index 1d02a1bfc..ccfe2f1b7 100644 --- a/src/SFML/Window/DRM/DRMContext.hpp +++ b/src/SFML/Window/DRM/DRMContext.hpp @@ -43,16 +43,16 @@ namespace sf::priv { struct Drm { - int fileDescriptor; + int fileDescriptor{}; - drmModeModeInfoPtr mode; - std::uint32_t crtcId; - std::uint32_t connectorId; + drmModeModeInfoPtr mode{}; + std::uint32_t crtcId{}; + std::uint32_t connectorId{}; - drmModeCrtcPtr originalCrtc; + drmModeCrtcPtr originalCrtc{}; - drmModeConnectorPtr savedConnector; - drmModeEncoderPtr savedEncoder; + drmModeConnectorPtr savedConnector{}; + drmModeEncoderPtr savedEncoder{}; }; class WindowImplDRM; diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index dd920b60e..d250210fd 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -425,7 +425,7 @@ struct GlContext::Impl // Structure to track which unshared object belongs to which context struct UnsharedGlObject { - std::uint64_t contextId; + std::uint64_t contextId{}; std::shared_ptr object; }; diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index b4205dacb..871273634 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -564,17 +564,12 @@ m_cursorGrabbed(m_fullscreen) struct WMHints { - unsigned long flags; - unsigned long functions; - unsigned long decorations; - long inputMode; - unsigned long state; - }; - - auto hints = WMHints(); - hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS; - hints.decorations = 0; - hints.functions = 0; + unsigned long flags{MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS}; + unsigned long functions{}; + unsigned long decorations{}; + long inputMode{}; + unsigned long state{}; + } hints; if (style & Style::Titlebar) { diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 1ad975d55..fcf2a8dff 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -79,9 +79,9 @@ IDirectInput8W* directInput = nullptr; struct JoystickRecord { - GUID guid; - unsigned int index; - bool plugged; + GUID guid{}; + unsigned int index{}; + bool plugged{}; }; using JoystickList = std::vector; @@ -89,8 +89,8 @@ JoystickList joystickList; struct JoystickBlacklistEntry { - unsigned int vendorId; - unsigned int productId; + unsigned int vendorId{}; + unsigned int productId{}; }; using JoystickBlacklist = std::vector; diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index 5eabc4a5b..981ea8199 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -286,12 +286,12 @@ int WglContext::selectBestPixelFormat(HDC deviceContext, unsigned int bitsPerPix // we can cache the result of the lookup instead of having to perform it multiple times for the same inputs struct PixelFormatCacheEntry { - unsigned int bitsPerPixel; - unsigned int depthBits; - unsigned int stencilBits; - unsigned int antialiasingLevel; - bool pbuffer; - int bestFormat; + unsigned int bitsPerPixel{}; + unsigned int depthBits{}; + unsigned int stencilBits{}; + unsigned int antialiasingLevel{}; + bool pbuffer{}; + int bestFormat{}; }; static std::mutex cacheMutex; diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index 6ac230fc5..ffaccd2be 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -96,9 +96,10 @@ namespace sf::priv //////////////////////////////////////////////////////////// struct WindowImpl::JoystickStatesImpl { - JoystickState states[Joystick::Count]; //!< Previous state of the joysticks + JoystickState states[Joystick::Count]{}; //!< Previous state of the joysticks }; + //////////////////////////////////////////////////////////// std::unique_ptr WindowImpl::create( VideoMode mode, diff --git a/src/SFML/Window/macOS/SFKeyboardModifiersHelper.mm b/src/SFML/Window/macOS/SFKeyboardModifiersHelper.mm index f6cebe195..546be9354 100644 --- a/src/SFML/Window/macOS/SFKeyboardModifiersHelper.mm +++ b/src/SFML/Window/macOS/SFKeyboardModifiersHelper.mm @@ -54,15 +54,15 @@ /// Modifiers states struct ModifiersState { - BOOL rightShiftWasDown; - BOOL leftShiftWasDown; - BOOL rightCommandWasDown; - BOOL leftCommandWasDown; - BOOL rightAlternateWasDown; - BOOL leftAlternateWasDown; - BOOL leftControlWasDown; - BOOL rightControlWasDown; - BOOL capsLockWasOn; + BOOL rightShiftWasDown{}; + BOOL leftShiftWasDown{}; + BOOL rightCommandWasDown{}; + BOOL leftCommandWasDown{}; + BOOL rightAlternateWasDown{}; + BOOL leftAlternateWasDown{}; + BOOL leftControlWasDown{}; + BOOL rightControlWasDown{}; + BOOL capsLockWasOn{}; }; diff --git a/test/Audio/SoundStream.test.cpp b/test/Audio/SoundStream.test.cpp index 4438ccb0d..aca5e5525 100644 --- a/test/Audio/SoundStream.test.cpp +++ b/test/Audio/SoundStream.test.cpp @@ -1,9 +1,24 @@ #include +#include + #include -static_assert(!std::is_constructible_v); -static_assert(!std::is_copy_constructible_v); -static_assert(!std::is_copy_assignable_v); -static_assert(!std::is_nothrow_move_constructible_v); -static_assert(!std::is_nothrow_move_assignable_v); +TEST_CASE("[Audio] sf::SoundStream") +{ + SECTION("Type traits") + { + STATIC_CHECK(!std::is_constructible_v); + STATIC_CHECK(!std::is_copy_constructible_v); + STATIC_CHECK(!std::is_copy_assignable_v); + STATIC_CHECK(!std::is_nothrow_move_constructible_v); + STATIC_CHECK(!std::is_nothrow_move_assignable_v); + } + + SECTION("Chunk") + { + const sf::SoundStream::Chunk chunk; + CHECK(chunk.samples == nullptr); + CHECK(chunk.sampleCount == 0); + } +}