Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make WithParamInterface<T>::GetParam static #1830

Closed
wants to merge 1 commit into from

Conversation

1camper
Copy link
Contributor

@1camper 1camper commented Sep 12, 2018

The decision for GetParam to be non-static can lead to order dependencies in multiple inheritance cases for otherwise normal usage.

As an example from libvpx we have

#define GET_PARAM(k) std::tr1::get< k >(GetParam())
template<class T1, class T2>
class CodecTestWith2Params : public ::testing::TestWithParam<
std::tr1::tuple< const libvpx_test::CodecFactory*, T1, T2 > > {
};
...
class ActiveMapTest
: public ::libvpx_test::EncoderTest,
public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
...
ActiveMapTest() : EncoderTest(GET_PARAM(0)) {}

here GetParam is called before the TestWithParam subobject has begun construction. This is, of course, undefined behaviour (and the address-sanitizer will complain). The problem can be avoided by changing the order of inheritance. But this seems to be a rather artificial restriction which does not necessarily weigh less than the desire to avoid mistakes that were meant to be prevented by making GetParam non-static.

Signed-off-by: Matthias Räncker <theonetruecamper@gmx.de>
1camper added a commit to 1camper/libvpx that referenced this pull request Sep 21, 2018
When running tests built with
-fsanitize=undefined and--disable-optimizations
the sanitizer will emit errors of the following general form:

runtime error: member call on address 0xxxxxxxxx which does not
point to an object of type 'WithParamInterface'
0xxxxxxxxx: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 ...
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr

This can be traced to calls to WithParamInterface<T>::GetParam before
the object argument has been initialized. Although GetParam only
accesses static data it is a non-static member function. This causes
that call to have undefined behaviour.
The patch makes GetParam a static member function.

upstream pull request:
google/googletest#1830

The alternative - if the pull request is denied - would be to
modify all parameterized tests to have them derive from
::libvpx_test::CodecTestWith*Params as the first base class.

Signed-off-by: Matthias Räncker <theonetruecamper@gmx.de>
Change-Id: I8e91a4fba5438c9b3e93fa398f789115ab86b521
@gennadiycivil
Copy link
Contributor

merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants