对boost :: uuid的Valgrind日志的分析

时间:2015-03-24 12:12:14

标签: c++ boost valgrind boost-uuid

我使用boost :: uuid来生成唯一ID:

string UUid()
{
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    return boost::lexical_cast<std::string>(uuid);
}

当我使用valgrind分析我的代码时,我会收到以下评论:

Valgrind日志

==47807== Conditional jump or move depends on uninitialised value(s)
==47807==    at 0x441D19: void boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>::seed<boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng> >(boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>&, boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>) (mersenne_twister.hpp:177)
==47807==    by 0x4417EC: void boost::uuids::detail::seed<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >(boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&) (seed_rng.hpp:249)
==47807==    by 0x440EAA: boost::uuids::basic_random_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >::basic_random_generator() (random_generator.hpp:50)
==47807==    by 0x43B4D5: ManageDb::randomid() (ManageDb.cpp:92)
==47807==    by 0x43B57A: ManageDb::fillTables(std::vector<Entity, std::allocator<Entity> > const&) (ManageDb.cpp:103)
==47807==    by 0x40BBF7: DataLoader::extractData() (DataLoader.cpp:78)
==47807==    by 0x42EF26: main (main.cpp:30)
==47807==  Uninitialised value was created by a heap allocation
==47807==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==47807==    by 0x44042A: boost::uuids::detail::seed_rng::sha1_random_digest_() (seed_rng.hpp:167)
==47807==    by 0x44025B: boost::uuids::detail::seed_rng::operator()() (seed_rng.hpp:103)
==47807==    by 0x441C97: boost::uuids::detail::generator_iterator<boost::uuids::detail::seed_rng>::generator_iterator(boost::uuids::detail::seed_rng*) (seed_rng.hpp:218)
==47807==    by 0x4417C5: void boost::uuids::detail::seed<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >(boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&) (seed_rng.hpp:247)
==47807==    by 0x440EAA: boost::uuids::basic_random_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u> >::basic_random_generator() (random_generator.hpp:50)
==47807==    by 0x43B4D5: ManageDb::randomid() (ManageDb.cpp:92)
==47807==    by 0x43B57A: ManageDb::fillTables(std::vector<Entity, std::allocator<Entity> > const&) (ManageDb.cpp:103)
==47807==    by 0x40BBF7: DataLoader::extractData() (DataLoader.cpp:78)
==47807==    by 0x42EF26: main (main.cpp:30)
==47807== 

问题

  • 为什么valgrind会为boost::uuid生成这些评论?
  • 如果在升级中遇到问题我可以忽略吗?

可能的错误

  • 根据此链接Ticket #7248 (reopened Bugs),这是一个可能的错误。如果是一个错误,我怎么能让valgrind忽略它呢?

Valgrind命令

valgrind --leak-check=full --track-origins=yes --suppressions=valgrind.supp ./MyProgram > valgrind-log.txt

valgrind.supp

# supression file for continuum with valgrind
# to generate each supression use: --gen-suppressions=yes option
# to use this supression file, use: --suppressions=<this filename>

{
Crypt_r
Memcheck:Cond
obj:/lib/libc-2.11.1.so
fun:__sha512_crypt_r
fun:crypt_r
}

{
Crypt_r use of uninitialised value of size 8
Memcheck:Value8
obj:/lib/libc-2.11.1.so
fun:__sha512_crypt_r
fun:crypt_r
}

{
String S_Create
Memcheck:Leak
fun:_Znwm
fun:_ZNSs4_Rep9_S_createEmmRKSaIcE
}

{
Mongo OID
Memcheck:Value8
fun:_ZN5mongo10toHexLowerEPKvi
fun:_ZNK5mongo3OID3strEv
}

1 个答案:

答案 0 :(得分:1)

请检查http://www.boost.org/doc/libs/1_50_0/libs/uuid/uuid.html

boost::uuids::basic_random_generator class默认构造函数为随机数生成器播放多个不同值的SHA-1哈希,包括std::time(0)std::clock()**uninitialized data**,{{1等等..

  

...使用Valgrind会使用boost :: uuids :: basic_random_generator的默认构造函数产生许多误报。一种解决方案是抑制Valgrind文档中描述的错误。另一种解决方案是使用boost :: uuids :: basic_random_generator的不同构造函数,并显式传入随机数生成器。