From 3ff1d251a8c3ce23c091da776db6f396b474c346 Mon Sep 17 00:00:00 2001 From: binary1248 Date: Tue, 23 Feb 2016 03:07:12 +0100 Subject: [PATCH] Fixed issues reported by Coverity Scan static analysis (run using Linux build configuration). --- examples/sound/Sound.cpp | 5 ++- examples/sound_capture/SoundCapture.cpp | 3 +- src/SFML/Graphics/ImageLoader.cpp | 45 ++++++++++++++++-------- src/SFML/Network/Http.cpp | 3 +- src/SFML/Network/Socket.cpp | 7 ++++ src/SFML/Network/Unix/SocketImpl.cpp | 12 +++++-- src/SFML/System/FileInputStream.cpp | 4 ++- src/SFML/System/Unix/ThreadLocalImpl.cpp | 3 +- src/SFML/Window/Unix/GlxContext.cpp | 2 +- src/SFML/Window/Unix/JoystickImpl.cpp | 36 ++++++++++++------- src/SFML/Window/Unix/JoystickImpl.hpp | 6 ++++ src/SFML/Window/Unix/WindowImplX11.cpp | 6 ++-- src/SFML/Window/Unix/WindowImplX11.hpp | 2 +- 13 files changed, 93 insertions(+), 41 deletions(-) diff --git a/examples/sound/Sound.cpp b/examples/sound/Sound.cpp index b579fb573..e71aa0d09 100644 --- a/examples/sound/Sound.cpp +++ b/examples/sound/Sound.cpp @@ -3,7 +3,6 @@ // Headers //////////////////////////////////////////////////////////// #include -#include #include #include @@ -36,7 +35,7 @@ void playSound() sf::sleep(sf::milliseconds(100)); // Display the playing position - std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.getPlayingOffset().asSeconds() << " sec "; + std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec "; std::cout << std::flush; } std::cout << std::endl << std::endl; @@ -70,7 +69,7 @@ void playMusic(const std::string& filename) sf::sleep(sf::milliseconds(100)); // Display the playing position - std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.getPlayingOffset().asSeconds() << " sec "; + std::cout << "\rPlaying... " << music.getPlayingOffset().asSeconds() << " sec "; std::cout << std::flush; } std::cout << std::endl << std::endl; diff --git a/examples/sound_capture/SoundCapture.cpp b/examples/sound_capture/SoundCapture.cpp index 02f2db2ac..19f114b46 100644 --- a/examples/sound_capture/SoundCapture.cpp +++ b/examples/sound_capture/SoundCapture.cpp @@ -3,7 +3,6 @@ // Headers //////////////////////////////////////////////////////////// #include -#include #include @@ -76,7 +75,7 @@ int main() while (sound.getStatus() == sf::Sound::Playing) { // Display the playing position - std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.getPlayingOffset().asSeconds() << " sec"; + std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec "; std::cout << std::flush; // Leave some CPU time for other threads diff --git a/src/SFML/Graphics/ImageLoader.cpp b/src/SFML/Graphics/ImageLoader.cpp index d42be523a..439ed0a4b 100644 --- a/src/SFML/Graphics/ImageLoader.cpp +++ b/src/SFML/Graphics/ImageLoader.cpp @@ -103,18 +103,23 @@ bool ImageLoader::loadImageFromFile(const std::string& filename, std::vector(data); unsigned char* ptr = stbi_load_from_memory(buffer, static_cast(dataSize), &width, &height, &channels, STBI_rgb_alpha); - if (ptr && width && height) + if (ptr) { // Assign the image properties size.x = width; size.y = height; - // Copy the loaded pixels to the pixel buffer - pixels.resize(width * height * 4); - memcpy(&pixels[0], ptr, pixels.size()); + if (width && height) + { + // Copy the loaded pixels to the pixel buffer + pixels.resize(width * height * 4); + memcpy(&pixels[0], ptr, pixels.size()); + } // Free the loaded pixels (they are now in our own pixel buffer) stbi_image_free(ptr); @@ -192,18 +202,23 @@ bool ImageLoader::loadImageFromStream(InputStream& stream, std::vector& p callbacks.eof = &eof; // Load the image and get a pointer to the pixels in memory - int width, height, channels; + int width = 0; + int height = 0; + int channels = 0; unsigned char* ptr = stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha); - if (ptr && width && height) + if (ptr) { // Assign the image properties size.x = width; size.y = height; - // Copy the loaded pixels to the pixel buffer - pixels.resize(width * height * 4); - memcpy(&pixels[0], ptr, pixels.size()); + if (width && height) + { + // Copy the loaded pixels to the pixel buffer + pixels.resize(width * height * 4); + memcpy(&pixels[0], ptr, pixels.size()); + } // Free the loaded pixels (they are now in our own pixel buffer) stbi_image_free(ptr); diff --git a/src/SFML/Network/Http.cpp b/src/SFML/Network/Http.cpp index 68737f48f..3654fc351 100644 --- a/src/SFML/Network/Http.cpp +++ b/src/SFML/Network/Http.cpp @@ -259,7 +259,8 @@ void Http::Response::parse(const std::string& data) // Copy the actual content data std::istreambuf_iterator it(in); - for (std::size_t i = 0; i < length; i++) + std::istreambuf_iterator itEnd; + for (std::size_t i = 0; ((i < length) && (it != itEnd)); i++) m_body.push_back(*it++); } diff --git a/src/SFML/Network/Socket.cpp b/src/SFML/Network/Socket.cpp index 58ff16afe..29e57e32a 100644 --- a/src/SFML/Network/Socket.cpp +++ b/src/SFML/Network/Socket.cpp @@ -82,6 +82,13 @@ void Socket::create() if (m_socket == priv::SocketImpl::invalidSocket()) { SocketHandle handle = socket(PF_INET, m_type == Tcp ? SOCK_STREAM : SOCK_DGRAM, 0); + + if (handle == priv::SocketImpl::invalidSocket()) + { + err() << "Failed to create socket" << std::endl; + return; + } + create(handle); } } diff --git a/src/SFML/Network/Unix/SocketImpl.cpp b/src/SFML/Network/Unix/SocketImpl.cpp index 9a33d9031..8cbb99b1f 100644 --- a/src/SFML/Network/Unix/SocketImpl.cpp +++ b/src/SFML/Network/Unix/SocketImpl.cpp @@ -26,6 +26,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -71,9 +72,16 @@ void SocketImpl::setBlocking(SocketHandle sock, bool block) { int status = fcntl(sock, F_GETFL); if (block) - fcntl(sock, F_SETFL, status & ~O_NONBLOCK); + { + if (fcntl(sock, F_SETFL, status & ~O_NONBLOCK) == -1) + err() << "Failed to set file status flags: " << errno << std::endl; + } else - fcntl(sock, F_SETFL, status | O_NONBLOCK); + { + if (fcntl(sock, F_SETFL, status | O_NONBLOCK) == -1) + err() << "Failed to set file status flags: " << errno << std::endl; + + } } diff --git a/src/SFML/System/FileInputStream.cpp b/src/SFML/System/FileInputStream.cpp index 82f4d72c9..05510d114 100644 --- a/src/SFML/System/FileInputStream.cpp +++ b/src/SFML/System/FileInputStream.cpp @@ -95,7 +95,9 @@ Int64 FileInputStream::seek(Int64 position) #else if (m_file) { - std::fseek(m_file, static_cast(position), SEEK_SET); + if (std::fseek(m_file, static_cast(position), SEEK_SET)) + return -1; + return tell(); } else diff --git a/src/SFML/System/Unix/ThreadLocalImpl.cpp b/src/SFML/System/Unix/ThreadLocalImpl.cpp index 8af39747a..0664d82e7 100644 --- a/src/SFML/System/Unix/ThreadLocalImpl.cpp +++ b/src/SFML/System/Unix/ThreadLocalImpl.cpp @@ -33,7 +33,8 @@ namespace sf namespace priv { //////////////////////////////////////////////////////////// -ThreadLocalImpl::ThreadLocalImpl() +ThreadLocalImpl::ThreadLocalImpl() : +m_key(0) { pthread_key_create(&m_key, NULL); } diff --git a/src/SFML/Window/Unix/GlxContext.cpp b/src/SFML/Window/Unix/GlxContext.cpp index 289556449..e988476dd 100644 --- a/src/SFML/Window/Unix/GlxContext.cpp +++ b/src/SFML/Window/Unix/GlxContext.cpp @@ -310,7 +310,7 @@ XVisualInfo GlxContext::selectBestVisual(::Display* display, unsigned int bitsPe { // Evaluate all the returned visuals, and pick the best one int bestScore = 0x7FFFFFFF; - XVisualInfo bestVisual; + XVisualInfo bestVisual = XVisualInfo(); for (int i = 0; i < count; ++i) { // Check mandatory attributes diff --git a/src/SFML/Window/Unix/JoystickImpl.cpp b/src/SFML/Window/Unix/JoystickImpl.cpp index d60075db7..f9ddb4269 100644 --- a/src/SFML/Window/Unix/JoystickImpl.cpp +++ b/src/SFML/Window/Unix/JoystickImpl.cpp @@ -439,6 +439,14 @@ namespace sf { namespace priv { +//////////////////////////////////////////////////////////// +JoystickImpl::JoystickImpl() : +m_file(-1) +{ + std::fill(m_mapping, m_mapping + ABS_MAX + 1, 0); +} + + //////////////////////////////////////////////////////////// void JoystickImpl::initialize() { @@ -650,19 +658,23 @@ JoystickState JoystickImpl::JoystickImpl::update() case JS_EVENT_AXIS: { float value = joyState.value * 100.f / 32767.f; - switch (m_mapping[joyState.number]) + + if (joyState.number < ABS_MAX + 1) { - case ABS_X: m_state.axes[Joystick::X] = value; break; - case ABS_Y: m_state.axes[Joystick::Y] = value; break; - case ABS_Z: - case ABS_THROTTLE: m_state.axes[Joystick::Z] = value; break; - case ABS_RZ: - case ABS_RUDDER: m_state.axes[Joystick::R] = value; break; - case ABS_RX: m_state.axes[Joystick::U] = value; break; - case ABS_RY: m_state.axes[Joystick::V] = value; break; - case ABS_HAT0X: m_state.axes[Joystick::PovX] = value; break; - case ABS_HAT0Y: m_state.axes[Joystick::PovY] = value; break; - default: break; + switch (m_mapping[joyState.number]) + { + case ABS_X: m_state.axes[Joystick::X] = value; break; + case ABS_Y: m_state.axes[Joystick::Y] = value; break; + case ABS_Z: + case ABS_THROTTLE: m_state.axes[Joystick::Z] = value; break; + case ABS_RZ: + case ABS_RUDDER: m_state.axes[Joystick::R] = value; break; + case ABS_RX: m_state.axes[Joystick::U] = value; break; + case ABS_RY: m_state.axes[Joystick::V] = value; break; + case ABS_HAT0X: m_state.axes[Joystick::PovX] = value; break; + case ABS_HAT0Y: m_state.axes[Joystick::PovY] = value; break; + default: break; + } } break; } diff --git a/src/SFML/Window/Unix/JoystickImpl.hpp b/src/SFML/Window/Unix/JoystickImpl.hpp index 34fce2ee7..d68e29974 100644 --- a/src/SFML/Window/Unix/JoystickImpl.hpp +++ b/src/SFML/Window/Unix/JoystickImpl.hpp @@ -44,6 +44,12 @@ class JoystickImpl { public: + //////////////////////////////////////////////////////////// + /// \brief Constructor + /// + //////////////////////////////////////////////////////////// + JoystickImpl(); + //////////////////////////////////////////////////////////// /// \brief Perform the global initialization of the joystick module /// diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 7aaf4e469..09156fa02 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -358,6 +358,7 @@ namespace priv //////////////////////////////////////////////////////////// WindowImplX11::WindowImplX11(WindowHandle handle) : m_window (0), +m_screen (NULL), m_inputMethod (NULL), m_inputContext (NULL), m_isExternal (true), @@ -411,6 +412,7 @@ m_fullscreen (false) //////////////////////////////////////////////////////////// WindowImplX11::WindowImplX11(VideoMode mode, const String& title, unsigned long style, const ContextSettings& settings) : m_window (0), +m_screen (NULL), m_inputMethod (NULL), m_inputContext (NULL), m_isExternal (false), @@ -732,7 +734,7 @@ void WindowImplX11::setTitle(const String& title) err() << "Failed to set _NET_WM_NAME property" << std::endl; } - if (utf8StringType && netWmName) + if (utf8StringType && netWmIconName) { if (!changeWindowProperty(netWmIconName, utf8StringType, 8, utf8String.length(), utf8String.c_str())) err() << "Failed to set _NET_WM_ICON_NAME property" << std::endl; @@ -1604,7 +1606,7 @@ void WindowImplX11::cleanup() //////////////////////////////////////////////////////////// -bool WindowImplX11::processEvent(XEvent windowEvent) +bool WindowImplX11::processEvent(XEvent& windowEvent) { // This function implements a workaround to properly discard // repeated key events when necessary. The problem is that the diff --git a/src/SFML/Window/Unix/WindowImplX11.hpp b/src/SFML/Window/Unix/WindowImplX11.hpp index a717bfb3d..f84c86c10 100644 --- a/src/SFML/Window/Unix/WindowImplX11.hpp +++ b/src/SFML/Window/Unix/WindowImplX11.hpp @@ -301,7 +301,7 @@ private: /// \return True if the event was processed, false if it was discarded /// //////////////////////////////////////////////////////////// - bool processEvent(XEvent windowEvent); + bool processEvent(XEvent& windowEvent); //////////////////////////////////////////////////////////// // Member data