Opened 8 years ago

Closed 8 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:


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:


Or wrapped to show the problem:

ee94 37b9

The simplest patch could be just:

>                 i=0;

Attachments (0)

Change History (2)

comment:1 Changed 8 years ago by Dave Abrahams

Severity: ProblemShowstopper

comment:2 Changed 8 years ago by Andy Tompkins

Resolution: fixed
Status: newclosed

Fixed as suggested in both trunk and merged into release.

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Andy Tompkins.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.