Skip to content

Commit 1562616

Browse files
committed
fix short overflow UB
1 parent 85c8e28 commit 1562616

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

include/uuid.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,11 @@ namespace uuids
835835
return ns / 100;
836836
}
837837

838-
static short get_clock_sequence()
838+
static unsigned short get_clock_sequence()
839839
{
840840
static std::mt19937 clock_gen(std::random_device{}());
841-
static std::uniform_int_distribution<short> clock_dis{ -32768, 32767 };
842-
static std::atomic_short clock_sequence = clock_dis(clock_gen);
841+
static std::uniform_int_distribution<unsigned short> clock_dis;
842+
static std::atomic_ushort clock_sequence = clock_dis(clock_gen);
843843
return clock_sequence++;
844844
}
845845

@@ -852,15 +852,15 @@ namespace uuids
852852

853853
auto tm = get_time_intervals();
854854

855-
short clock_seq = get_clock_sequence();
855+
auto clock_seq = get_clock_sequence();
856856

857857
auto ptm = reinterpret_cast<uuids::uuid::value_type*>(&tm);
858858

859859
memcpy(&data[0], ptm + 4, 4);
860860
memcpy(&data[4], ptm + 2, 2);
861861
memcpy(&data[6], ptm, 2);
862862

863-
memcpy(&data[8], reinterpret_cast<uuids::uuid::value_type*>(&clock_seq), 2);
863+
memcpy(&data[8], &clock_seq, 2);
864864

865865
// variant must be 0b10xxxxxx
866866
data[8] &= 0xBF;

0 commit comments

Comments
 (0)