Fixed issues reported by Coverity Scan static analysis (run using Linux build configuration).

This commit is contained in:
binary1248 2016-02-23 03:07:12 +01:00 committed by Lukas Dürrenberger
parent 23ea17eab7
commit 3ff1d251a8
13 changed files with 93 additions and 41 deletions

View File

@ -3,7 +3,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iomanip>
#include <iostream>
#include <string>
@ -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;

View File

@ -3,7 +3,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iomanip>
#include <iostream>
@ -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

View File

@ -103,18 +103,23 @@ bool ImageLoader::loadImageFromFile(const std::string& filename, std::vector<Uin
pixels.clear();
// 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(filename.c_str(), &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);
@ -141,19 +146,24 @@ bool ImageLoader::loadImageFromMemory(const void* data, std::size_t dataSize, st
pixels.clear();
// 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;
const unsigned char* buffer = static_cast<const unsigned char*>(data);
unsigned char* ptr = stbi_load_from_memory(buffer, static_cast<int>(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<Uint8>& 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);

View File

@ -259,7 +259,8 @@ void Http::Response::parse(const std::string& data)
// Copy the actual content data
std::istreambuf_iterator<char> it(in);
for (std::size_t i = 0; i < length; i++)
std::istreambuf_iterator<char> itEnd;
for (std::size_t i = 0; ((i < length) && (it != itEnd)); i++)
m_body.push_back(*it++);
}

View File

@ -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);
}
}

View File

@ -26,6 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network/Unix/SocketImpl.hpp>
#include <SFML/System/Err.hpp>
#include <errno.h>
#include <fcntl.h>
#include <cstring>
@ -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;
}
}

View File

@ -95,7 +95,9 @@ Int64 FileInputStream::seek(Int64 position)
#else
if (m_file)
{
std::fseek(m_file, static_cast<std::size_t>(position), SEEK_SET);
if (std::fseek(m_file, static_cast<std::size_t>(position), SEEK_SET))
return -1;
return tell();
}
else

View File

@ -33,7 +33,8 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
ThreadLocalImpl::ThreadLocalImpl()
ThreadLocalImpl::ThreadLocalImpl() :
m_key(0)
{
pthread_key_create(&m_key, NULL);
}

View File

@ -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

View File

@ -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;
}

View File

@ -44,6 +44,12 @@ class JoystickImpl
{
public:
////////////////////////////////////////////////////////////
/// \brief Constructor
///
////////////////////////////////////////////////////////////
JoystickImpl();
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the joystick module
///

View File

@ -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

View File

@ -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