How can I intentionally initialize a variable with uninitialized data so that valgrind treats the variable as uninitialized?

I have a garbage data buffer in my code that I pass to a function that writes to it. I do not need the data of this garbage buffer. Since I need such garbage data buffers in several places, but never read them, I use the same garbage buffer everywhere.

When some dummy code is read from the garbage data buffer, it will be fatal or, for example, when I use the garbage data buffer, where I had to use the proper allocated buffer.

Ideally, I would like the fake code to crash quickly, so I scrambled the data after calling the write function, so no one can use (and rely on) the recycling data.

Now I had the idea that I could copy uninitialized data from garbage data instead of scrambling it, so valgrind could find all reads from the garbage buffer.

My current solution is to new [] a few bytes and copy the uninitialized contents to the garbage buffer, and then delete [] the uninitialized data. Since uninitialized data is in most cases (unfortunately), I XOR it with a template. All this seems rather complicated for such a simple thing.

Has anyone come up with a better solution?

+3
source share
1 answer

Valgrind (and memcheck) have an API API !

You can simply do this:

#include <memcheck.h>

// ....

VALGRIND_MAKE_MEM_UNDEFINED(trash_buffer, trash_length);

VALGRIND_MAKE_MEM_NOACCESS, , valgrind , .

valgrind . , valgrind.

+7

All Articles