3

I am using boost::uuid in order to generate unique ids:

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

When I use valgrind in order to analyse my code i get the following remarks:

Valgrind log

==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== 

Questions

  • Why is valgrind generating these remarks for boost::uuid?
  • If it's a problem in boost can I ignore it?

Possible bug


Valgrind command

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
}
Community
  • 1
  • 1
Hani Goc
  • 2,371
  • 5
  • 45
  • 89
  • Is it causing your program to crash? There are a lot of things valgrind picks up that live inside boost and/or system libraries. A lot of the time, they're caused by the values you pass to those constructs. A lot of the time, they're innocuous and can't be helped/can be safely ignored. Valgrind isn't for bulletproofing your program, but for getting it 90% of the way there. – Qix - MONICA WAS MISTREATED Mar 24 '15 at 12:16
  • @Qix oh no it's not crashing my program. But valgrind is considering that it's an error. Can I ignore it while using valgrind? – Hani Goc Mar 24 '15 at 12:18
  • What arguments are you running valgrind with? – Qix - MONICA WAS MISTREATED Mar 24 '15 at 12:23
  • I am just using the following command: **valgrind --leak-check=full --track-origins=yes --suppressions=valgrind.supp ./MyProgram > valgrind-log.txt** – Hani Goc Mar 24 '15 at 12:24
  • 1
    `--track-origins=yes` is the culprit here. It checks uninitialized data, which (in my opinion) is unreliable. Valgrind creates a virtual machine that allows it to see when a program not only allocates memory, but reads/writes to it. There are a lot of heuristics valgrind doesn't run on code that can definitely result in this false positive. The idea is to be overly verbose instead of under-verbose. – Qix - MONICA WAS MISTREATED Mar 24 '15 at 12:29
  • @Qix i'll remove it from the command. It is still reporting this error – Hani Goc Mar 24 '15 at 12:35
  • Qix actually, valgrinds reporting of unitialized data is prone to _underreporting_. What's causing "false positives" is that a lot of code out there never bothers to clear buffers before use. The typical pattern is: 1. allocate buffer 2. write part of buffer 3. read whole buffer (e.g. to copy it)). – sehe Mar 24 '15 at 12:59
  • @sehe depends on the libraries you use. Analyzers either over-report or under-report. Valgrind tends to over-report in my experience, which isn't necessarily a bad thing. But you're correct. I distinguish it as a false positive simply because fixing it requires a lot more work to fix an issue that isn't much of an issue to begin with (unless you contribute to Boost frequently). – Qix - MONICA WAS MISTREATED Mar 24 '15 at 13:25

1 Answers1

3

Please check http://www.boost.org/doc/libs/1_50_0/libs/uuid/uuid.html

The boost::uuids::basic_random_generator class default constructor seeds the random number generator with a SHA-1 hash of a number of different values including std::time(0), std::clock(), **uninitialized data**, value return from new unsigned int, etc..

...Using Valgrind produces a number of false positives with the default constructor of boost::uuids::basic_random_generator. One solution is to suppress the errors as described in Valgrind's documentation. Another solution is to use a different constructor of boost::uuids::basic_random_generator and explicitly pass in a random number generator.

Stefan
  • 17,448
  • 11
  • 60
  • 79
Sikor
  • 31
  • 2