Opened 13 years ago

Closed 13 years ago

#3971 closed Patches (fixed)

basic_random_generator doesn't produce a fully randomized uuid when sizeof(unsigned long) == 4

Reported by: gareth.sylvester-bradley@… Owned by: Andy Tompkins
Milestone: Boost 1.43.0 Component: uuid
Version: Boost 1.42.0 Severity: Showstopper
Keywords: Cc:

Description

There is a flaw in basic_random_generator<> (boost/uuid/uuid_generators.hpp, line 366-374) that means it will only generate about 4 billion different values before a collision on the many platforms where sizeof(unsigned long) == 4.

On these platforms the first 8 bytes of the uuid are populated with random data, but the contents of the remaining 8 bytes are undefined.

In my tests they are populated with repeated data, due to Microsoft's specific implementation for the undefined behaviour of >> when its "right operand is greater than or equal to the length in bits of the promoted left operand."

For example, try the following test program with a Win32 build by Visual C++ 9.0 –

#include <iostreams>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

int main( int, char*[] )
{
  std::cout << boost::uuids::random_generator()() << std::endl;
  return 0;
}

This produced for me:

f1633d75-ee94-47b9-ae94-37b9ee9437b9

Or wrapped to show the problem:

f1633d75-
ee94-47b9-
ae94-37b9
ee94 37b9

The simplest patch could be just:

370a370
>                 i=0;

Change History (2)

comment:1 by Dave Abrahams, 13 years ago

Severity: ProblemShowstopper

comment:2 by Andy Tompkins, 13 years ago

Resolution: fixed
Status: newclosed

Fixed as suggested in both trunk and merged into release.

Note: See TracTickets for help on using tickets.