You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As mentioned in b93d569#r43494808, the changes to EncodeEntropyRand in #7 cause the random component to be 0 when the function is called on a ulid. The problem occurs with how casting is happening here:
In C++ order of operations, a c style cast happens before multiplication or division. That means this expression is resolved:
(std::rand() * 255ull)
then cast as a uint8_t before the division by RAND_MAX occurs. Thus, you end up with a number always less than 0 (since a unint8_t can only hold up to 255, and RAND_MAX (on 32 bit platforms) is 2147483647).
Fix
A fix was proposed in #9. However, I think that only capturing the last 0xFF part of the output of rand() may be suboptimal (see std::rand() notes here).
Another fix would be to force the correct order of operations and cast the entire expression. I've tested that this is working.
The text was updated successfully, but these errors were encountered:
Issue Description
As mentioned in b93d569#r43494808, the changes to
EncodeEntropyRand
in #7 cause the random component to be 0 when the function is called on a ulid. The problem occurs with how casting is happening here:In C++ order of operations, a c style cast happens before multiplication or division. That means this expression is resolved:
then cast as a uint8_t before the division by
RAND_MAX
occurs. Thus, you end up with a number always less than 0 (since a unint8_t can only hold up to 255, andRAND_MAX
(on 32 bit platforms) is 2147483647).Fix
A fix was proposed in #9. However, I think that only capturing the last
0xFF
part of the output ofrand()
may be suboptimal (see std::rand() notes here).Another fix would be to force the correct order of operations and cast the entire expression. I've tested that this is working.
The text was updated successfully, but these errors were encountered: