简体   繁体   English

GTest:使用 std::function 参数化测试时的 Valgrind 警告

[英]GTest: Valgrind warnings when using std::function parametrized test

When running the following dummy Google Test program with valgrind (3.14.0), I receive a series of warning messages which leads me to believe there might be something wrong with the way the program is structured.当使用 valgrind (3.14.0) 运行以下虚拟 Google 测试程序时,我收到一系列警告消息,这让我相信程序的结构方式可能有问题。 This is the first time I have observed something like this and I believe it is related to the fact that FooTest is parametrized with a std::function instantiation.这是我第一次观察到这样的事情,我相信这与 FooTest 使用std::function实例化参数化这一事实有关。

#include <functional>

#include "gtest/gtest.h"

using ::testing::TestWithParam;
using ::testing::Values;


namespace
{

int add(int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

}

class FooTestHarness
    : public TestWithParam<std::function<int(int, int)>> {};

TEST_P(FooTestHarness, FooTest)
{
  // ...
}

INSTANTIATE_TEST_CASE_P(FooInstantiation, FooTestHarness, Values(add, sub));

The following is an excerpt from the (shortened) valgrind ouput:以下是(缩短的)valgrind 输出的摘录:

==20816== Use of uninitialised value of size 8
==20816==    at 0x4C299BD: _itoa_word (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C2D269: vfprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C55AC5: vsnprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C35503: snprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x13033C: testing::(anonymous namespace)::PrintByteSegmentInObjectTo(unsigned char const*, unsigned long, unsigned long, std::ostream*)
...
==20816== Conditional jump or move depends on uninitialised value(s)
==20816==    at 0x4C299CE: _itoa_word (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C2D269: vfprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C55AC5: vsnprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C35503: snprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x13033C: testing::(anonymous namespace)::PrintByteSegmentInObjectTo(unsigned char const*, unsigned long, unsigned long, std::ostream*)
...
==20816== Conditional jump or move depends on uninitialised value(s)
==20816==    at 0x4C2E100: vfprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C55AC5: vsnprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C35503: snprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x13033C: testing::(anonymous namespace)::PrintByteSegmentInObjectTo(unsigned char const*, unsigned long, unsigned long, std::ostream*)
...
==20816== Conditional jump or move depends on uninitialised value(s)
==20816==    at 0x4C2D3BD: vfprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C55AC5: vsnprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x4C35503: snprintf (in /usr/lib/libc-2.28.so)
==20816==    by 0x13033C: testing::(anonymous namespace)::PrintByteSegmentInObjectTo(unsigned char const*, unsigned long, unsigned long, std::ostream*)
...

This happens for several versions of GTest including the most recent.这发生在 GTest 的多个版本中,包括最新版本。 Can someone diagnose whether this is my fault or a problem with GTest and/or valgrind?有人可以诊断这是我的错还是 GTest 和/或 valgrind 的问题?

EDIT: in case this is relevant: I'm on Linux and using gcc 8.2.1编辑:如果这是相关的:我在 Linux 上并使用 gcc 8.2.1

I faced this issue a while ago and got it (partially) solved by imlementing operator << for my class.不久前我遇到了这个问题,并通过为我的班级实现operator <<来(部分)解决了这个问题。 You could do that for std::function<int(int, int)> :你可以为std::function<int(int, int)>

#include <ostream>
#include <functional>

std::ostream& operator<<(std::ostream& os, const std::function<int(int, int)>& myFunction) {
  return os << "Do something you wish to represent your function";
}

Reference: https://github.com/google/googletest/blob/master/docs/advanced.md#teaching-googletest-how-to-print-your-values参考: https : //github.com/google/googletest/blob/master/docs/advanced.md#teaching-googletest-how-to-print-your-values

Edit : It seems GTest will try to print your parameters using the operator << (which totally makes sense, of course).编辑:似乎 GTest 会尝试使用operator <<打印您的参数(当然,这完全有道理)。 However, I'm still unsure on how this relates to the use of unitiliased value valgrind catches.但是,我仍然不确定这与使用单元化值 valgrind 捕获有什么关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM