Skip to content

[Bug]: googlemock's own unit tests invoke Undefined Behavior #4846

@carlosgalvezp

Description

@carlosgalvezp

Describe the issue

The documentation states:

Important note: gMock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. Do not alternate between calls to EXPECT_CALL() and calls to the mock functions, and do not set any expectations on a mock after passing the mock to an API.

However, googlemock's own unit tests are violating this requirement, thus invoking Undefined Behavior:

https://github.com/google/googletest/blob/main/googlemock/test/gmock-actions_test.cc#L1472
https://github.com/google/googletest/blob/main/googlemock/test/gmock-actions_test.cc#L1867
https://github.com/google/googletest/blob/main/googlemock/test/gmock-actions_test.cc#L1907
https://github.com/google/googletest/blob/main/googlemock/test/gmock-matchers-arithmetic_test.cc#L1189
https://github.com/google/googletest/blob/main/googlemock/test/gmock-matchers-comparisons_test.cc#L1060
https://github.com/google/googletest/blob/main/googlemock/test/gmock-matchers-containers_test.cc#L1648
https://github.com/google/googletest/blob/main/googlemock/test/gmock-matchers-containers_test.cc#L1776
https://github.com/google/googletest/blob/main/googlemock/test/gmock-matchers-containers_test.cc#L2107
https://github.com/google/googletest/blob/main/googlemock/test/gmock-spec-builders_test.cc#L305
https://github.com/google/googletest/blob/main/googlemock/test/gmock-spec-builders_test.cc#L2422

There are two alternatives moving forward:

  • Fixing the tests.
  • Making this behavior well-defined, i.e. removing the parts of the documentation that say this is undefined behavior.

Steps to reproduce the problem

N/A

What version of GoogleTest are you using?

trunk

What operating system and version are you using?

N/A

What compiler and version are you using?

N/A

What build system are you using?

N/A

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions