Search code examples
bashvalgrind

valgrind --error-exitcode , negative values


I'm trying to set a custom error code if valgrind reports an error(building a test script), but it doesn't seem to work as expected when setting a negative value, this how I run valgrind:

valgrind --leak-check=full --error-exitcode=-1 -q ./leaks

This however, returns 0 (which is the return value of leaks), instead of -1.

echo $?
0

If a positive value is set, it works as expected:

valgrind --leak-check=full --error-exitcode=1 -q ./leaks

echo $?
1

The Valgrind manual, specifies:

--error-exitcode=<number> [default: 0]

Specifies an alternative exit code to return if Valgrind reported any errors in the run. When set to the default value (zero), the return value from Valgrind will always be the return value of the process being simulated. When set to a nonzero value, that value is returned instead, if Valgrind detects any errors. This is useful for using Valgrind as part of an automated test suite, since it makes it easy to detect test cases for which Valgrind has reported errors, just by inspecting return codes.

Anybody knows why this behavior occurs?

Using Valgrind-3.10.0


Solution

  • The manual is inaccurate, Valgrind can't return a negative value, it is in Valgrind's source. It assigns the option's value to clo_eror_exitcode:

    else if VG_INT_CLO (arg, "--error-exitcode", VG_(clo_error_exitcode)) {}
    

    and then checks if it is positive, otherwise it ignores the option.

    if (VG_(clo_error_exitcode) > 0
        && VG_(get_n_errs_found)() > 0) {
       VG_(client_exit)( VG_(clo_error_exitcode) );
    } else {
       /* otherwise, return the client's exit code, in the normal
          way. */
       VG_(client_exit)( VG_(threads)[tid].os_state.exitcode );
    }
    

    Even if it could, it would not be useful as per Linux, negative exit codes?