在使用valgrind(3.14.0)运行以下虚拟Google Test程序时,我收到一系列警告消息,使我相信该程序的结构可能存在问题。这是我第一次观察到这样的情况,我相信这与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));
以下是(缩短的)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*)
...
GTest的多个版本(包括最新版本)都会发生这种情况。有人可以诊断这是我的错还是GTest和/或valgrind问题?
编辑:如果这是相关的:我在Linux上并使用gcc 8.2.1
答案 0 :(得分:0)
我不久前遇到了这个问题,并通过为我的班级实现 operator <<
解决了(部分)问题。你可以为 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";
}
编辑:似乎 GTest 会尝试使用 operator <<
打印您的参数(当然这完全有道理)。但是,我仍然不确定这与使用单元化值 valgrind 捕获有何关系。