From 3c429387f2cee104ce9792aad3d204c9c90fc2e3 Mon Sep 17 00:00:00 2001 From: Emmanuel Atse Date: Thu, 18 Jul 2013 04:04:22 +0200 Subject: [PATCH 1/3] Fixed undefined behavior of returned pointer --- src/SFML/Window/Linux/WindowImplX11.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SFML/Window/Linux/WindowImplX11.cpp b/src/SFML/Window/Linux/WindowImplX11.cpp index e26627ff7..ae684fd5c 100644 --- a/src/SFML/Window/Linux/WindowImplX11.cpp +++ b/src/SFML/Window/Linux/WindowImplX11.cpp @@ -62,19 +62,19 @@ namespace // Find the name of the current executable const char* findExecutableName() { - char buffer[512]; + //Default fallback name + const char* executableName = "sfml"; + static char buffer[512]; std::size_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); + if ((length > 0) && (length < sizeof(buffer))) { // Remove the path to keep the executable name only buffer[length] = '\0'; - return basename(buffer); - } - else - { - // Fallback name - return "sfml"; + executableName = basename(buffer); } + std::memcpy(buffer, executableName, std::strlen(executableName) + 1); + return buffer; } } From 43a21e2acc1d79734f899fba0f51dda67f52bf89 Mon Sep 17 00:00:00 2001 From: Emmanuel Atse Date: Thu, 18 Jul 2013 04:11:35 +0200 Subject: [PATCH 2/3] Fixed some uninitialized values --- src/SFML/Audio/SoundFile.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SFML/Audio/SoundFile.cpp b/src/SFML/Audio/SoundFile.cpp index 5e2532749..91db819a4 100644 --- a/src/SFML/Audio/SoundFile.cpp +++ b/src/SFML/Audio/SoundFile.cpp @@ -97,6 +97,7 @@ bool SoundFile::openRead(const std::string& filename) // Open the sound file SF_INFO fileInfo; + fileInfo.format = 0; m_file = sf_open(filename.c_str(), SFM_READ, &fileInfo); if (!m_file) { @@ -132,6 +133,7 @@ bool SoundFile::openRead(const void* data, std::size_t sizeInBytes) // Open the sound file SF_INFO fileInfo; + fileInfo.format = 0; m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_memory); if (!m_file) { @@ -169,6 +171,7 @@ bool SoundFile::openRead(InputStream& stream) // Open the sound file SF_INFO fileInfo; + fileInfo.format = 0; m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_stream); if (!m_file) { From 5a42c919856c1eea456e81ce4afadb839ed83dec Mon Sep 17 00:00:00 2001 From: Emmanuel Atse Date: Thu, 18 Jul 2013 16:23:57 +0200 Subject: [PATCH 3/3] Avoid const_cast, static buffer and buffer overlap --- src/SFML/Window/Linux/WindowImplX11.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/SFML/Window/Linux/WindowImplX11.cpp b/src/SFML/Window/Linux/WindowImplX11.cpp index ae684fd5c..1b712103f 100644 --- a/src/SFML/Window/Linux/WindowImplX11.cpp +++ b/src/SFML/Window/Linux/WindowImplX11.cpp @@ -60,21 +60,18 @@ namespace } // Find the name of the current executable - const char* findExecutableName() + void findExecutableName(char* buffer, std::size_t bufferSize) { //Default fallback name const char* executableName = "sfml"; - static char buffer[512]; - std::size_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); - - if ((length > 0) && (length < sizeof(buffer))) + std::size_t length = readlink("/proc/self/exe", buffer, bufferSize); + if ((length > 0) && (length < bufferSize)) { // Remove the path to keep the executable name only buffer[length] = '\0'; executableName = basename(buffer); } - std::memcpy(buffer, executableName, std::strlen(executableName) + 1); - return buffer; + std::memmove(buffer, executableName, std::strlen(executableName) + 1); } } @@ -249,10 +246,11 @@ m_previousSize(-1, -1) } // Set the window's WM class (this can be used by window managers) - const char* windowClass = findExecutableName(); + char windowClass[512]; + findExecutableName(windowClass, sizeof(windowClass)); XClassHint* classHint = XAllocClassHint(); - classHint->res_name = const_cast(windowClass); - classHint->res_class = const_cast(windowClass); + classHint->res_name = windowClass; + classHint->res_class = windowClass; XSetClassHint(m_display, m_window, classHint); XFree(classHint);