Search code examples
c++cinitializationgoogletest

Why would a Google Test pass in one machine yet fail in another?


We use GoogleTest to test our code in Eclipse but I have a test which passes in one machine and fails in another due to a pointer de-reference and I'm not sure why it runs differently. This test was recently added by a jr dev to the end of the test file to validate a small change. The test included an error yet passed somehow in their machine:

TEST(TEST_SUITE, TEST_NAME)
{
    uint8* response_data;
    uint16 response_length = 1;
    response_data[0] = 0x54;
    //more code and actual validation
}

The attempt to write to a null pointer caused my application to fail, yet somehow in their machine the data was defined and therefore able to pass. I set breakpoints at the first line of the test to see and this is what I get:

My Machine - null pointer: Failing Test at first line in test

Their Machine - defined pointer (and length): Passing Test at first line in test

Somehow, although the call stack is the same and although the tests run in the same order, in one machine the variables are somehow carrying some other values.

Is there a reason why this would behave differently on two different machines with the same codebase? Do variables defined in a test not have a limited scope?


Solution

  • As UnholySheep mentioned in the comments, responseData was uninitialized, and your workmates are using it.

    As the Wikipedia points out, the C++ standard does not specify what should happen in this case (this is called undefined behaviour). That means it could work with them and not with you. Thus, if you are using a different compiler than they are (or even if you set different compilation flags like optimisation levels, or whether you use debug), you may not get the same results.

    To explain your specific case more deeply, with them, it seems the pointer is not set to anything (responseData had a garbage value), whereas with you, responseData was set to nullptr.