From 002cd1d46173cab5758bd6d91cbd918e22072492 Mon Sep 17 00:00:00 2001 From: vittorioromeo Date: Thu, 3 Oct 2024 19:01:21 +0200 Subject: [PATCH] Cppcheck fixes part 1 --- examples/X11/X11.cpp | 4 +-- examples/sound_effects/SoundEffects.cpp | 2 -- include/SFML/Network/SocketSelector.hpp | 6 ++-- include/SFML/System/Utf.inl | 10 +++--- src/SFML/Audio/SoundBuffer.cpp | 9 +++-- src/SFML/Audio/SoundRecorder.cpp | 3 +- src/SFML/Graphics/Image.cpp | 4 +-- src/SFML/Main/MainAndroid.cpp | 6 ++-- src/SFML/Network/Ftp.cpp | 5 ++- src/SFML/Network/SocketSelector.cpp | 6 ++-- src/SFML/Window/DRM/DRMContext.cpp | 40 ++++++++++------------- src/SFML/Window/EglContext.cpp | 4 ++- src/SFML/Window/Unix/ClipboardImpl.cpp | 25 +++++++------- src/SFML/Window/Unix/ClipboardImpl.hpp | 2 +- src/SFML/Window/Unix/WindowImplX11.cpp | 6 ++-- src/SFML/Window/Win32/WindowImplWin32.cpp | 2 +- test/Network/SocketSelector.test.cpp | 2 +- 17 files changed, 65 insertions(+), 71 deletions(-) diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index 18ae46093..44545fa92 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -21,7 +21,7 @@ /// \return True if operation was successful, false otherwise /// //////////////////////////////////////////////////////////// -[[nodiscard]] bool initialize(sf::Window& window) +[[nodiscard]] bool initialize(const sf::Window& window) { // Activate the window if (!window.setActive()) @@ -73,7 +73,7 @@ /// \return True if operation was successful, false otherwise /// //////////////////////////////////////////////////////////// -[[nodiscard]] bool draw(sf::Window& window, float elapsedTime) +[[nodiscard]] bool draw(const sf::Window& window, float elapsedTime) { // Activate the window if (!window.setActive()) diff --git a/examples/sound_effects/SoundEffects.cpp b/examples/sound_effects/SoundEffects.cpp index 45926f872..2bbbc2f1d 100644 --- a/examples/sound_effects/SoundEffects.cpp +++ b/examples/sound_effects/SoundEffects.cpp @@ -732,8 +732,6 @@ protected: float a2{}; float b1{}; float b2{}; - float c0{}; - float d0{}; }; using Processing::Processing; diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index 8b8f93948..54b5c405f 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -100,7 +100,7 @@ public: /// \see `remove`, `clear` /// //////////////////////////////////////////////////////////// - void add(Socket& socket); + void add(const Socket& socket); //////////////////////////////////////////////////////////// /// \brief Remove a socket from the selector @@ -113,7 +113,7 @@ public: /// \see `add`, `clear` /// //////////////////////////////////////////////////////////// - void remove(Socket& socket); + void remove(const Socket& socket); //////////////////////////////////////////////////////////// /// \brief Remove all the sockets stored in the selector @@ -162,7 +162,7 @@ public: /// \see `isReady` /// //////////////////////////////////////////////////////////// - [[nodiscard]] bool isReady(Socket& socket) const; + [[nodiscard]] bool isReady(const Socket& socket) const; private: struct SocketSelectorImpl; diff --git a/include/SFML/System/Utf.inl b/include/SFML/System/Utf.inl index 124283db9..546620383 100644 --- a/include/SFML/System/Utf.inl +++ b/include/SFML/System/Utf.inl @@ -27,6 +27,8 @@ //////////////////////////////////////////////////////////// #include // NOLINT(misc-header-include-cycle) +#include + //////////////////////////////////////////////////////////// // References: @@ -127,10 +129,10 @@ Out Utf<8>::encode(std::uint32_t input, Out output, std::uint8_t replacement) std::size_t bytestoWrite = 1; // clang-format off - if (input < 0x80) bytestoWrite = 1; - else if (input < 0x800) bytestoWrite = 2; - else if (input < 0x10000) bytestoWrite = 3; - else if (input <= 0x0010FFFF) bytestoWrite = 4; + if (input < 0x80) { bytestoWrite = 1; } + else if (input < 0x800) { bytestoWrite = 2; } + else if (input < 0x10000) { bytestoWrite = 3; } + else { assert(input <= 0x0010FFFF); bytestoWrite = 4; } // clang-format on // Extract the bytes to write diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index 4774fa3cc..9d19f5d02 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -77,12 +77,11 @@ SoundBuffer::SoundBuffer(const std::int16_t* samples, //////////////////////////////////////////////////////////// -SoundBuffer::SoundBuffer(const SoundBuffer& copy) +SoundBuffer::SoundBuffer(const SoundBuffer& copy) : +// don't copy the attached sounds +m_samples(copy.m_samples), +m_duration(copy.m_duration) { - // don't copy the attached sounds - m_samples = copy.m_samples; - m_duration = copy.m_duration; - // Update the internal buffer with the new samples if (!update(copy.getChannelCount(), copy.getSampleRate(), copy.getChannelMap())) err() << "Failed to update copy-constructed sound buffer" << std::endl; diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index c6c1839c3..5fda38c74 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -421,8 +421,9 @@ void SoundRecorder::setChannelCount(unsigned int channelCount) { m_impl->channelMap = {SoundChannel::Mono}; } - else if (channelCount == 2) + else { + assert(channelCount == 2); m_impl->channelMap = {SoundChannel::FrontLeft, SoundChannel::FrontRight}; } } diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 251aa71e1..3457c6c84 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -422,8 +422,8 @@ void Image::createMaskFromColor(Color color, std::uint8_t alpha) if (!m_pixels.empty()) { // Replace the alpha of the pixels that match the transparent color - std::uint8_t* ptr = m_pixels.data(); - std::uint8_t* end = ptr + m_pixels.size(); + std::uint8_t* ptr = m_pixels.data(); + const std::uint8_t* end = ptr + m_pixels.size(); while (ptr < end) { if ((ptr[0] == color.r) && (ptr[1] == color.g) && (ptr[2] == color.b) && (ptr[3] == color.a)) diff --git a/src/SFML/Main/MainAndroid.cpp b/src/SFML/Main/MainAndroid.cpp index 91d1a6a82..a5f632c71 100644 --- a/src/SFML/Main/MainAndroid.cpp +++ b/src/SFML/Main/MainAndroid.cpp @@ -305,7 +305,7 @@ void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) // Wait for the event to be taken into account by SFML states.updated = false; - while (!(states.updated | states.terminated)) + while (!(states.updated || states.terminated)) { states.mutex.unlock(); sf::sleep(sf::milliseconds(10)); @@ -327,7 +327,7 @@ void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* /* window // Wait for the event to be taken into account by SFML states.updated = false; - while (!(states.updated | states.terminated)) + while (!(states.updated || states.terminated)) { states.mutex.unlock(); sf::sleep(sf::milliseconds(10)); @@ -512,7 +512,7 @@ JNIEXPORT void ANativeActivity_onCreate(ANativeActivity* activity, void* savedSt // Wait for the main thread to be initialized states->mutex.lock(); - while (!(states->initialized | states->terminated)) + while (!(states->initialized || states->terminated)) { states->mutex.unlock(); sf::sleep(sf::milliseconds(20)); diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp index 28d4b38d7..1b9547053 100644 --- a/src/SFML/Network/Ftp.cpp +++ b/src/SFML/Network/Ftp.cpp @@ -606,8 +606,7 @@ void Ftp::DataChannel::receive(std::ostream& stream) void Ftp::DataChannel::send(std::istream& stream) { // Send data - char buffer[1024]; - std::size_t count = 0; + char buffer[1024]; for (;;) { @@ -620,7 +619,7 @@ void Ftp::DataChannel::send(std::istream& stream) break; } - count = static_cast(stream.gcount()); + const auto count = static_cast(stream.gcount()); if (count > 0) { diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index 3ff8eed41..5cf93fd8e 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -88,7 +88,7 @@ SocketSelector& SocketSelector::operator=(SocketSelector&&) noexcept = default; //////////////////////////////////////////////////////////// -void SocketSelector::add(Socket& socket) +void SocketSelector::add(const Socket& socket) { const SocketHandle handle = socket.getNativeHandle(); if (handle != priv::SocketImpl::invalidSocket()) @@ -130,7 +130,7 @@ void SocketSelector::add(Socket& socket) //////////////////////////////////////////////////////////// -void SocketSelector::remove(Socket& socket) +void SocketSelector::remove(const Socket& socket) { const SocketHandle handle = socket.getNativeHandle(); if (handle != priv::SocketImpl::invalidSocket()) @@ -187,7 +187,7 @@ bool SocketSelector::wait(Time timeout) //////////////////////////////////////////////////////////// -bool SocketSelector::isReady(Socket& socket) const +bool SocketSelector::isReady(const Socket& socket) const { const SocketHandle handle = socket.getNativeHandle(); if (handle != priv::SocketImpl::invalidSocket()) diff --git a/src/SFML/Window/DRM/DRMContext.cpp b/src/SFML/Window/DRM/DRMContext.cpp index 071a69d5d..2e7b5f6cc 100644 --- a/src/SFML/Window/DRM/DRMContext.cpp +++ b/src/SFML/Window/DRM/DRMContext.cpp @@ -445,13 +445,13 @@ void checkInit() deviceString = nullptr; // Use environment variable "SFML_DRM_MODE" (or nullptr if not set) - char* modeString = std::getenv("SFML_DRM_MODE"); + const char* modeString = std::getenv("SFML_DRM_MODE"); // Use environment variable "SFML_DRM_REFRESH" (or 0 if not set) // Use in combination with mode to request specific refresh rate for the mode // if multiple refresh rates for same mode might be supported unsigned int refreshRate = 0; - char* refreshString = std::getenv("SFML_DRM_REFRESH"); + const char* refreshString = std::getenv("SFML_DRM_REFRESH"); if (refreshString) refreshRate = static_cast(atoi(refreshString)); @@ -513,15 +513,13 @@ EGLDisplay getInitializedDisplay() namespace sf::priv { //////////////////////////////////////////////////////////// -DRMContext::DRMContext(DRMContext* shared) +DRMContext::DRMContext(DRMContext* shared) : +// Get the initialized EGL display +m_display(getInitializedDisplay()), +// Get the best EGL config matching the default video settings +m_config(getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, ContextSettings{})) { contextCount++; - - // Get the initialized EGL display - m_display = getInitializedDisplay(); - - // Get the best EGL config matching the default video settings - m_config = getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, ContextSettings{}); updateSettings(); // Create EGL context @@ -535,15 +533,13 @@ DRMContext::DRMContext(DRMContext* shared) //////////////////////////////////////////////////////////// -DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, const WindowImpl& owner, unsigned int bitsPerPixel) +DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, const WindowImpl& owner, unsigned int bitsPerPixel) : +// Get the initialized EGL display +m_display(getInitializedDisplay()), +// Get the best EGL config matching the requested video settings +m_config(getBestConfig(m_display, bitsPerPixel, settings)) { contextCount++; - - // Get the initialized EGL display - m_display = getInitializedDisplay(); - - // Get the best EGL config matching the requested video settings - m_config = getBestConfig(m_display, bitsPerPixel, settings); updateSettings(); // Create EGL context @@ -555,15 +551,13 @@ DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, cons //////////////////////////////////////////////////////////// -DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, Vector2u size) +DRMContext::DRMContext(DRMContext* shared, const ContextSettings& settings, Vector2u size) : +// Get the initialized EGL display +m_display(getInitializedDisplay()), +// Get the best EGL config matching the requested video settings +m_config(getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, settings)) { contextCount++; - - // Get the initialized EGL display - m_display = getInitializedDisplay(); - - // Get the best EGL config matching the requested video settings - m_config = getBestConfig(m_display, VideoMode::getDesktopMode().bitsPerPixel, settings); updateSettings(); // Create EGL context diff --git a/src/SFML/Window/EglContext.cpp b/src/SFML/Window/EglContext.cpp index e11d7ebfe..1113d975e 100644 --- a/src/SFML/Window/EglContext.cpp +++ b/src/SFML/Window/EglContext.cpp @@ -67,7 +67,7 @@ EGLDisplay getInitializedDisplay() return states.display; -#endif +#else static EGLDisplay display = EGL_NO_DISPLAY; @@ -78,6 +78,8 @@ EGLDisplay getInitializedDisplay() } return display; + +#endif } diff --git a/src/SFML/Window/Unix/ClipboardImpl.cpp b/src/SFML/Window/Unix/ClipboardImpl.cpp index de60cce31..d0410b63b 100644 --- a/src/SFML/Window/Unix/ClipboardImpl.cpp +++ b/src/SFML/Window/Unix/ClipboardImpl.cpp @@ -74,21 +74,18 @@ void ClipboardImpl::processEvents() //////////////////////////////////////////////////////////// -ClipboardImpl::ClipboardImpl() +ClipboardImpl::ClipboardImpl() : +// Open a connection with the X server +m_display(openDisplay()), +// Create a hidden window that will broker our clipboard interactions with X +m_window(XCreateSimpleWindow(m_display.get(), DefaultRootWindow(m_display.get()), 0, 0, 1, 1, 0, 0, 0)), +// Get the atoms we need to make use of the clipboard +m_clipboard(getAtom("CLIPBOARD", false)), +m_targets(getAtom("TARGETS", false)), +m_text(getAtom("TEXT", false)), +m_utf8String(getAtom("UTF8_STRING", true)), +m_targetProperty(getAtom("SFML_CLIPBOARD_TARGET_PROPERTY", false)) { - // Open a connection with the X server - m_display = openDisplay(); - - // Get the atoms we need to make use of the clipboard - m_clipboard = getAtom("CLIPBOARD", false); - m_targets = getAtom("TARGETS", false); - m_text = getAtom("TEXT", false); - m_utf8String = getAtom("UTF8_STRING", true); - m_targetProperty = getAtom("SFML_CLIPBOARD_TARGET_PROPERTY", false); - - // Create a hidden window that will broker our clipboard interactions with X - m_window = XCreateSimpleWindow(m_display.get(), DefaultRootWindow(m_display.get()), 0, 0, 1, 1, 0, 0, 0); - // Register the events we are interested in XSelectInput(m_display.get(), m_window, SelectionNotify | SelectionClear | SelectionRequest); } diff --git a/src/SFML/Window/Unix/ClipboardImpl.hpp b/src/SFML/Window/Unix/ClipboardImpl.hpp index eddbe94ff..9c334fa19 100644 --- a/src/SFML/Window/Unix/ClipboardImpl.hpp +++ b/src/SFML/Window/Unix/ClipboardImpl.hpp @@ -135,8 +135,8 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - ::Window m_window{}; ///< X identifier defining our window std::shared_ptr<::Display> m_display; ///< Pointer to the display + ::Window m_window; ///< X identifier defining our window Atom m_clipboard; ///< X Atom identifying the CLIPBOARD selection Atom m_targets; ///< X Atom identifying TARGETS Atom m_text; ///< X Atom identifying TEXT diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 03adf679d..da71bfe90 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -343,7 +343,7 @@ bool getEWMHFrameExtents(::Display* disp, ::Window win, long& xFrameExtent, long #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-align" - long* extents = reinterpret_cast(data); + const long* extents = reinterpret_cast(data); #pragma GCC diagnostic pop xFrameExtent = extents[0]; // Left. @@ -1178,7 +1178,7 @@ void WindowImplX11::requestFocus() { const std::lock_guard lock(allWindowsMutex); - for (sf::priv::WindowImplX11* windowPtr : allWindows) + for (const sf::priv::WindowImplX11* windowPtr : allWindows) { if (windowPtr->hasFocus()) { @@ -1693,6 +1693,7 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) XSetICFocus(m_inputContext); // Grab cursor + // TODO: ??? if (m_cursorGrabbed) { // Try multiple times to grab the cursor @@ -1718,6 +1719,7 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) sf::sleep(sf::milliseconds(50)); } + // TODO: ??? if (!m_cursorGrabbed) err() << "Failed to grab mouse cursor" << std::endl; } diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index f89945949..706e88531 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -1127,7 +1127,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) if ((wParam == DBT_DEVICEARRIVAL) || (wParam == DBT_DEVICEREMOVECOMPLETE)) { // Some sort of device change has happened, update joystick connections if it is a device interface - auto* deviceBroadcastHeader = reinterpret_cast(lParam); + const auto* deviceBroadcastHeader = reinterpret_cast(lParam); if (deviceBroadcastHeader && (deviceBroadcastHeader->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) JoystickImpl::updateConnections(); diff --git a/test/Network/SocketSelector.test.cpp b/test/Network/SocketSelector.test.cpp index 4d3ea46ad..67b88b595 100644 --- a/test/Network/SocketSelector.test.cpp +++ b/test/Network/SocketSelector.test.cpp @@ -17,7 +17,7 @@ TEST_CASE("[Network] sf::SocketSelector") STATIC_CHECK(std::is_nothrow_move_assignable_v); } - sf::UdpSocket socket; + const sf::UdpSocket socket; SECTION("Construction") {