I am currently maintaining a memory pool. I have recently added valgrind function calls to this pool so as to make it more useful for detecting valgrind errors occurring via the use of said pool. What I want to do with this is to write a unit test to check that my calls to valgrind functions are working properly. E.g.,
int main(void)
{
int * test = pool_malloc(sizeof(*test)); // details not important
*test = 3;
pool_free(test); // details not important
if (*test == 2)
{
printf("HERE");
}
assert(VALGRIND_COUNT_ERRORS == 1);
}
This code now correctly gives me an invalid read error, whereas previously it wouldn't as even though the memory was returned to the pool, it wasn't actually free
-d. However, I can't use this exact code as our unit test framework assumes that any valgrind errors means the test failed, and therefore my above test would fail. I've tried using VALGRIND_DISABLE_ERROR_REPORTING
, but that also appears to disable not just reporting, but also checking for errors - i.e., VALGRIND_COUNT_ERRORS
now returns 0. What I really want is something like VALGRIND_DISABLE_ERROR_REPORTING_BUT_KEEP_COUNTING_ERRORS_THAT_OCCUR
- does something like that exist? Is there a better way to accomplish what I want to do?
What you can do is to use the valgrind client request VALGRIND_COUNT_ERRORS.
valgrind.h among others says:
...
/* Can be useful in regression testing suites -- eg. can
send Valgrind's output to /dev/null and still count
errors. */
VG_USERREQ__COUNT_ERRORS = 0x1201,
...
/* Counts the number of errors that have been recorded by a tool. Nb:
the tool must record the errors with VG_(maybe_record_error)() or
VG_(unique_error)() for them to be counted. */
So, something like: valgrind --log-file=/dev/null your_program will make the valgrind errors reported to /dev/null, and your_program can then output an error if the error count is not as expected.