Curunir
Curunir

Reputation: 1288

C++/Qt Valgrind uninitialized bytes

I am using valgrind to find and close potential memory issues with my application as well as preventing undefined behaviour. My valgrind call looks like this

valgrind --leak-check=full --track-origins=yes -v ./app

The part of code I am not able to fix is this one:

int App::initSignalHandler(bool dfl)
{
for(size_t sigidx =0; sigidx < sigcount; sigidx++ )
    {
        int signal = stopSignals[sigidx];
        const char *signalName = stopSignalNames[sigidx];      // change signal handler
        struct sigaction new_action;
        sigemptyset (&new_action.sa_mask);
        if(dfl)
            new_action.sa_handler = SIG_DFL;
        else
            new_action.sa_handler = App::stopSignalHandler;
        new_action.sa_flags |= SA_RESTART;
        if(sigaction (signal,&new_action,NULL)!=0)
            qWarning() << Q_FUNC_INFO << "error setting signal handler for : " << signalName;
    }
}

Valgrinds output:

==22462== Conditional jump or move depends on uninitialised value(s)
==22462==    at 0x527427C: __libc_sigaction (sigaction.c:53)
==22462==    by 0x117B83: App::initSignalHandler(bool) (app.cpp:127)
==22462==    by 0x116DD7: main (main.cpp:21)
==22462==  Uninitialised value was created by a stack allocation
==22462==    at 0x117B04: App::initSignalHandler(bool) (app.cpp:88)

app.cpp:88 points to the line with the opening bracket of the function initSignalHandler. I looked at every call of this function. I am always passing the boolean and the parameter also has a default false value, so there is no way that this is undefined behaviour.

app.cpp:127 points to

 if(sigaction (signal,&new_action,NULL)!=0)

I pressume that this error stems from the new_actions struct not being fully initialized. Things I have tried to remedy this: memset(&new_action, 0, sizeof(new_action)); but this does not do anything

Somewhere I also found a suggestion struct sigaction new_action = {0}; but that results in a compiler warning -Wmissing-field-initializers which I'd prefer not to have.

So the question is how should I initialize the sigaction struct correctly. I assume that the supposed origin of this error app.cpp:88 will then also be resolved?

Upvotes: 0

Views: 232

Answers (1)

Miles Budnek
Miles Budnek

Reputation: 30734

Since this is C++, and not C, the proper way to zero-initialize new_object is

struct sigaction new_action{};
//                         ^^ Note the brackets here

Note that the struct keyword is only needed here because of the name conflict between the class sigaction and the function sigaction.


There is one other glaring issue with your function that you should fix: initSignalHandler is declared to return an int, but it doesn't return anything. Falling off the end of a non-void function results in undefined behavior. Your compiler should have warned you about that with something like:

warning: no return statement in function returning non-void [-Wreturn-type]

If it didn't, then you should make sure you have sufficient warnings enabled.

Upvotes: 1

Related Questions