SFML community forums

General => General discussions => Topic started by: dabbertorres on May 25, 2014, 08:18:19 am

Title: Valgrind Conditional Jumps
Post by: dabbertorres on May 25, 2014, 08:18:19 am
While debugging an application of mine, Valgrind threw several errors, some of which might be coming from SFML (Joystick stuff): "dependencies on uninitialized value(s)".

These may be false positives, but I was hoping someone else more familiar with this kind of stuff could have a look, just in case.

non-SFML programs do not have these errors, so it's not likely anything else.

System:
Arch Linux x86_64
Latest SFML sources

Minimal Reproduction:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
sf::RenderWindow window;
window.create(sf::VideoMode(800, 600), "SFML");

return 0;
}


Valgrind Output:
(click to show/hide)

Looking at SFML's code, I think the errors are here (https://github.com/SFML/SFML/blob/6b2a4c27db867fe4921aeeed1a9250af27c5fc2c/src/SFML/Window/Unix/JoystickImpl.cpp#L288) and here (https://github.com/SFML/SFML/blob/6b2a4c27db867fe4921aeeed1a9250af27c5fc2c/src/SFML/Window/Unix/JoystickImpl.cpp#L154);
Title: Re: Valgrind Conditional Jumps
Post by: Nexus on May 25, 2014, 10:32:38 am
Looking at SFML's code, I think the errors are here (https://github.com/SFML/SFML/blob/6b2a4c27db867fe4921aeeed1a9250af27c5fc2c/src/SFML/Window/Unix/JoystickImpl.cpp#L288) and here (https://github.com/SFML/SFML/blob/6b2a4c27db867fe4921aeeed1a9250af27c5fc2c/src/SFML/Window/Unix/JoystickImpl.cpp#L154);
The function std::snprintf (http://en.cppreference.com/w/cpp/io/c/fprintf) and the macro JSIOCGNAME (https://www.kernel.org/doc/Documentation/input/joystick-api.txt) both use the pointer as an output parameter, the C string need not be initialized.

I wonder why Valgrind claims that the standard library functions std::strlen and std::ctype::do_widen operate on uninitialized memory... They shouldn't, since we use std::snprintf and JSIOCGNAME as documented.
Title: Re: Valgrind Conditional Jumps
Post by: binary1248 on May 25, 2014, 11:15:50 am
I think the Linux source (http://code.woboq.org/linux/linux/drivers/input/joydev.c.html#582) shows us why valgrind is complaining:
case JSIOCGNAME(0):
        name = dev->name;
        if (!name)
                return 0;

        len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
        return copy_to_user(argp, name, len) ? -EFAULT : len;
 
In the case when the joystick has an empty name, it doesn't touch the output parameter in any way and just returns 0. When you initialise the string from the char joyname[128], everything in it is going to be uninitialised since not even a null was written to the first byte.

Judging from this code, I guess it is safe to assume that checking for > 0 from the ioctl call is good enough and would avoid this situation? If anything (even an empty string) was written to the output parameter, ioctl will return a positive value since strlen(name) + 1 is always positive in that case.