Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Fix Issue 21363 - [REG2.094] Implementation of core.bitop.ror(x,0) is…
Browse files Browse the repository at this point in the history
… using UB

This is a quick fix to Issue 21363. When `count` is set to `0`, `T.sizeof * 8 - count` will always be `T.sizeof * 8` therefore fails compiler check here:
https://github.com/dlang/dmd/blob/81f9f57257da8733cf741355524bb8a3ec9c45ce/src/dmd/dinterpret.d#L3018-L3024
  • Loading branch information
Chigusa0w0 committed May 2, 2021
1 parent cc7a13e commit 042c004
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/core/bitop.d
Original file line number Diff line number Diff line change
Expand Up @@ -953,27 +953,39 @@ pure T rol(T)(const T value, const uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T))
{
assert(count < 8 * T.sizeof);
if (count == 0)
return cast(T) value;

return cast(T) ((value << count) | (value >> (T.sizeof * 8 - count)));
}
/// ditto
pure T ror(T)(const T value, const uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T))
{
assert(count < 8 * T.sizeof);
if (count == 0)
return cast(T) value;

return cast(T) ((value >> count) | (value << (T.sizeof * 8 - count)));
}
/// ditto
pure T rol(uint count, T)(const T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T))
{
static assert(count < 8 * T.sizeof);
static if (count == 0)
return cast(T) value;

return cast(T) ((value << count) | (value >> (T.sizeof * 8 - count)));
}
/// ditto
pure T ror(uint count, T)(const T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T))
{
static assert(count < 8 * T.sizeof);
static if (count == 0)
return cast(T) value;

return cast(T) ((value >> count) | (value << (T.sizeof * 8 - count)));
}

Expand All @@ -996,4 +1008,9 @@ unittest

assert(rol!3(a) == 0b10000111);
assert(ror!3(a) == 0b00011110);

enum c = rol(uint(1), 0);
enum d = ror(uint(1), 0);
assert(c == uint(1));
assert(d == uint(1));
}

0 comments on commit 042c004

Please sign in to comment.